微服务架构下服务多了配置文件多,为了方便统一管理配置,可以使用Spring Cloud Config集中管理。
可以从本地仓库读取配置文件,也可以从Git仓库获取。本地仓库的话就是把所有配置文件放在你的Config Server 工程下面,Git的话就新建一个专门放配置文件的仓库就好了。
一、构建Config Server
在前面工程的基础上,本节新建两个model工程,Config Server 和 Config Client。
先演示从Git仓库读取配置
新建model:config-server
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-netflix-eureka-server</artifactId>
</dependency>
在应用主类添加注解:
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
配置文件application.yml
eureka:
client:
serviceUrl:
# 注明自己的服务注册中心的地址
defaultZone: http://localhost:7777/eureka/
server:
port: 9999
spring:
application:
name: config-server
cloud:
config:
server:
git:
# 在github上新建一个仓库(springCloudConfigServer)存放配置文件
# 直接访问文件浏览器也能输出值 http://localhost:9999/config-client-dev.properties
uri: https://github.com/MistraR/springCloudConfigServer
# springCloudConfigServer仓库下的配置文件访问路径
search-paths: /**
#访问git仓库的用户名
username: MistraR
password: ******
# 配置仓库的分支
label: master
创建Git仓库及文件
二、构建Config Client
新建model:config-client
pom.xml
<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-server</artifactId>
</dependency>
应用主类添加注解,添加测试方法:
@SpringBootApplication
@RestController
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
/**
* config-client从config-server获取了name的属性,而config-server是从git仓库读取的
*/
@Value("${name}")
String name;
@RequestMapping(value = "/hi")
public String hi() {
return name;
}
}
配置文件application.yml
eureka:
client:
serviceUrl:
# 注明自己的服务注册中心的地址
defaultZone: http://localhost:7777/eureka/
server:
port: 9998
spring:
application:
name: config-client
cloud:
config:
label: master
#dev开发环境配置文件,test测试环境,pro正式环境
profile: dev
uri: http://localhost:9999/
discovery:
#从配置中心读取文件
enabled: true
# 配置中心的serviceId,即服务名
service-id: config-server
三、测试
启动eureka-server,config-server,config-client。
访问:http://localhost:9998/hi
浏览器输出了name属性的值:RoronoaZoro
config-client从config-server取值,config-server从Git仓库读取文件取值。
四、从本地仓库读取配置文件
config-server工程的依赖包和应用主类上注解不变,配置文件稍作修改
application.yml
server:
port: 9999
spring:
cloud:
config:
server:
#指定从本地读取配置文件,resources/configFile
native:
search-locations: classpath:/configFile
#其他服务配置文件的命名格式{applicationName}-{activeProfile}.yml如:user-service-pro.yml
#所有服务可以共享一个配置文件,在configFile下新建一个application.yml
profiles:
#指定从本地读取配置文件
active: native
application:
name: config-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
在resources文件夹下新建目录configFile,存放所有的配置文件
注意configFile目录下的application.yml文件,一些公共配置就可以写在这里,因为其他的服务读取配置文件的时候,除了读取自己服务的配置文件,还会读取这个文件的配置。
譬如在这里配置了一些公共配置:
#公共配置文件所有配置文件默认继承
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
#关闭服务监控信息Actuator的安全验证
management:
security:
enabled: false
#feign调用开启Hystrix
feign:
hystrix:
enabled: true
这里有一个服务叫user-service,它的配置文件就是上图中的user-service-pro.yml。
在user-service这个工程中的resources目录下的bootstrap.yml配置文件就要指定去哪里读取它真正的配置文件:
bootstrap.yml
#bootstrap名称的配置文件比application名称的配置文件有更高的优先级执行
spring:
application:
name: user-service
cloud:
config:
#指定读取配置文件服务的地址
uri: http://localhost:9998
fail-fast: true
profiles:
#需要读取的配置文件版本 dev pro test
active: pro
user-service-pro.yml才配置了user-service的配置信息
server:
port: 7001
spring:
#指定zipkin服务地址
zipkin:
base-url: http://localhost:8001
# rabbitmq:
# host: localhost
# port: 5672
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://112.74.38.117:3306/springcloud?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&&useSSL=true
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
Spring Cloud Config 可以组合 Spring Cloud Bus 实现配置文件动态刷新,参考文档:文档