我有
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/employee/**")
.uri("http://localhost:8081/")
.id("employeeModule"))
.build()
但是我想拥有一个动态派生的值,而不是http:://localhost:8081/
,例如:@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder,
@Autowired val discoveryService: DiscoveryService) {
return builder.routes()
.route(r -> r.path("/employee/**")
.uri(discoveryService.getHost())
.id("employeeModule"))
这可能吗?如何更改语法以自动连接DiscoveryService?
最佳答案
像这样尝试:
RouteLocatorBuilder.Builder routes = builder.routes();
discoveryClient.getServices().forEach(service->routes.route(r -> r.path("/"+service+"/**")
.uri("lb://"+service).id(service)));
return routes.build();
关于spring - Autowiring 参数作为bean声明参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64589616/