最近开始微服务的正式学习,网上找了一圈决定用开源项目passcloud作为学习参考,万分感谢~
这个系列教程的目的是完全梳理paascloud项目架构和功能,并不是从零开始学习微服务,所有内容均建立在阅读者已拥有微服务基础知识储备并具备基本开发能力。下面开始正式学习。
paascloud-discovery 配置中心
配置中心的目的是为了集中管理分布式服务的配置文件。
随着服务的数目越来越多,不同的配置文件也越来越繁杂,如果不能统一管理这些文件将会是运维人员的噩梦,哈哈。
spring cloud config组件支持将配置文件在本地管理,也支持用git或svn方式在远程仓库中管理,在实际生产中第二种方式更为常用,在paascloud中也使用了git方式。
pom: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<!--关键依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--服务客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--关键依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--安全相关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-security</artifactId>
</dependency>
<!--监控客户端 spring-boot-admin组件可以在服务端监控整个项目的运行情况-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>
<!--利用mq代理的消息总线,用于通知刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
java:用注释EnableConfigServer标识该类为配置中心1
2
3
4
5
6
7
8
9
10
11
12
13
14@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class PaasCloudDiscoveryApplication {
true/**
* The entry point of application.
*
* @param args the input arguments
*/
truepublic static void main(String[] args) {
truetrueSpringApplication.run(PaasCloudDiscoveryApplication.class, args);
true}
}
可以看到有两个主要的资源配置文件:bootstrap.yml、application.yml。
bootstrap.yml: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#git配置
spring:
cloud:
config:
server:
git: #配置文件地址
uri: https://github.com/paascloud/paascloud-config-repo.git
username: #如果为公共仓库就为空,私有仓库需要用户名和密码
password:
search-paths: /* #路径
default-label: ${spring.profiles.active} #指定配置环境
basedir: /data/config/paascloud-config-repo #git、svn相关系统后台文件存放地址,指定到非系统盘目录下都行(放到系统盘下有被当做垃圾清理的危险)
inetutils: #上一节讲过了
ignoredInterfaces:
- docker0
- veth.*
preferredNetworks:
- 192.168
#对配置内容进行加密
encrypt:
key-store:
location: classpath:/server.jks #指定密钥地址
password: paascloud-storepass #密钥密码
alias: paascloud-key-store #密钥别名
secret: paascloud-keypass #秘密保护密钥(默认为密码相同)
eureka:
client:
serviceUrl: #指定服务注册中心地址
defaultZone: http://root:root@paascloud-eureka:8761/eureka/
instance: #设置实例id会在eureka界面下的status中显示
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
application.yml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22server:
port: 8080
spring:
profiles:
active: dev
application:
name: @pom.artifactId@
rabbitmq: #唯一需要说一下的,需要本地已经装了rabbitmq并启动
host: paascloud-mq-rabbit
port: 5672
username: guest
password: guest
boot:
admin:
username: admin #监控默认密码
password: admin #监控默认密码
security:
user:
name: admin #界面默认密码
password: admin #界面默认密码
logback-spring.xml 日志配置文件 没什么好说的
其他服务想要获取自己的配置文件需要如下步骤:
引入依赖
1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>加入配置
1
2
3
4
5
6
7
8
9
10
11spring:
cloud:
config:
fail-fast: true
discovery:
service-id: paascloud-discovery #配置中的服务名,一定要正确!
enabled: true
label: ${spring.profiles.active}
profile: ${spring.profiles.active}
username: admin
password: admin
这样就能在服务启动时去配置中心获取自己的配置文件了。
通过配置中心,可以将配置文件从远端仓库加载到项目中来,方便实用,但是有一个问题,如果配置文件变了怎么办?通常情况下需要重启配置中心来加载配置文件,但是如果我们通过消息总线来通知各个服务动态刷新配置文件就能实现配置文件的“热加载”!具体如何操作会穿插在下节的讲解中。
本节到此结束,下一节我们来分析paascloud的监控中心monitor,感谢阅读!