前言
动态路由的概念,就是说通过注册中心获取到服务地址,然后进行路由。有些文章中提到了两种方式:
(1) 一种是通过注册中心下发配置的方式,也就是说路由的配置文件不在application.yml中,而是在配置中心中编写,这种其实和application.yml中编写路由规则是一样的,只不过一个是静态的,一个是动态的。如果你上网搜,nacos和gateway实现动态路由,有很多的文章都是这么写的。实现的基本思路就是这样
路由信息不再配置在配置文件中,将路由信息配置在Nacos的配置中。
在服务网关Spring Cloud Gateway中开启监听,监听Nacos配置文件的修改。
Nacos配置文件一旦发生改变,则Spring Cloud Gateway重新刷新自己的路由信息。
(2) 另一种方式是不通过手动或自动配置,而是直接使用注册中心中的服务名进行动态路由的添加和修改,我的理解就是说,你开发了一个微服务,到注册中心进行了注册,那么网关就可以自动的获取到你的微服务的名称,然后自动的就可以进行路由,而不需要手动的再次编写路由配置。所以可以看到很多文章中,都会提到 uri: lb://message-provider 和 Eureka 注册中心这两个名词。
参考文章:
1.使用Nacos实现Spring Cloud Gateway的动态路由 (第一种配置中心方式)
2.基于Nacos配置中心实现Spring Cloud Gateway的动态路由管理 (第一种配置中心方式)
3.微服务架构基础之注册中心
4.Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单(第一种配置中心方式)
5.基于Nacos实现Spring Cloud Gateway实现动态路由 (第一种配置中心方式)
6.springcloud gateway动态路由实现,mysql存储配置 (将路由信息保存到数据库中,通过数据库动态的增加,修改和删除路由信息)
本文就是主要探讨的就是第二种方式进行动态路由的实现,那么SpringCloud GateWay结合Nacos作为动态路由的步骤:
参考文章:
1.微服务网关Gateway在实战中如何结合注册中心,定义过滤器? (这里使用了nacos和lb的模式,即动态服务名路由的方式,但是里面也有问题,就是增加了一个@EnableFeignClients的注解,根据我的实验,我自始至终都没有用到过这个注解,没有这个注解,其实也可以正常调用微服务的。这篇文章中还有就是在文末的彩蛋,有很多面试和其他技术方面的链接文章,可以顺便学习一下,比如配置tomcat,mysql三高等,作为一个不难么正经的后端,很多东西我都没有学过。)
2.Spring Cloud Gateway、 Nacos Discovery Example (这是官方提供的动态路由的例子)
3.注解 @EnableFeignClients 工作原理
4.Spring Boot 2下使用Feign找不到@EnableFeignClients的解决办法
5.Feign基础入门及特性讲解
1.引入依赖
(1) 父模块引入了spring-boot和spring-cloud-starter-alibaba-nacos-discovery等依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.bibichuan</groupId> <artifactId>SpringBootCould</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>lsmfhx</name>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent>
<modules> <module>bigdata</module> <module>gateway</module> </modules>
<dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
</project>
|
(2) 子模块中就添加了一个网关依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringBootCould</artifactId> <groupId>com.bibichuan</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../</relativePath> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId> <packaging>jar</packaging> <description>能源管理系统网关</description>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties>
<dependencies>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
</dependencies>
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main/app</source> <source>src/main/server</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build>
</project>
|
(3) 其他的子模块也是只引入了nacos服务发现的依赖
2.编写application.yml配置文件
有两种方式配置发现路由
(1) 第一种是直接开启locator.enabled=true,这样可以直接使用服务的名称进行服务的调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server: port: 7000 spring: application: name: ${app-name} cloud: nacos: discovery: server-addr: 127.0.0.1:8848 ip: 127.0.0.1 gateway: discovery: locator: enabled: true
|
(2) 第二种就是直接使用uri指定服务名,lb://{服务名}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server: port: 7000 spring: application: name: ${app-name} cloud: nacos: discovery: server-addr: 127.0.0.1:8848 ip: 127.0.0.1 gateway: routes: - id: url-proxy-1 uri: lb://bigdata predicates: - Path=/nacos/** filters: - StripPrefix=1
|
3.启动类添加注解
1 2 3 4 5 6 7
| @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
4.解决问题重点
(1) 经过测试和分析,nacos中的服务名不能包含下划线,否则就会出现:Invalid host: lb://lsmfhx_bigdata 错误。
根据pom.xml文件显示,我使用的gateway的版本是:spring-cloud-gateway-core-2.1.1.RELEASE.jar:2.1.1.RELEASE。
nacos中的服务名不能包含下划线,但是可以包含中划线。
参考文章:
1.Should we must limit lb:// protocol use host format?
2.serviceComb集成springcloudgateway无法使用微服务id进行路由导航
3.Spring-Cloud-Gateway 源码解析 —— 路由(2.3)之 Java 自定义 RouteLocator