Spring Cloud 实战(三)- Spring Cloud Config配置
Spring Cloud Config配置
线上环境,修改配置,动态加载是刚需。本节我将记录下如何运用Spring Cloud Config动态加载服务所需的配置,实现热加载的功能。
前期准备
Config 服务前期准备
Spring Cloud Config需要 git或者svn 作为配置的存储之处。我在 git 上建立了一个Config-Repo的工程,如下图:
这里需要了解一下,Spring boot启动时加载文件的顺序问题
具体可参考:翟永超的《Spring Cloud 微服务实战》一书,第25页。
其中 newproduct.yml 配置为:
spring:
profiles:
active: dev
newproduct-dev.yml 为:
#server
server:
port: 9005
#spring
spring:
application:
name: newproduct
datasource:
name: SpringCloudSell
type: com.alibaba.druid.pool.DruidDataSource
#druid
druid:
filters: stat
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/SpringCloudSell?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: zyx
password: zyx
initial-size: 1
min-idle: 1
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
#mybatis
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.taeyeon
#logging
logging:
level:
com.taeyeon.category.dao: debug
com.taeyeon.info.dao: debug
#test
girl:
name: zwy
age: 23
#boy
boy:
name: zyx
age: 18
Config服务配置
- pom 文件需要引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
- 启动类需要注解 @EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
System.out.println("++++++++++++++++++++++ConfigApplication Start+++++++++++++++++++++++++");
SpringApplication.run(ConfigApplication.class, args);
System.out.println("======================ConfigApplication Start is Done=========================");
}
}
- application.yml 配置
server:
port: 9001
#spring
spring:
application:
name: config
#uri 填写Config-Repo的 github 地址;username和 Password 则填写你的 github的用户名和密码
cloud:
config:
server:
git:
uri: xxxx
username: xxxx
password: xxxx
#eureka
eureka:
client:
service-url:
defaultZone: http://peers1:9010/eureka/,http://peers0:9009/eureka/
fetch-registry: true
register-with-eureka: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
#这里需要消息队列,实现将配置的更改信息推送到消息对列,然后消费者从消息队列中获取配置信息。
rabbitmq:
addresses: localhost
port: 15672
username: guest
password: guest
#主要用于开启refresh 功能
management:
endpoints:
web:
exposure:
include: "*"
至此,config 服务配置完毕,接下来配置config-client:newproduct服务。
newproduct服务配置
- pom 依赖
<!-- RabbitMQ-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
- bootstrap.yml 配置
由于 newproduct需要从 config服务中拉去配置信息,因此 newproduct本地配置 bootstrap.yml,且它的加载优先级高于application.yml。
#spring
spring:
application:
name: newproduct
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev
rabbitmq:
addresses: localhost
port: 15672
username: guest
password: guest
#Eureka Server
eureka:
client:
service-url:
defaultZone: http://peers0:9009/eureka/,http://peers1:9010/eureka/
register-with-eureka: true
fetch-registry: true
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
待续…