我当前的application.yaml具有-
spring:
application:
name: app-gateway
cloud:
gateway:
routes:
- id: app-1
uri: ${services.app1.host}/pages/
predicates:
- Path=/pages/**
- id: app-2
uri: ${services.app1.host}/content/
predicates:
- Path=/content/**
services:
app1:
host: http://app1.hostname:8080
app2:
host: http://app2.hostname:8080
在我的应用中,我需要获取网关路由uri的“评估”值
如果我使用Environment
读取所有值并遍历所有属性,则会获得带有${services.app1.host}
等嵌入式变量的uri。
而且,我不想为每个路线/ uri组合都使用@Value
。
如何获得spring.cloud.gateway.routes
下所有已评估值的映射?
您可以在包含映射的属性值的新类上使用@ConfigurationProperties
:
@ConfigurationProperties(prefix = "spring.cloud.gateway")
public class RouteProperties {
private List<RoutePropertyEntry> routes;
//Getter and setter for routes
}
使用POJO映射每个路线条目:
public class RoutePropertyEntry {
private String id;
private String uri;
private List<String> predicates;
//Getters and setters...
}
并像这样检索属性值:
@Component
@EnableConfigurationProperties(RouteProperties.class)
public class SomeLogicClass {
private final RouteProperties routeProperties;
//Your logic here
}