gateway 集成 nacos 实现动态路由,前几篇单独分析gateway网关时,仅介绍了静态路由的原理和使用,今天我们先分析动态路由,然后介绍一下两者的区别和优缺点。
一、动态路由实战分析:
1、构建一个springboot 服务,去掉web相关的jar:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
加上:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-webflux</artifactId>
- </dependency>
引入相关核心jar
- artifactId>common-gateway</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>common-gateway</name>
- <description>网关服务</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
-
- <!-- springboot web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-webflux</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-loadbalancer</artifactId>
- </dependency>
-
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-core</artifactId>
- </dependency>
- <dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- </dependency>
-
- </dependencies>
2、项目中yaml配置:
- spring:
- main:
- web-application-type: reactive
- application:
- name: @project.artifactId@
- version: @project.version@
- cloud:
- nacos:
- config:
- namespace: common
- file-extension: yaml
- shared-configs:
- - data-id: common-shared-config.yaml
- group: common
- refresh: true
- loggingRoot: logs
3、idea中nacos环境配置: