学习笔记:微服务10 spring cloud config server配置中心
微服务集群应用服务众多,spring config server 可以把各微服务的配置文件集中起来管理
一、spring config server搭建
1.创建一个spring boot start 项目,我这命名为microservice-config-server-8401
2.pom.xml 加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.application.properties配置
server.port=8401
spring.application.name=MicroserviceConfigServer8401
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8101/eureka/,http://admin:[email protected]:8102/eureka/
spring.profiles.active=native //**本地配置文件
spring.cloud.config.server.native.search-locations=classpath:/config //配置文件放置目录
4.启动类加@EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class MicroserviceConfigServer8401Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConfigServer8401Application.class, args);
}
}
5.可以启动测试了
以上是配置文件集中存放目录,可以查看配置文件,说明服务器ok了
二 spring config 客户端
就是各个微服务
1. 在原来的微服务application.properties 同目录下创建文件bootstrap.properties 这个文件优先度较高,会在启动时先读取
bootstrap.properties的内容如下
spring.application.name=MicroserviceServerA8801 //应用名称
spring.cloud.config.uri=http://centos7.linbsoft.com:8401 //配置中心地址
spring.cloud.config.enabled=true
spring.cloud.config.profile=dev
这样,自动会到配置中心找 MicroserviceServerA8801-dev.properties的文件
2. 在配置中心config目录创建配置文件MicroserviceServerA8801-dev.properties,把原来微服务下的application.properties内容移动到MicroserviceServerA8801-dev.properties文件,(除了spring.application.name=MicroserviceServerA8801这行外)
server.port=8801
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8101/eureka/,http://admin:[email protected]:8102/eureka/
3.先启动config server配置中心服务,再启动微服务,会看到能正常启动,控制台可以看见启动时从config服务器拉取配置