Spring Cloud与Docker高并发微服务架构设计实施---配置管理中心
配置管理中心可以为所有微服务提供一个统一的配置管理服务。微服务可以使用本地工程的配置,也可以使用配置管理中心的配置,当这两方面具有相同的配置项时,系统默认优先使用配置管理中心提供的配置。
在模块的项目管理中添加如下依赖引用
<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-server</artifactId> </dependency>
<!--消息总路线服务组件,可以为配置管理中心提供使用消息进行通信的功能--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies>
创建一个启动程序
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigApplication { public static void main(String[] args){ SpringApplication.run(ConfigApplication.class,args); } }
@EnableDiscoveryClient:将这个应用设置为注册管理中心的客户端,可以使用服务发现的功能
@EnableConfigServer:将这个应用设置为配置管理服务器
在工程的配置文件application.yml中添加如下配置:
server: port: 8888 management: security: enabled: false eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
将应用的端口设置为:8888
连接了本地的注册管理中心
关闭安全设置。因为配置中心在内网中使用,也不对外提供服务,所以为了方便调用,这里将安全管理功能设置为false
在工程中添加一个名字为bootstrap.yml的配置文件
spring: application: name: config cloud: config: server: git: uri: https://gitee.com/chenshaojian/base-config-repo.git username: password: rabbitmq: addresses: amqp://localhost:5762 username: guest password: guest
使用Git仓库来存放客户端的配置文件,它的链接地址设置如下:
https://gitee.com/chenshaojian/base-config-repo.git
设定RabbitMQ服务器的配置。其中RabbitMQ服务器的地址根据安装情况进行设定
微服务如何使用配置管理中心
首先,在应用的项目管理配置pom.xml文件中添加如下依赖:
<!--配置服务--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--消息服务--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
然后,在工程中增加一个名称为bootstrap.yml的配置文件
spring: application: name:turbine cloud: config: uri: http://localhost:8888 rabbitmq: address: amqp://localhost:5762 username: guest password: guest
将这个应用的名称设置为:turbine
连接配置管理中心
设定rabbitMQ服务器的各项参数
当在配置管理中心的文件仓库中具有“application.xml”和“turbine.xml”文件时,其中的配置信息就将会被这个应用使用。application.yml将会被所有使用配置管理中心的应用使用,而以应用名称开头的配置文件只为应用单独所有。