持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第36天,点击查看活动详情。
将参数写在配置文件内是很普遍,这里举例说明yaml
类型配置文件List
和Map
类型参数的配置和注入方法。
1.Gateway
1.1 查看源码
最先是从jar
包内的spring.factories
查看自动加载的配置:
参数对象类:
@ConfigurationProperties(GatewayProperties.PREFIX)
@Validated
public class GatewayProperties {
public static final String PREFIX = "spring.cloud.gateway";
private final Log logger = LogFactory.getLog(getClass());
@NotNull
@Valid
private List<RouteDefinition> routes = new ArrayList<>();
private List<FilterDefinition> defaultFilters = new ArrayList<>();
private List<MediaType> streamingMediaTypes = Arrays.asList(MediaType.TEXT_EVENT_STREAM,
MediaType.APPLICATION_STREAM_JSON);
private boolean failOnRouteDefinitionError = true;
}
routes
也就是List
对象类:
@Validated
public class RouteDefinition {
private String id;
@NotEmpty
@Valid
private List<PredicateDefinition> predicates = new ArrayList<>();
@Valid
private List<FilterDefinition> filters = new ArrayList<>();
@NotNull
private URI uri;
private Map<String, Object> metadata = new HashMap<>();
private int order = 0;
}
yaml
里的配置:
spring:
cloud:
gateway:
routes:
- id: gateway-service-1
uri: https://www.baidu.com
predicates:
- Path=/searchBaidu/**
filters:
- CacheRequestFilter
- ValidateCodeFilter
- StripPrefix=1
- /authmxl/uklogin
- id: gateway-service-2
uri: https://www.google.com
predicates:
- Path=/searchGoogle/**
filters:
- CacheRequestFilter
- ValidateCodeFilter
- StripPrefix=1
- /authmxl/uklogin
1.2 效仿一下
配置类:
@Data
@Component
@ConfigurationProperties(TranslateConfiguration.PREFIX)
public class TranslateConfiguration {
public static final String PREFIX = "translate";
private List<TranslateConfig> config= new ArrayList<>();
@Data
public static class TranslateConfig {
private String type;
private int open;
private String fromUrl;
private String fromPort;
private String toUrl;
private String toPort;
}
}
yaml
参数:
translate:
config:
- type: jafka-jafka
open: 1
fromUrl: 192.168.0.1
fromPort: 9092
toUrl: 192.168.0.2
toPort: 9092
- type: kafka-jafka
open: 0
fromUrl: 192.168.0.2
fromPort: 9092
toUrl: 192.168.0.1
toPort: 9092
2.DynamicDataSource
2.1 查看源码
// 这里只贴出 datasource 也就是 Map 对象
public class DynamicDataSourceProperties {
private Map<String, DataSourceProperty> datasource;
}
// Map 里的 Value 对象
public class DataSourceProperty {
private String driverClassName;
private String url;
private String username;
private String password;
yaml
配置:
datasource:
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
greenplum:
driver-class-name: com.pivotal.jdbc.GreenplumDriver
url: jdbc:pivotal:greenplum://localhost:5432;DatabaseName=test
username: root
password: root
2.2 效仿一下
这个跟上边的配置是一样的,Value 对象没有进行封装:
@Data
@Component
@ConfigurationProperties(prefix = "translate")
public class TranslateConfiguration {
/**
* 转换配置
*/
private Map<String, Object> config;
}
yaml
配置:
translate:
config:
translateJ2J:
type: jafka-jafka
open: 1
fromUrl: 192.168.0.207
fromPort: 9092
toUrl: 192.168.0.207
toPort: 9092
translateK2J:
type: kafka-jafka
open: 0
fromUrl: 192.168.0.207
fromPort: 9092
toUrl: 192.168.0.207
toPort: 9092
3.总结
- 两种方式都能够实现类似的配置,List和Map都可以存放封装对象,而Map多出来一个Key,可以存额外的信息。
- 注意前缀及字段的对应关系。