【SpringCloud NetFlix】网关Zuul(三)路由配置

简单路由
SimpleHostRoutingFilter
配置连接池:

​ zuul.host.maxTotalConnections:目标主机的最大连接数,默认值为200。配置该项,相当于调用了PoolingHttpClientConnectionManager的setMaxTotal方法。

​ zuul.host.maxPerRouteConnections:每个主机的初始连接数,默认值为20。配置该项,相当于调用了PoolingHttpClientConnectionManager的setDefaultMaxPerRoute方法。

这里写图片描述

在上面的转发过程中会使用一个HttpClient连接池的功能

简单路由配置:

zuul:
  routes:    
    routeTest:
      path: /routeTest/163
      url: http://www.163.com/
    route163:
      url: http://www.163.com/

访问测试:

http://localhost:9000/routeTest/163

http://localhost:9000/route163

跳转路由

​ SendForwardFilter

​ forward

这里写图片描述

配置如下:

zuul:
  routes: 
    helloRoute:
      path: /test/**
      url: forward:/source/hello

访问连接:

http://localhost:9000/test/zlt

Ribbon路由
zuul:
  routes:
    sale:
      path: /sale/**
      serviceId: spring-zuul-sale
    spring-zuul-sale:
      path: /sale/**
    abc:
      path: /sale/**
      url: spring-zuul-sale

上面的配置中,url后面为一个简单的字符串,会被当成一个serviceId来使用

自定义路由规则
  • PatternServiceRouteMapper
  • zuul.ignoredServices
  • zuul.ignoredPatterns
@Configuration
public class MyConfig {

    /**
     * 访问网关的 /sale/**,将会被路由到 spring-zuul-sale 服务进行处理
     */
    @Bean
    public PatternServiceRouteMapper patternServiceRouteMapper() {
        return new PatternServiceRouteMapper(
                "(spring)-(zuul)-(?<module>.+)", "${module}/**");
    }
}

作用相当于如下:

zuul:
  routes:
    sale:
      path: /sale/**
      serviceId: spring-zuul-sale
    spring-zuul-sale:
      path: /sale/**
    abc:
      path: /sale/**
      url: spring-zuul-sale
请求头配置
zuul:
  sensitive-headers: cookie
路由端点
  • Actuator依赖
  • management.security.enabled设置为false

引用依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>

management.security.enabled设置为false

management:
  security:
    enabled: false

启动网关服务,并访问该链接:http://localhost:9000/routes

这里写图片描述
以上为疯狂SpringCloud微服务架构实战学习笔记
感谢杨恩雄老师:https://my.oschina.net/JavaLaw

猜你喜欢

转载自blog.csdn.net/zlt995768025/article/details/81708334