spring-gateway基于数据库 + nacos 的动态路由

简介: spring-gateway基于数据库 + nacos 的动态路由

动态路由的实现方式多种多样,研究一下基于数据方式的动态路由。

 

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;
    }
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
return gatewayRouterService.getGatewayRoutes();
    }
@Override
public Mono<Void> save(Mono<RouteDefinition> route) {
//TODO
return Mono.empty();
    }
@Override
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:
server-addr: 192.168.255.10:8848

 

6. 编写 springboot  启动引导类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class GwApplication {
public static void main(String[] args) {
   SpringApplication.run(GwApplication.class, args);
 }
}
相关文章
|
14天前
|
Cloud Native Java Nacos
微服务时代的新宠儿!Spring Cloud Nacos实战指南,带你玩转服务发现与配置管理,拥抱云原生潮流!
【8月更文挑战第29天】Spring Cloud Nacos作为微服务架构中的新兴之星,凭借其轻量、高效的特点,迅速成为服务发现、配置管理和治理的首选方案。Nacos(命名和配置服务)由阿里巴巴开源,为云原生应用提供了动态服务发现及配置管理等功能,简化了服务间的调用与依赖管理。本文将指导你通过五个步骤在Spring Boot项目中集成Nacos,实现服务注册、发现及配置动态管理,从而轻松搭建出高效的微服务环境。
77 0
|
2月前
|
SQL Java 数据库
使用Spring Boot和Flyway进行数据库迁移
使用Spring Boot和Flyway进行数据库迁移
|
2月前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
16天前
|
Cloud Native Java Nacos
Spring Cloud Config、Apollo、Nacos和Archaius对比
这篇文章对比了Spring Cloud Config、Apollo、Nacos和Archaius这四种配置中心的适应场景、优缺点。文中讨论了它们的功能特点,例如Spring Cloud Config的集中化配置管理和动态刷新能力,Apollo的实时配置推送和权限治理,Nacos的服务发现和管理功能,以及Archaius的动态配置更新能力。文章指出选择配置中心应根据项目需求和架构来决定,并提供了一个对比图来帮助读者更直观地理解这些工具的差异。
31 1
Spring Cloud Config、Apollo、Nacos和Archaius对比
|
27天前
|
SQL 数据库
Spring5入门到实战------13、使用JdbcTemplate操作数据库(批量增删改)。具体代码+讲解 【下篇】
这篇文章是Spring5框架的实战教程,深入讲解了如何使用JdbcTemplate进行数据库的批量操作,包括批量添加、批量修改和批量删除的具体代码实现和测试过程,并通过完整的项目案例展示了如何在实际开发中应用这些技术。
Spring5入门到实战------13、使用JdbcTemplate操作数据库(批量增删改)。具体代码+讲解 【下篇】
|
25天前
|
JSON Nacos 开发工具
微服务通过nacos实现动态路由
微服务通过nacos实现动态路由
43 7
|
26天前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
27天前
|
XML 数据库 数据格式
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
这篇文章是Spring5框架的实战教程的终结篇,介绍了如何使用注解而非XML配置文件来实现JdbcTemplate的数据库操作,包括增删改查和批量操作,通过创建配置类来注入数据库连接池和JdbcTemplate对象,并展示了完全注解开发形式的项目结构和代码实现。
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
|
27天前
|
SQL XML Java
Spring5入门到实战------12、使用JdbcTemplate操作数据库(增删改查)。具体代码+讲解 【上篇】
这篇文章是Spring5框架的实战教程,详细讲解了如何使用JdbcTemplate进行数据库的增删改查操作,包括在项目中引入依赖、配置数据库连接池、创建实体类、定义DAO接口及其实现,并提供了具体的代码示例和测试结果,最后还提供了完整的XML配置文件和测试代码。
Spring5入门到实战------12、使用JdbcTemplate操作数据库(增删改查)。具体代码+讲解 【上篇】
|
2月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
266 15