- A+
这两天搭了下Spring Cloud Gateway,在配置文件里配置路由,可以成功转发。但是现阶段Spring Cloud Gateway用配置文件的方式不支持动态路由,而网关作为所有访问的入口,必须保证高可用,动态路由能让网关服务不重启动态改变路由。实现方法比较简单,但是中间也踩了一些坑。
1.增加一个类,代码如下:
package com.zp.gateway; 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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.concurrent.Executor; @Component public class NacosDynamicRouteService implements ApplicationEventPublisherAware { private String dataId = "gateway-router"; private String group = "test2"; @Value("${spring.cloud.nacos.config.server-addr}") private String serverAddr; @Autowired private RouteDefinitionWriter routeDefinitionWriter; private ApplicationEventPublisher applicationEventPublisher; private static final List<String> ROUTE_LIST = new ArrayList<>(); @PostConstruct public void dynamicRouteByNacosListener() { try { ConfigService configService = NacosFactory.createConfigService(serverAddr); String config = configService.getConfig(dataId, group, 5000); System.out.println(config); configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { clearRoute(); try { List<RouteDefinition> gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class); for (RouteDefinition routeDefinition : gatewayRouteDefinitions) { addRoute(routeDefinition); } publish(); } catch (Exception e) { e.printStackTrace(); } } @Override public Executor getExecutor() { return null; } }); } catch (NacosException e) { e.printStackTrace(); } } private void clearRoute() { for(String id : ROUTE_LIST) { this.routeDefinitionWriter.delete(Mono.just(id)).subscribe(); } ROUTE_LIST.clear(); } private void addRoute(RouteDefinition definition) { try { routeDefinitionWriter.save(Mono.just(definition)).subscribe(); ROUTE_LIST.add(definition.getId()); } catch (Exception e) { e.printStackTrace(); } } private void publish() { this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter)); } @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } }
2.将原本配置文件里的静态路由删掉。
3.在nacos的管理界面里新增一个配置,DataId和Group和上面代码里保持 一致,分别为gateway-router和test2。
配置格式选为json,内容为
[{ "filters":[{ "args":{ "parts": "1" }, "name": "StripPrefix" }], "id": "provider12", "order": 0, "predicates":[{ "args":{ "pattern": "/consumer115/**" }, "name": "Path" }], "uri": "lb://consumer" }]
注意:这里的filters里的写法请注意:name为属性名,parts为属性值,这个写法等价于配置文件里的StripPrefix=1
而StripPrefix=1的意思是,将匹配的前缀去掉。上面的json配置中,当请求为/consumer115/name时,将转发给服务名为consumer的/name接口
如果不加StripPrefix=1,则会转发给consumer的/consumer115/name接口