开发工具:IntelliJ Idea
JDK 1.8
Spring boot 2.3.12.RELEASE
spring cloud Alibaba 2.2.7.RELEASE
Nacos 2.0.4
spring cloud Hoxton.SR12
点击进入【基于Nacos的微服务】
程序源码
<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.0modelVersion>
<groupId>com.jwsswgroupId>
<artifactId>microservice-gatewayartifactId>
<version>1.0.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.12.RELEASEversion>
<relativePath/>
parent>
<properties>
<java.version>17java.version>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<spring.cloud.version>Hoxton.SR12spring.cloud.version>
<alibaba.cloud.version>2.2.7.RELEASEalibaba.cloud.version>
<fastjson.version>2.0.4fastjson.version>
<lombok.version>1.18.24lombok.version>
<hutool.version>5.8.1hutool.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${alibaba.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>${hutool.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
有关nacos的配置,可根据自己的实际情况进行调整配置。
config标签中的内容,可自行配置
server:
port: 8000
# spring 配置
spring:
application:
name: gateway-server
cloud:
# nacos客户端配置
nacos:
# nacos服务注册与发现
discovery:
server-addr: 127.0.0.1:8844,127.0.0.1:8846,127.0.0.1:8848
# nacos服务配置
config:
server-addr: 127.0.0.1:8844,127.0.0.1:8846,127.0.0.1:8848
namespace: c6340cf4-c9af-4a4e-b8c3-526e708c56b2
group: GATEWAY_GROUP
file-extension: yaml
# 网关配置
gateway:
discovery:
locator:
# 服务名小写
lower-case-service-id: true
# 配置
config:
# 动态
dynamic-route:
# nacos 配置dataId
dataId: gateway-routes
# 分组
group: ${spring.cloud.nacos.config.group}
# nacos服务地址
server-addr: ${spring.cloud.nacos.config.server-addr}
# 命名空间
namespace: ${spring.cloud.nacos.config.namespace}
package com.jwssw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 类描述:网关启动类
*
* @version 1.0
* @date 2022/5/19 15:35
* @slogan 设计就是代码,代码就是设计
* @since JDK 8
*/
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
package com.jwssw.gateway.config;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* 类描述:通过Nacos的配置动态更新网管路由
*
* ApplicationEventPublisherAware 是由 Spring 提供的用于为 Service 注入 ApplicationEventPublisher 事件发布器的接口,使用这个接口,
* 我们自己的 Service 就拥有了发布事件的能力。用户注册后,不再是显示地调用其他的业务 Service,而是发布一个用户注册事件。
*
*
* @version 1.0
* @date 2022/5/26 11:00
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
* @since JDK 8
*/
@Slf4j
@RefreshScope
@Component
public class DynamicRouteConfig implements ApplicationEventPublisherAware {
/** 常量 */
private static final String PROPERTIES_SERVER_ADDR = "serverAddr";
private static final String PROPERTIES_NAMESPACE = "namespace";
private static final String PROPERTIES_GROUP = "group";
/** nacos 配置dataId */
@Value("${config.dynamic-route.dataId:gateway-routes}")
private String dataId = "gateway-routes";
/** nacos 配置group */
@Value("${config.dynamic-route.group:GATEWAY_GROUP}")
private String group = "GATEWAY_GROUP";
/** nacos 配置地址 */
@Value("${config.dynamic-route.server-addr}")
private String serverAddr;
/** nacos 命名空间 */
@Value("${config.dynamic-route.namespace}")
private String namespace;
/** 已加载的路由id集合 */
private static final List<String> ROUTE_LIST = new ArrayList<>();
private final RouteDefinitionWriter routeDefinitionWriter;
/** 事件发布器 */
private ApplicationEventPublisher applicationEventPublisher;
/**
* 方法描述: 构造函数
*
* @param routeDefinitionWriter 路由定义写对象
* @date 2022/5/26 15:25
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
*/
public DynamicRouteConfig(RouteDefinitionWriter routeDefinitionWriter) {
this.routeDefinitionWriter = routeDefinitionWriter;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
/**
* 方法描述: 从Nacos的配置中加载动态路由
*
* @date 2022/5/26 15:11
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
*/
@PostConstruct
public void loadRouteFromNacosAndListener() {
try {
ConfigService configService = NacosFactory.createConfigService(getProperties());
// 程序启动时调用Nacos的配置进行路由加载
String initConfigInfo = configService.getConfig(dataId, group, 5000);
addRouteAndPublish(initConfigInfo);
// 添加监听路由变化
addListener(configService);
} catch (NacosException e) {
log.error("加载路由配置错误,详情:", e);
}
}
/**
* 方法描述: 添加监听
*
* @param cs 配置服务对象
* @date 2022/5/26 15:24
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
*/
private void addListener(ConfigService cs) throws NacosException {
// 添加监听
cs.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
// 将监听到的路由加载到路由定义器中
addRouteAndPublish(configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
});
}
/**
* 方法描述: Nacos配置属性
*
* @return {@link Properties}
* @date 2022/5/26 15:23
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
*/
private Properties getProperties() {
Assert.notBlank(serverAddr, "Nacos的服务地址为空了!");
Properties properties = new Properties();
properties.put(PROPERTIES_SERVER_ADDR, serverAddr);
if (StrUtil.isNotBlank(namespace)) {
properties.put(PROPERTIES_NAMESPACE, namespace);
}
if (StrUtil.isNotBlank(group)) {
properties.put(PROPERTIES_GROUP, group);
}
return properties;
}
/**
* 添加并发布配置的路由
*
* @param configInfo 路由配置字符串;格式:JSON数组
*/
private void addRouteAndPublish(String configInfo) {
// 加载前需要清空有存在的路由
clearRoute();
// 解析从Nacos配置中读取的路由配置信息
List<RouteDefinition> gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class);
for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
// 将路由写到定义器中
routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
// 将路由id加入内存集合中
ROUTE_LIST.add(routeDefinition.getId());
}
// 刷新路由定义器
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
}
/**
* 方法描述: 清空已存在的路由
*
* @date 2022/5/26 15:22
* @slogan 设计就是代码,代码就是设计;创建好的软件是一项学习和思考的活动。
*/
private void clearRoute() {
ROUTE_LIST.forEach(id -> this.routeDefinitionWriter.delete(Mono.just(id)).subscribe());
ROUTE_LIST.clear();
}
}
Gateway+Nacos动态路由
Gateway + Nacos 实现动态路由