Nacos结合Zuul实现路由动态刷新

Nacos与Zuul的动态路由

Nacos配置中心原理

之前项目用的网关如果需要实现路由的动态刷新的话,往往需要停机更新,这是不允许的。因为是网关服务。肯定不可以随便停机的。

依赖


<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>

启动类加入注解

@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
    
    //......
}

配置 关键的配置类

package com.foodom.platform.gateway.config.nacos;

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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent;
import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

@Slf4j
@Configuration
public class NacosServerConfig {

    @Value("${nacos.serverAddr}")
    private String serverAddr;

    @Value("${nacos.namespace}")
    private String namespace;

    @Value("${nacos.dataId}")
    private String dataId;

    @Value("${nacos.group}")
    private String group;

    //路由信息配置在sso的数据库,所以通过feign获取到路由信息
    private SsoFeignClient ssoFeignClient;
    private CompositeRouteLocator compositeRouteLocator;
    private ZuulProperties zuulProperties;

    @Autowired
    public NacosServerConfig(SsoFeignClient ssoFeignClient, CompositeRouteLocator compositeRouteLocator, ZuulProperties zuulProperties) {
        this.ssoFeignClient = ssoFeignClient;
        this.compositeRouteLocator = compositeRouteLocator;
        this.zuulProperties = zuulProperties;
    }

    @Bean
    public ConfigService configService(){
        try {
            Properties properties = new Properties();
            properties.put("serverAddr",serverAddr);
            //因为在nacos中添加了命名空间来区分不同的配置文件,所以需要添加 namespace 的参数
            properties.put("namespace",namespace);
            ConfigService configService = NacosFactory.createConfigService(properties);

            //初次获取配置文件的内容
            String content = configService.getConfig(dataId, group, 5000);

            configService.addListener(dataId, group, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String s) {

                    log.info("gateway 路由动态刷新");
                    //由于我的网关的业务场景并不是很复杂,所以直接在这里实现路由的动态刷新就好了
                    //动态刷新业务代码
                    log.info("refresh result={}",zuulProperties.getRoutes());


                }
            });

            return configService;
        } catch (NacosException e) {

            log.error("config service error ={}",e);
        }

        return null;

    }
}

nacos 中的配置

Nacos结合Zuul实现路由动态刷新_第1张图片

最后不要忘了引用该 bean

@Autowired
private ConfigService configService;

结果:

Nacos结合Zuul实现路由动态刷新_第2张图片

未完待续…

你可能感兴趣的:(SpringCloud)