SpringCloud-第二章-配置文件

SpringCloud-第二章-配置文件

1.配置文件

在SpringBoot构建完之后,会在resource目录下创建一个application.properties文件,这是一个空文件。SpringBoot应用默认就会读取application.properties文件,但我们可以修改为application.yml,个人建议使用yml,因为YMAL的风格会更加适合配置文件。两者的功能上是一致的,只是语法不同,本文不重点介绍语法。

##2.实战

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

@Configuration
@EnableConfigurationProperties
public class ConditionConfig {

@Bean
public GatewayProperties gatewayProperties() {
return new GatewayProperties();
}
}

@Validated
@ConfigurationProperties(prefix = "spring.cloud.gateway")
public class GatewayProperties {
@NotNull
@Valid
private List<RouteDefinition> routes = new ArrayList<>();

public void setRoutes(List<RouteDefinition> routes) {
this.routes = routes;
}

@PostConstruct
private void init() {
System.out.println(JSON.toJSONString(this));
}
}

@Validated
public class PredicateDefinition {
@NotNull
private String name;

private Map<String, String> args = new LinkedHashMap<>();

public PredicateDefinition(){}
public PredicateDefinition(String text) {
int eqIdx = text.indexOf('=');
if (eqIdx <= 0) {
throw new ValidationException("Unable to parse PredicateDefinition text '"
+ text + "'" + ", must be of the form name=value");
}
setName(text.substring(0, eqIdx));

String[] args = tokenizeToStringArray(text.substring(eqIdx + 1), ",");

for (int i = 0; i < args.length; i++) {
this.args.put(NameUtils.generateName(i), args[i]);
}
}
public void setName(String name) {
this.name = name;
}

public void setArgs(Map<String, String> args) {
this.args = args;
}
}

public class RouteDefinition {
@NotEmpty
private String id;

@NotEmpty
@Valid
private List<PredicateDefinition> predicates = new ArrayList<>();

@NotNull
private URI uri;
private int order = 0;
public RouteDefinition(){}

public RouteDefinition(String text) {
int eqIdx = text.indexOf('=');
if (eqIdx <= 0) {
throw new ValidationException("Unable to parse RouteDefinition text '" + text
+ "'" + ", must be of the form name=value");
}

setId(text.substring(0, eqIdx));

String[] args = tokenizeToStringArray(text.substring(eqIdx + 1), ",");

setUri(URI.create(args[0]));

for (int i = 1; i < args.length; i++) {
this.predicates.add(new PredicateDefinition(args[i]));
}
}
public void setId(String id) {
this.id = id;
}

public void setPredicates(List<PredicateDefinition> predicates) {
this.predicates = predicates;
}

public void setUri(URI uri) {
this.uri = uri;
}

public void setOrder(int order) {
this.order = order;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
gateway:
routes:
- id: yml-id
uri: www.baidu.com
order: -1
predicates:
- name: predicatesName
args:
arg0: 1
arg1: 2
- Path=/echo

以上代码实现用yml文件,映射到类GatewayProperties中。最终的打印结果是

1
{"routes":[{"id":"yml-id","order":-1,"predicates":[{"args":{"arg0":"1","arg1":"2"},"name":"predicatesName"}],"uri":"www.baidu.com"}]}

细心的同学可能会注意到@EnableConfigurationProperties、@ConfigurationProperties(prefix = “spring.cloud.gateway”)、@Validated 这三个很重要的注解。是的,如果想实现配置文件直接映射成Bean,@EnableConfigurationProperties和@ConfigurationProperties是必须要配置的,prefix=”spring.cloud.gateway”代表从spring.cloud.gateway开始采集配置。

@Validated 是校验配置的工具注解,除了代码中应用到的注解外,其它常见注解:

  • @AssertFalse 校验false
  • @AssertTrue 校验true
  • @DecimalMax(value=,inclusive=) 小于等于value,inclusive=true,是小于等于
  • @DecimalMin(value=,inclusive=) 与上类似
  • @Max(value=) 小于等于value
  • @Min(value=) 大于等于value
  • @NotNull 检查Null
  • @Past 检查日期
  • @Pattern(regex=,flag=) 正则
  • @Size(min=, max=) 字符串,集合,map限制大小
  • @Validate 对po实体类进行校验
1
- Path=/echo

这行代码有些不好理解,其实就是RouteDefinition 有两个构造函数,这句配置的意思是调用了有参数的构造函数来定义RouteDefinition对象,使得构建网关的配置更加简洁。

##3.总结

以上的内容,是通过阅读SpringCloudGateway源代码,发现实现方式很优雅,便总结下来。利用好SpringBoot的配置,可以写出很优雅的代码,提高维护性。

评论