zuul+apollo实现zuul网关动态路由

@Component
@Configuration
public class DynamicRouteConfig implements ApplicationContextAware {

    private static final Log log = LogFactory.getLog(DynamicRouteConfig.class);

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private RouteLocator routeLocator;
    @Autowired
    private ZuulProperties properties;

    /**
     * Description @ApolloConfigChangeListener配置改变监听器,触发时将会指向configChange()方法
     * 获取到所有改变的配置中心的key
     * value = "zuul-config" 为apollo中namespace名称
     *
     * @date 2023/5/6 17:22
     * @param: changeEvent
     * @return: void
     */
    @ApolloConfigChangeListener(value = "zuul-config.yaml")
    private void configChange(ConfigChangeEvent changeEvent) {
        // 获取路由
        Map<String, ZuulProperties.ZuulRoute> routes = properties.getRoutes();
        boolean flat = false;
        for (String key : changeEvent.changedKeys()) {
            // 事件内容判断
            if (key.startsWith("zuul.") || key.contains("ribbon.listOfServers")) {
                // 路由是否需要刷新的标识
                flat = true;
                // 根据路由改变类型判断路由是否需要删除
                PropertyChangeType changeType = changeEvent.getChange(key).getChangeType();
                // 删除类的改变类型
                if (PropertyChangeType.DELETED.equals(changeType)) {
                    String[] split = key.split("\\.");
                    String deleteTarget = split[2];
                    Set<String> needDeleteKeys = routes.keySet();
                    if (needDeleteKeys.contains(deleteTarget)) {
                        routes.remove(deleteTarget);
                    }
                }
            }
        }
        // 重新设置路由
        properties.setRoutes(routes);
        // zuul路由刷新
        if (flat) {
            this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
            this.applicationContext.publishEvent(new RoutesRefreshedEvent(this.routeLocator));
        }
    }

    /**
     * Description 路由更新操作
     *
     * @date 2023/5/8 14:06
     * @param: routeEntries
     * @return: void
     */
    private void addConfiguredRoutes(List<ZuulProperties.ZuulRoute> routeEntries) {
        // key为yaml中定义的信息
        Map<String, ZuulProperties.ZuulRoute> routes = this.properties.getRoutes();
        for (ZuulProperties.ZuulRoute routeEntry : routeEntries) {
            String route = routeEntry.getPath();
            if (routes.containsKey(route)) {
                log.warn("Overwriting route " + route + ": already defined by "
                        + routes.get(route));
            }
            routes.put(route, routeEntry);
        }
        //新路由集合重新设置
        this.properties.setRoutes(routes);
    }

    /**
     * Description 获取web上下文
     *
     * @date 2023/5/6 17:21
     * @param: applicationContext
     * @return: void
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * Description 从path中提取ZuulRoute的id
     *
     * @date 2023/5/8 14:30
     * @param: path
     * @return: java.lang.String
     */
    private String extractId(String path) {
        path = path.startsWith("/") ? path.substring(1) : path;
        path = path.replace("/*", "").replace("*", "");
        return path;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Zuul中使用WebSocket和SockJS,您需要进行以下配置: 1. 添加依赖项 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 2. 配置Zuul路由 ```yml zuul: routes: websocket: path: /websocket/** url: ws://localhost:8081 ``` 这将把所有以“/websocket”开头的请求路由到WebSocket服务器上。 3. 配置SockJS ```java @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); } } ``` 这将配置一个SockJS端点,它将处理所有以“/websocket”开头的请求,并使用简单的代理模式将消息转发到“/topic”目的地。 4. 启用Zuul ```java @SpringBootApplication @EnableZuulProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这将启用Zuul代理,并将它们路由到相应的WebSocket服务器和SockJS端点。 现在,您应该可以在Zuul中使用WebSocket和SockJS了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值