spring cloud zuul 实现动态路由
程序员文章站
2022-06-03 19:01:48
...
前期工作
服务提供者的eureka添加配置
# 配置 server.servlet.context-path 必须配置,不可单独为反斜杠 /
server.servlet.context-path=/user/
# 在eurek的metadata-map 中存入 server.servlet.context-path
eureka.instance.metadata-map.context-path=${server.servlet.context-path}
改造zuul 自定义网关路由规则
package cn.zhangfusheng.zuul.server.config;
import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author fusheng.zhang
* @Description 网关动态路由
* @create 2020-04-17 20:04:00
*/
@Slf4j
@Configuration
public class ZuulRoutesConfig {
/**
* 注入 discoveryClient 用于获取注册中心的服务提供者列表
*/
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
ZuulProperties zuulProperties;
@Autowired
ServerProperties serverProperties;
@Bean
public DynamicRoutes dynamicRoutes() {
return new DynamicRoutes(this.serverProperties.getServlet().getContextPath(), this.zuulProperties);
}
/**
* 动态路由
* 路由规则: /${eureka.instance.metadata-map.context-path}/2020-04-20/**
* 目的: 路由一天一变化__未测试
*/
class DynamicRoutes extends SimpleRouteLocator {
public DynamicRoutes(String servletPath, ZuulProperties properties) {
super(servletPath, properties);
}
/**
* 重写 SimpleRouteLocator.locateRoutes()方法
* 自定义路由生成方式
* @return
*/
@Override
protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
Map<String, ZuulProperties.ZuulRoute> routes = new LinkedHashMap<>();
// 获取服务名称列表[serviceId]
discoveryClient.getServices().forEach(serviceId -> {
// 获取服务名称对应的实例
discoveryClient.getInstances(serviceId).forEach(instance -> {
InstanceInfo instanceInfo = ((EurekaServiceInstance) instance).getInstanceInfo();
// 从 eureka.instance.metadata-map中获取context-path,既服务提供者的server.servlet.context-path
// 如果获取不到,则采用服务名称'-'切割的方式作为路由
String key = instanceInfo.getMetadata().getOrDefault(
"context-path", String.format("/%s/", instanceInfo.getSecureVipAddress().split("-")[0]));
// 今天的日期
String localDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// 生成 /${eureka.instance.metadata-map.context-path}/2020-04-20/** 格式的key
key = String.format("%s%s/**", key, localDate);
ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
zuulRoute.setLocation(instanceInfo.getSecureVipAddress());
zuulRoute.setId(instanceInfo.getSecureVipAddress());
zuulRoute.setPath(key);
zuulRoute.setServiceId(instanceInfo.getSecureVipAddress());
routes.put(key, zuulRoute);
});
});
return routes;
}
@Override
public List<Route> getRoutes() {
List<Route> values = new ArrayList<>();
for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : locateRoutes().entrySet()) {
values.add(getRoute(entry.getValue(), entry.getValue().getPath()));
}
return values;
}
}
}
推荐阅读
-
Spring Cloud GateWay 路由转发规则介绍详解
-
Spring Cloud Ribbon实现客户端负载均衡的方法
-
利用Spring Cloud Config结合Bus实现分布式配置中心的步骤
-
详解Spring Cloud Hystrix断路器实现容错和降级
-
spring cloud实现前端跨域问题的解决方案
-
Spring Cloud动态配置实现原理与源码分析
-
Spring Cloud Zuul的重试配置详解
-
spring cloud-zuul的Filter使用详解
-
spring cloud 使用Hystrix 实现断路器进行服务容错保护的方法
-
Spring Cloud zuul自定义统一异常处理实现方法