Search in sources :

Example 1 with RouteEntity

use of com.simple4h.gateway.entity.RouteEntity in project simple4h-study by simplecxm.

the class NacosDynamicRoutingConfig method refreshRouting.

@Bean
public void refreshRouting() throws NacosException {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, nacosServer);
    // properties.put(PropertyKeyConst.NAMESPACE, "8282c713-da90-486a-8438-2a5a212ef44f");
    ConfigService configService = NacosFactory.createConfigService(properties);
    configService.addListener(DATA_ID, GROUP, new Listener() {

        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            log.info(configInfo);
            boolean refreshGatewayRoute = JSONObject.parseObject(configInfo).getBoolean("refreshGatewayRoute");
            if (refreshGatewayRoute) {
                List<RouteEntity> list = JSON.parseArray(JSONObject.parseObject(configInfo).getString("routeList")).toJavaList(RouteEntity.class);
                for (RouteEntity route : list) {
                    update(assembleRouteDefinition(route));
                }
            } else {
                log.info("路由未发生变更");
            }
        }
    });
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) List(java.util.List) Properties(java.util.Properties) RouteEntity(com.simple4h.gateway.entity.RouteEntity) Bean(org.springframework.context.annotation.Bean)

Aggregations

ConfigService (com.alibaba.nacos.api.config.ConfigService)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 RouteEntity (com.simple4h.gateway.entity.RouteEntity)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 Executor (java.util.concurrent.Executor)1 Bean (org.springframework.context.annotation.Bean)1