Spring Cloud服务网关整合

  1. 云栖社区>
  2. 博客>
  3. 正文

Spring Cloud服务网关整合

xiaoshuaiv5 2019-03-29 10:55:31 浏览552
展开阅读全文

网关

网管是程序或者系统之间的连接节点,扮演者程序或系统之间的门户,允许它们之前通过通讯协议交换信息,它们可能是同构或异构系统
例如

  • REST API网关
  • WebServices网关

使用场景

  • 监控(Monitoring)
  • 测试 (Testing)
  • 动态路由(Dynamic Routing)
  • 服务整合(Service Integeation)
  • 负荷减配(Load Shedding)
  • 安全(Security)
  • 静态资源处理(Static Resources handling)
  • 活跃流量管理(Active traffic management)

数据来源

  • 服务发现
  • 服务注册

通讯方式

  • 协议:二进制、文本
  • 方式:同步、异步

qq1

Spring Cloud Zuul

1.增加依赖

<!-- 依赖 Spring Cloud Netflix Zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

2.创建zuul代理应用

package com.segumentfault.spring.cloud.lesson11.zuul.proxy;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * Zuul 代理引导类
 *
 */
@EnableZuulProxy
@SpringCloudApplication
public class ZuulProxyApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulProxyApplication.class, args);
    }

}

3.配置zuul应用
application.properties

## Zuul 代理应用
spring.application.name = zuul-proxy

## 服务端口
server.port = 6060


4.配置zuul路由规则

## 指定 user-service-provider
zuul.routes.user-service-provider = /user-service/**

## 配置 ribbon
user-service-provider.ribbon.listOfServers = http://localhost:9090/

## http://localhost:8080/user-service/* => http://localhost:9090/*

5.配合HTTP客户端
注意:实际配置Ribbon底层HTTP调用客户端,并非zuul独享此功能

  • 默认客户端:HttpClient
    装配类:HttpClientRibbonConfiguration
  • 配置客户端:OkHttpClient
    装配类:OkHttpRibbonConfiguration

激活配置:ribbon.okhttp.enabled=ture

Spring Cloud整合

服务端口信息
端口信息
​ zuul-proxy : 6060
​ config-server : 7070
​ user-service-client: 8080
​ user-service-provider : 9090
​ eureka-server : 10000

服务依赖关系

eureka先启然后user-service-provider然后config-server然后user-service-client然后zuul-proxy

config-server配置zuul-proxy信息

configs/zuul-config.properties

## Zuul Proxy 配置内容

## 指定 user-service-provider
zuul.routes.user-service-provider = /user-service/**
## 指定 user-service-client
zuul.routes.user-service-client = /user-client/**

zuul-proxy作为配置客户端

1.增加config client依赖

 <!-- 依赖 Spring Cloud Config Client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

配置config client信息
bootstrap.properties

## Zuul 代理应用
spring.application.name = zuul-proxy

## 配置客户端应用关联的应用
## spring.cloud.config.name 是可选的
## 如果没有配置,采用 ${spring.application.name}
spring.cloud.config.name = zuul-config
## 关联 profile
spring.cloud.config.profile = default
## 关联 label
spring.cloud.config.label = master
## 激活 Config Server 服务发现
spring.cloud.config.discovery.enabled = true
## Config Server 服务器应用名称
spring.cloud.config.discovery.serviceId = config-server
## Spring Cloud Eureka 客户端 注册到 Eureka 服务器
eureka.client.serviceUrl.defaultZone = http://localhost:10000/eureka

zuul-proxy激活服务发现

增加 eureka Client依赖

 <!-- 依赖 Spring Cloud Netflix Eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

下面知识只做了解


ZuulFilter调用链
qq2
 RequestContext context = RequestContext.getCurrentContext();
            context.setZuulEngineRan();

            try {
                preRoute();
            } catch (ZuulException e) {
                error(e);
                postRoute();
                return;
            }
            try {
                route();
            } catch (ZuulException e) {
                error(e);
                postRoute();
                return;
            }
            try {
                postRoute();
            } catch (ZuulException e) {
                error(e);
                return;
            }

Zuul自动装配

ZuulServletFilter使用范围更大,可以拦截所有的Servlet,包括ZuulServlet
ZuulServlet会有URL匹配的模式,url-pattern
Zuul有两种的激活模式:

  • @EnableZuulProxy
    导入ZuulProxyMarkerConfiguration,随后生成一个ZuulProxyMarkerConfiguration.Marker()Bean,这个Bean作为ZuulProxyAutoConfiguration的装配前置条件。

请注意:ZuulProxyMarkerConfiguration扩展了ZuulServerAutoConfiguration,所以ZuulServlet和ZuulController会被自动装配
ZuulController有DispatcherServlet来在控制,它的映射地址是:“/*”,DispatcherServlet中注册一个ZuulHandlerMapping,它控制映射到ZuulController,可以参考ZuulServerAutoConfiguration:

@Bean
    public ZuulController zuulController() {
        return new ZuulController();
    }

    @Bean
    public ZuulHandlerMapping zuulHandlerMapping(RouteLocator routes) {
        ZuulHandlerMapping mapping = new ZuulHandlerMapping(routes, zuulController());
        mapping.setErrorController(this.errorController);
        return mapping;
    }

通过源码分析,ZuulController 将请求委派给ZuulServlet,所以所有的ZuulFilter 实例都会被执行。

因此,访问 http://localhost:6060/user-service-client/user/find/all ,实际将请求递交给 DispatcherServlet

发送请求"/user-service-client/user/find/all"

  • DispatcherServlet

    • ZuulHandlerMapping

      • ZuulController

        • ZuulServlet

          • RibbonRoutingFilter
  • @EnableZuulServer

导入ZuulServerMarkerConfiguration ,随后生成一个 ZuulServerMarkerConfiguration.Marker() Bean ,主要用作引导装配ZuulServerAutoConfiguration

ZuulServerAutoConfiguration与 父类 ZuulProxyAutoConfiguration 区别:

父类ZuulProxyAutoConfiguration 提供了RibbonRoutingFilter

调用层次:

  • DispatcherServlet

    • ZuulHandlerMapping

      • ZuulController

        • ZuulServlet

          • ZuulFilter

网友评论

登录后评论
0/500
评论
xiaoshuaiv5
+ 关注