Gateway2案例

1.新建moudle:zds-egg-gateway
2.pom 不要引入spring-boot-starter-web,gateway使用的是webflux

  1. <dependencies>
  2. <!--gateway-->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-gateway</artifactId>
  6. </dependency>
  7. <!-- nacos -->
  8. <dependency>
  9. <groupId>com.alibaba.cloud</groupId>
  10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.alibaba.cloud</groupId>
  14. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  15. </dependency>
  16. </dependencies>

3.yml 新增网关配置
nacos

  1. #dev
  2. spring:
  3. application:
  4. name: zds-egg-gateway
  5. profiles:
  6. active: dev
  7. main:
  8. allow-bean-definition-overriding: true
  9. cloud:
  10. nacos:
  11. config:
  12. server-addr: 192.168.34.126:8848
  13. file-extension: yaml
  14. namespace: 5fa02c50-78b7-4c9d-a43c-8883874c7c56
  15. group: DEFAULT_GROUP
  16. discovery:
  17. server-addr: 192.168.34.126:8848
  18. file-extension: yaml
  19. namespace: 5fa02c50-78b7-4c9d-a43c-8883874c7c56
  20. group: DEFAULT_GROUP
server:
  port: 9527

spring:
  application:
    name: zds-egg-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.34.126:8848
    #############################新增网关配置###########################
    gateway:
      routes:
        - id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8082  #匹配后提供服务的路由地址
          predicates:
            - Path=/timeout/** # 断言,路径相匹配的进行路由

        - id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8082  #匹配后提供服务的路由地址
          predicates:
            - Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1

4.启动类

@SpringBootApplication
@EnableDiscoveryClient
public class Gateway9527 {
    public static void main(String[] args) {
        SpringApplication.run(Gateway9527.class, args);
    }
}

5.测试
原地址
http://localhost:8082/timeout/10
http://localhost:8082/selectStuById?id=1
新地址
http://localhost:9527/timeout/10
http://localhost:9527/selectStuById?id=1
两者返回结果一致

Gateway配置路由的两种方式

  • 在配置文件yml中配置,见上
  • 代码中注入RouteLocator的Bean

案例:百度国内新闻网址,需要外网 - http://news.baidu.com/guonei
自己写一个:业务需求 - 通过9527网关访问到外网的百度新闻网址

@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("path_route_biz3",
                r-> r.path("/guonei")
                        .uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }
}

测试
浏览器输入http://localhost:9527/guonei,返回http://news.baidu.com/guonei相同的页面。

GateWay配置动态路由

默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能(不写死一个地址)。
启动
zds-egg-gateway 9527
zds-egg-biz 8082/8085
YML
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。

server:
  port: 9527

spring:
  application:
    name: zds-egg-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.34.126:8848
    #############################新增网关配置###########################
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
        - id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8082  #匹配后提供服务的路由地址
          uri: lb://zds-egg-biz
          predicates:
            - Path=/timeout/** # 断言,路径相匹配的进行路由

        - id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
#          uri: http://localhost:8082  #匹配后提供服务的路由地址
          uri: lb://zds-egg-biz
          predicates:
            - Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1

测试
浏览器输入 - http://localhost:9527/timeout/10
结果
不停刷新页面,8082/8085两个端口切换。