SpringCloud config分布式配置中心
springCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中化的外部配置。就是相当于把每个工程中相同的配置都放在配置中心,比如四个微服务的yml文件中都需要连接同一个mysql数据库那么就能够将mysql的配置提取出来放到一个公共的地方让这四个微服务进行调用,四个微服务私有的配置还是需要在各自的微服务项目中写,这个就不能提取到公共啦。
springCloud Config分为服务端和客户端两部分。 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对境配置进行版管理,并且可以通过git客户端工具来方便的管理和访问配置内容 springcloud config的作用: 1.集中管理配置文件 2.不同环境不同配置,动态化的配置更新,分环境部署 3.运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息 4.当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置 5.将配置信息以REST接口的形式暴露(post、curl访问都可以)
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式) https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/ 官网地址
在自己的github上新建一个仓库比如说 这里的文件起名是有说法的不能随便起名字,如果随便起名字有可能无法访问到该文件。 下面是新建一个SpringCloud config的项目来测试是否能够访问到github中的文件以及是否能够成功的读取文件中的内容。 服务端项目,就是所谓的提供者 图中的Config Server 其余微服务不会再直接访问github而是通过访问服务端获取github上的配置文件信息,以达到共用公共配置文件的作用 新建项目 pom.xml文件
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.at.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>yml文件
server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: uri: https://github.com/xxxx/sprincloud-config.git searchPaths: sssss username: xxxxx # 以及相应的账户名 password: xxxx default-label: main #访问时的uri地址 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka启动类
@SpringBootApplication //Config注解 @EnableConfigServer //注册中心注解 @EnableEurekaClient public class ConfigCenterMain { public static void main(String[] args) { SpringApplication.run(ConfigCenterMain.class,args); } }新建完成后启动项目,访问地址如下: 比如我们想访问我们新建的github上的application.yml文件那么因为我们在配置文件的文件在main分支中所以访问的路径应该是 http://127.0.0.1:3344/main/application.yml 如果我们想要访问的文件是另一个abc-dev.yml访问地址: http://127.0.0.1:3344/main/abc-dev.yml
文件命名规则如下
config-3344.com这个是在hosts中配置上。不配置直接替换为ip即可 第一种: 分支关键词/文件名称 /{label}/{application}-{profile}.yml(最推荐使用这种方式) master分支:
http://config-3344.com:3344/master/config-dev.ymlhttp://config-3344.com:3344/master/config-test.ymlhttp://config-3344.com:3344/master/config-prod.ymldev分支: 1.http://config-3344.com:3344/dev/config-dev.yml 2.http://config-3344.com:3344/dev/config-test.yml 3.http://config-3344.com:3344/dev/config-prod.yml
main分支: 1.http://config-3344.com:3344/main/config-dev.yml 2.http://config-3344.com:3344/main/config-test.yml 3.http://config-3344.com:3344/main/config-prod.yml
第二种:不在路径中写分支关键词 /文件名称 /{application}-{profile}.yml 1.http://config-3344.com:3344/config-dev.yml 2.http://config-3344.com:3344/config-test.yml 3.http://config-3344.com:3344/config-prod.yml 4.http://config-3344.com:3344/config-xxxx.yml(不存在的配置)
第三种: /{application}-{profile}[/{label}] http://config-3344.com:3344/config/dev/master http://config-3344.com:3344/config/dev/mian http://config-3344.com:3344/config/dev/dev
新建客户端项目就是消费者,只要是用到公共配置文件的微服务项目都是客户端
pom.xml文件
<dependencies> <!--只有客户端才添加此配置,服务端添加带有server的配置--> <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>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--需要引入该jar才能使bootstrap配置文件生效--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> <version>2.2.2.RELEASE</version> </dependency> </dependencies>bootstrap.yml文件
server: port: 3355 spring: application: name: cloud-client cloud: config: label: main name: config profile: dev uri: http://localhost:3344/ #rabbitmq配置 # rabbitmq: # host: localhost # port: 5672 # username: guest # password: guest eureka: client: #表示是否将自己注册进EurekaServer默认为true register-with-eureka: true #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetch-registry: true service-url: #集群版 #defaultZone: http://eureka7001.com:7001/eureka/ #单机版 defaultZone: http://eureka7001.com:7001/eureka/ instance: instance-id: configclient prefer-ip-address: true #访问路径可以显示ip #Eureka客户端向服务端发送心跳的实际间隔,单位为秒(默认为30秒) lease-renewal-interval-in-seconds: 1 #Eureka服务端收到最后一次心跳后等待时间上线,单位为秒(默认为90秒) 超时将剔除服务 lease-expiration-duration-in-seconds: 2 # 暴露监控端点用于手动刷新客户端不用再重启项目 management: endpoints: web: exposure: include: "*"启动类
@SpringBootApplication @EnableEurekaClient public class ConfigClientMain { public static void main(String[] args) { SpringApplication.run( ConfigClientMain.class,args); } }测试是否能够访问服务端来获取到github上的文件内容 @RefreshScope是手动刷新时的注解
@RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }如果在项目已经启动后想要再修改github上的配置文件,如果不加手动刷新那么客户端就只能通过重启才能获取到更新后到数据,所以这里需要添加手动刷新客户端项目 手动刷新到步骤如下: 1.pom.xml中添加监控
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>2.在bootstrap.yml中添加暴露端口
management: endpoints: web: exposure: include: "*"3.在controller层添加@RefreshScope注解 4.在cmd中输入curl -X POST "http://localhost:3355/actuator/refresh这个是最主要到一步,用来告诉客户端需要刷新啦 以上四步就能够实现手动刷新客户端,当github上的配置文件发生修改时就不需要重启项目只需要输入curl -X POST "http://localhost:3355/actuator/refresh进行刷新即可
使用bootstrap.yml配置的config地址中的配置文件就相当于在项目中直接配置的yml文件,可以直接使用,比如把数据库配置配置在github中的文件上,那么只需要bootstrap.yml文件中配置上以下数据就能够正常使用。就像在application.yml中配置一样。但是如果项目中存在application.yml那么最后不要使用bootstrap.yml否则会报错,bootstrap.yml中也可以直接配置需要的数据,比如配置数据库和redis等和application.yml配置一样。
config: label: main name: config profile: dev uri: http://localhost:3344/以上是手动的刷新客户端,如果客户端过多那么会很麻烦,下一篇文章讲讲如何自动刷新。