动态路由的实现方式多种多样,研究一下基于数据方式的动态路由。 1. 创建项目,并pom.xml文件引入如下依赖 project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-
动态路由的实现方式多种多样,研究一下基于数据方式的动态路由。
1. 创建项目,并pom.xml文件引入如下依赖
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion><groupId>com.olive</groupId>
<artifactId>olive-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
</parent><dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies><dependency>
<groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies></project>
使用 spring-boot-starter-paren t就不需要给子集pom配置版本号了,因为它包括了
-
定义了 java 编译版本为 1.8
-
使用 utf-8 格式编码
-
继承 spring-boot-dependencies 进行统一版本依赖管理
-
执行打包 war jar 操作配置;可以省略打包 plugin 的配置
-
自动化资源过滤。如 application.properties 和 application.yml 的资源过滤、 包括 profile 多环境配置的
-
自动化插件配置
-
不需要配置 maven 打包 plugin 插件配置
2. 从数据库加载路由配置
先定义一个接口,该接口的功能主要是返回数据库配置的所有路由
import org.springframework.cloud.gateway.route.RouteDefinition;
import reactor.core.publisher.Flux;
/**
* 返回所有路由数据
*/public interface GatewayRouterService {
Flux<RouteDefinition> getGatewayRoutes();
}
实现该接口
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Service;
import com.olive.router.GatewayRouterService;
import reactor.core.publisher.Flux;
@Service
public class RouterServiceImpl implements GatewayRouterService {
@Override
public Flux<RouteDefinition> getGatewayRoutes() {System.out.println("------getGatewayRoutes-------");
List<RouteDefinition> routes = null;
//TODO查询数据库返回所有有效路由return Flux.fromIterable(routes );
}
}
3. 动态加载路由
实现 RouteDefinitionRepository 接口,Spring自动从数据库中读取路由配置;采用 nacos 作为服务发现与配置中心,nacos 自动触发心跳检测,网关基于心跳检测会自动刷新数据库路由配置,默认 30s 进行一次路由刷新。参考实现 RouteRefreshListener。
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import com.olive.router.GatewayRouterService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class DatabaseRouteDefinitionRepository implements RouteDefinitionRepository {
private final GatewayRouterService gatewayRouterService;
public InDBRouteDefinitionRepository(GatewayRouterService gatewayRouterService) {
this.gatewayRouterService= gatewayRouterService;
}
public Flux<RouteDefinition> getRouteDefinitions() {
return gatewayRouterService.getGatewayRoutes();
}
public Mono<Void> save(Mono<RouteDefinition> route) {//TODO
return Mono.empty();
}
public Mono<Void> delete(Mono<String> routeId) {
//TODO
return Mono.empty();}
}
4. 配置加载自定义的路由
spring-gateway 默认是先从 application.yml 文件加载路由配置;这里通过 AutoConfigureBefore 注解,加载数据库的路由配置。
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import com.olive.route.DatabaseRouteDefinitionRepository;
import com.olive.router.GatewayRouterService;
@AutoConfigureBefore(GatewayAutoConfiguration.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 5)
@Configuration
public class GatewayConfig {
/*** 网关路由配置实现bean
*/
@Bean
@ConditionalOnMissingBean(RouteDefinitionRepository.class)@ConditionalOnBean(GatewayRouterService.class)
public DatabaseRouteDefinitionRepository databaseRouteDefinitionRepository(GatewayRouterService gatewayRouterService) {
return new DatabaseRouteDefinitionRepository(gatewayRouterService);
}}
5. 添加 application.yml 配置文件
需要启动nacos,然后要配置 nacos 注册中心地址。
server:
port: 8089
spring:
application:
name: olive-gateway
cloud:
nacos:
discovery:
6. 编写 springboot 启动引导类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
public class GwApplication {
public static void main(String[] args) {
SpringApplication.run(GwApplication.class, args);
}
}