配置中心服务器
- 配置中心服务器,以版本的管理方式对分布式系统提供外部配置支持;
- SpringCloud中采用Spring Cloud Config 进行集成,而想要进行实施更新则需要采用spring cloud bus的方式, 如 Rabbit、Kafka 等。
- 版本管理采用 Git
- 这里采用Kafka,因为本地有开发测试环境
- 服务注册与发现参考
- /bus/refresh 为消息总线带来的刷新方式,可以手动调用
- destination参数:用于刷新指定的应用服务
- e.g. /bus/refresh?destination=feignserver:8084
配置中心服务
创建配置中心服务module
在build.gradle引入:cloud:spring-cloud-config-server、spring-boot-autoconfigure; 因为我们还想可以自动更新所以引入 spring-cloud-starter-bus-kafka,引入spring-boot-starter-actuator、spring-cloud-config-monitor用于监听
build.gradle
apply plugin: 'org.springframework.boot'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
mavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersion
mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.boot:spring-boot-autoconfigure')
compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-config-monitor')
compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
compile('org.xerial.snappy:snappy-java:' + snappyVersion)
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
all*.exclude module: 'log4j-over-slf4j'
all*.exclude module: 'snappy-java'
}
sourceSets {
main {
resources.srcDirs = ['src/main/resources', 'src/main/java']
resources.includes = ['**/*.xml', '**/*.yml']
}
}
jar {
baseName = 'configserver-bootcwenao'
}
因为引入了spring-cloud-starter-bus-kafka需要排除掉这个组件自带 logback,然后又需要兼容本身的log,所以还需要引入org.apache.logging.log4j:log4j-1.2-api
排除logging
configurations {
all*.exclude module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
all*.exclude module: 'log4j-over-slf4j'
all*.exclude module: 'snappy-java'
}
在application.yml中配置Git
spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/youraccount/project/
search-paths: dir
username: xxxxxxxx@xx.com
password: xxxxxx
default-label: master
在application.yml中配置Kafka
spring:
cloud:
stream:
kafka:
binder:
brokers: localhost:9092
zk-nodes: localhost:2181
通过 @EnableConfigServer 启用配置服务
/**
* @author cwenao
* @version $Id ConfigserverBootcwenaoApplication.java, v 0.1 2017-01-12 14:21 cwenao Exp $$
*/
@EnableDiscoveryClient
@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigserverBootcwenaoApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigserverBootcwenaoApplication.class).web(true).run(args);
}
}
创建Config-Client服务
在build.gradle引入:cloud:spring-cloud-starter-config,同时引入 spring-cloud-starter-bus-kafka
apply plugin: 'org.springframework.boot'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
mavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersion
mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.cloud:spring-cloud-netflix-sidecar')
compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
compile('org.springframework.cloud:spring-cloud-stream')
compile('org.xerial.snappy:snappy-java:' + snappyVersion)
compile ('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
all*.exclude module: 'log4j-over-slf4j'
all*.exclude module: 'snappy-java'
}
sourceSets {
main {
resources.srcDirs = ['src/main/resources', 'src/main/java']
resources.includes = ['**/*.xml', '**/*.yml']
}
}
jar {
baseName = 'apigateway-bootcwenao'
}
配置 bootstarp.yml 中 discovery开启访问配置服务中心
server:
port: 10002
sidecar:
port: 20001
#configServer
spring:
application:
name: apigateway
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888/
discovery:
enabled: true
stream:
kafka:
binder:
brokers: localhost:9092
zk-nodes: localhost:2181
注解 @EnableDiscoveryClient
@SpringCloudApplication
@EnableDiscoveryClient
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);
}
}
Git 中文件名字 apigateway-dev.yml
内容为原先在本地的配置
启动顺序
服务注册中心 --> 配置中心 --> 其他服务
可以看到配置中心服务于想依赖的服务都已经启动注册成功
手动调用刷新
- 修改任意的配置文件
- 在postman中调用 /bus/refresh ,post方式调用
自动更新配置/webhook
webhook配置
- 在application.yml中加入encrypt.key
- 此key与github上的Secret,gitoschina上的密码对应
- **webhook url: ** http://cwenao.eicp.net/configserver/bus/refresh
application.yml
encrypt:
key: cwenao
git仓库 webhook配置
可能的错误
- zookeeper没有读取到
- kafka没启动
- log4j2与其他冲突
- git uri地址错误,需要最后加“/”
代码
代码请移步 Github参考地址
如有疑问请加公众号(K171),如果觉得对您有帮助请 github start