先介绍下Spring Cloud Config
功能 :配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
接下来直接来一个demo
我这里是集成了eureka的,将config-server和config-client注册到eureka
先上eureka的demo
pom依赖,我这里有父级pom,具体jar的版本号就写,自己可以去maven中央仓库找
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-web
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
config-server的demo
pom依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-config-server
//下面这个包是为引入bus热部署
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
配置文件
spring:
application:
name: config-server
rabbitmq:
host: 192.168.1.154
port: 5672
username: admin
password: admin123qwezxc
virtual-host: /developer
//以上部分是集成bus用到的中间件,这里使用rabbitmq
cloud:
config:
server:
health:
enabled: true #健康检查关闭
# repositories:
# config-repo-demo:
# name:
# label:
# profiles:
git:
uri:xxxxxx//配置文件地址
search-paths: /
username: xxxxxx//例如git用户名
password: xxxxxxx//例如git密码
##默认为false,Spring Cloud Config Server强制从远程存储库中提取,如果本地副本是脏的
force-pull: false
##searchPaths: 搜索路径
# repos: 源分组
##路径
# simple:
# special:
# pattern: special*/dev*,*special*/dev*
# uri: https://github.com/special/config-repo
## order: 优先级,order属性的数值越低,优先级越高
# overrides:
# foo: bar将导致配置客户端的所有应用程序独立于自己的配置读取该参数下面的值
label: developer
##该服务端口号
server:
port: 8888
eureka:
client:
service-url:
defaultZone: http://localhost:8889/eureka/
##http://localhost:8881/actuator/bus-refresh post 请求这个地址以后动态刷新配置信息
#bus的端点暴露在服务端的好处是分布式环境以下,只需要请求一次这个地址就可以做到所有的应用的配置文件自动刷新
management:
endpoints:
web:
exposure:
include: bus-refresh//开启这个管理节点,供http调用
# 用户认证,客户端应用接入时加入安全认证配置
# security:
# user:
# name: config
# password: config2018
# basic:
# enabled: true
# 基于消息总线的MQ配置
# cloud:
# stream:
# kafka:
# binder:
# zk-nodes: ${ZK_NODES:localhost:2181}
# brokers: ${KAFKA_BROKERS:localhost:9092}
# requiredAcks: -1
# configuration:
# security:
# protocol: SASL_PLAINTEXT
# sasl:
# mechanism: PLAIN
# jaas:
# loginModule: org.apache.kafka.common.security.plain.PlainLoginModule
# options:
# username: test
# password: test-secret
# # 开启跟踪事件消息(默认是false)
# bus:
# trace:
# enabled: true
# # 自定义topic主题
# destination: test.springcloud.config
config-client的demo
pom依赖导入
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
启动类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
@EnableDiscoveryClient
@RefreshScope
public class ConfigClientApplication {
@Value("${foo}")
private String value;
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi() {
return value;
}
}
配置文件
application
spring:
cloud:
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: 192.168.1.154
port: 5672
username: admin
password: admin123qwezxc
virtual-host: /developer
##http://localhost:8881/actuator/bus-refresh post 请求这个地址以后动态刷新配置信息
#management:
# endpoints:
# web:
# exposure:
# include: bus-refresh
# spring:
# application:
# name: letv-mas-client
# index: ${CLIENT_SERVER_IP:127.0.0.1}:${server.port}
# profiles:
# active: ${CLIENT_PROFILE:default}
# #include: busdev,streamdev
# cloud:
# config:
# uri: ${CONFIG_SERVER_DOMAIN:http://config.xxx.cn/}
# failFast: true #the client will halt with an Exception
# enabled: true
# # boostrap.yml配置优先于启动参数变量--spring.profiles.active
# profile: ${spring.profiles.active:${CLIENT_PROFILE:default}}
# label: master
# # 访问配置中心,用户安全认证
# username: config
# password: config2018
# # 激活定时任务,当GIT版本发生变更后加载最新配置上下文
# watcher:
# enabled: true
# security:
# user:
# name: config
# password: config2018
#
# # 基于消息总线的MQ配置(kakfa队列),如果zipkin中也使用kafka队列,那么需要通过binder形式配置做隔离,否则会互相影响,无法下发配置消息。
# spring:
# cloud:
# stream:
# # 自定义开关
# enabled: true
# # 指定中间件
# default-binder: config-kafka
# binders:
# config-kafka:
# type: kafka
# environment:
# spring:
# cloud:
# stream:
# kafka:
# binder:
# zkNodes: ${ZK_NODES:localhost:2181}
# brokers: ${KAFKA_BROKERS:localhost:9092}
# # 生产者确认,0、1、-1,默认为1。0为不确认,1为leader单确认,-1为同步副本确认。-1的情况下消息可靠性更高。
# required-acks: -1
# # 是否自动创建topic,默认为true。设为false的情况下,依赖手动配置broker相关topic>配置,如果topic不存在binder则无法启动。
# auto-create-topics: true
# configuration:
# security:
# protocol: SASL_PLAINTEXT
# sasl:
# mechanism: PLAIN
# jaas:
# loginModule: org.apache.kafka.common.security.plain.PlainLoginModule
# options:
# username: test
# password: test-secret
# bus:
# # 是否启用bus
# enabled: true
# # Bus使用的队列或Topic,kafka中的topic,rabbitmq中的queue
# destination: test.springcloud.config
# trace:
# # 是否启用bus事件跟踪,可以通过/trace页面查看
# enabled: true
# refresh:
# # 是否发送refresh事件,开启时支持基于config文件变更的动态配置
# enabled: true
# env:
# # 是否开启env事件,开启时支持直接动态配置相应环境变量,如/bus/env?arg1=value1&arg2=value2
# enabled: true
bootstrap
spring:
application:
name: config-client
cloud:
config:
label: developer
profile: dev
uri: http://localhost:8888/
discovery:
enabled: true
service-id: config-server
server:
port: 8881
eureka:
client:
service-url:
defaultZone: http://localhost:8889/eureka/
操作说明
1.首先启动eureka项目,然后再启动config-server,最后启动config-client
2.启动成功后,访问hi接口,得到foo参数变量;
3.去仓库中修改foo变量值
4.利用postman请求http://localhost:8881/actuator/bus-refresh 注意必须使用post请求
5.重复第二步,获取到最新的foo参数.
注意:这里最好是将bus集成到config-server中,这样才分布式环境中只需要请求config-server一次,就可以实现所有机器服务的刷新
说明一下bootstrap.propertie与application.properties的区别
① .bootstrap.yml先于application.properties被加载。
②.当使用Spring Cloud Config Server时,需要将spring.application.name和spring.cloud.config.server.git.uri写入bootstrap.yml。
③. 一些encryption/decryption(加密/解密)存储在bootstrap.yml里面。
④. bootstrap.yml被Spring ApplicationContext的父类加载,这个类先于加载**application.yml的ApplicatonContext启动。
当使用 Spring Cloud 的时候,配置信息一般是从 config server 加载的,为了取得配置信息(比如密码等),你需要一些提早的或引导配置。因此,把 **config server **信息放在 bootstrap.yml,用来加载真正需要的配置信息。