Dubbo使用Apollo作为配置中心

如下示例均基于Spring Boot框架环境实现。

启动Apollo

参考Apollo官方文档部署并启动实例。
在使用Apollo作为配置中心时,可以将Dubbo的所有配置信息都保存到Apollo中。

依赖及配置

在服务提供者或服务消费者项目中添加依赖配置。

1
2
3
4
5
<!-- 使用apollo作为配置中心 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-apollo</artifactId>
</dependency>

同时,在服务提供者或服务消费者项目配置文件(application.yml)中添加如下配置。

1
2
3
4
5
6
7
8
9
app:
id: dubbo-springboot-demo-consumer
apollo:
meta: http://192.168.100.232:8080
bootstrap:
enabled: true
eagerLoad:
enabled: true
namespaces: TEST1.dubbo

在使用Apollo作为配置中心时,不需要在启动类中添加任务注解。
在读取Apollo中的配置参数时可以使用注解@ApolloJsonValue@Value

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
// 在这个Bean中处理apollo统一配置参数
@Component("annotatedBean")
public class AnnotatedBean {

private static final Logger logger = LoggerFactory.getLogger(AnnotatedBean.class);

private int timeout;
private int batch;
private String dubboApplicationName;
private List<JsonBean> jsonBeans;

/**
* ApolloJsonValue annotated on fields example, the default value is specified as empty list - []
* <br /> jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
*/
@ApolloJsonValue("${jsonBeanProperty:[]}")
private List<JsonBean> anotherJsonBeans;

@Value("${batch:100}")
public void setBatch(int batch) {
logger.info("updating batch, old value: {}, new value: {}", this.batch, batch);
System.out.println(String.format("updating batch, old value: %s, new value: %s", this.batch, batch));
this.batch = batch;
}

@Value("${timeout:200}")
public void setTimeout(int timeout) {
logger.info("updating timeout, old value: {}, new value: {}", this.timeout, timeout);
System.out.println(String.format("updating timeout, old value: %s, new value: %s", this.timeout, timeout));
this.timeout = timeout;
}

// 解析用小数点连接的参数
@Value("${dubbo.application.name}")
//@Value("${dubboApplicationName}")
public void setDubboApplicationName(String dubboApplicationName) {
this.dubboApplicationName = dubboApplicationName;
}

public String getDubboApplicationName() {
return dubboApplicationName;
}

/**
* ApolloJsonValue annotated on methods example, the default value is specified as empty list - []
* <br /> jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
*/
@ApolloJsonValue("${jsonBeanProperty:[]}")
public void setJsonBeans(List<JsonBean> jsonBeans) {
logger.info("updating json beans, old value: {}, new value: {}", this.jsonBeans, jsonBeans);
System.out.println(String.format("updating json beans, old value: %s, new value: %s", this.jsonBeans, jsonBeans));
this.jsonBeans = jsonBeans;
}

public int getTimeout() {
return timeout;
}

public int getBatch() {
return batch;
}

@Override
public String toString() {
return String.format("[AnnotatedBean] timeout: %d, batch: %d, jsonBeans: %s", timeout, batch,
jsonBeans);
}

private static class JsonBean {
private String someString;
private int someInt;

@Override
public String toString() {
return "JsonBean{" +
"someString='" + someString + '\'' +
", someInt=" + someInt +
'}';
}
}
}

在使用Apollo作为Dubbo服务的注册中心时,网上的一些博客是错误的,参照其配置参数和步骤无法生效。

参考如下配置不成功:

https://juejin.cn/post/6844903872746487821 Dubbo-SpringBoot使用Apollo配置中心
https://blog.csdn.net/dbqb007/article/details/88617752 Dubbo使用Apollo作为配置中心实战

参考如下配置成功:
https://github.com/apolloconfig/apollo-demo-java

【参考】
apollo
Apollo文档
Apollo安装部署
Spring Boot 集成 Apollo 配置中心,真香、真强大