文章目录
前言
对应两条分支
springcloud-greenwich集成consul-config + springboot2.1.x
springcloud-finchley集成springcloud-config + springboot2.0.x
Note
分布式微服务架构中,服务数量剧增,如果还是手动去实现配置信息的修改或数据的迁移等,效率是很低的,而且手动操作配置也极有可能出现错误的情况:
- 复杂的业务对应大量的配置项
- 对集群部署的应用配置进行修改时需要一次修改每个节点上的应用配置
这种背景下,中心化的配置服务即配置中心应运而生。配置中心就是一种统一管理各种应用配置的基础服务组件
GitHub
地址:https://github.com/ithuhui/hui-base-springcloud
分支:finchley | greenwich
模块:【hui-base-springcloud-config】
finchley分支配置中心配置存放在git-> https://github.com/ithuhui/hui-base-springcloud-config-repo
Greenwich分支(springcloud-consul-config)
对应springcloud-greenwich分支,配置中心采用spring-cloud-consul-config
Consul里面已经集成了,在UI界面的Key/Map可以进行配置
maven
<!-- 支持consul做配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml
server:
port: 29090
servlet:
context-path: /config-service-client
spring:
application:
name: config-service-client
profiles:
active: dev
## consul 配置
cloud:
consul:
# consul服务器地址
host: localhost
# consul服务端口
port: 8500
config:
# enabled为true表示启用配置管理功能
enabled: true
# watch选项为配置监视功能,主要监视配置的改变
watch:
enabled: true
delay: 10000
wait-time: 30
# 表示如果没有发现配置,是否抛出异常,true为是,false为否,当为false时,consul会打印warn级别的日志信息
fail-fast: true
# 表示使用的配置格式
format: yaml
# 配置所在的应用目录名称
# prefix: config
name: ${spring.application.name}
data-key: data
# 服务发现配置
discovery:
# 服务名称
service-name: ${spring.application.name}
# 启用服务发现
enabled: true
# 启用服务注册
register: true
# 服务停止时取消注册
deregister: true
# 表示注册时使用IP而不是hostname
prefer-ip-address: true
# 执行监控检查的频率
health-check-interval: 30s
# 设置健康检查失败多长时间后,取消注册
health-check-critical-timeout: 30s
# 健康检查的路径
health-check-path: /${spring.application.name}/actuator/info
# 服务注册标识,格式为:应用名称+服务器IP+端口
instance-id: ${spring.application.name}
#instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
## test refresh config
test:
name: ithuhui
address: guangzhou
Code
package com.hui.base.springcloud.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* <b><code>ConfigApplication</code></b>
* <p/>
* Description: 配置中心DEMO
* <p/>
* <b>Creation Time:</b> 2018/11/26 0:06.
*
* @author Hu Weihui
*/
@SpringBootApplication
@EnableDiscoveryClient
//当需要动态刷新配置的时候添加
@EnableConfigurationProperties
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
动态刷新配置
创建配置文件类
@Component
@ConfigurationProperties(prefix = "test")
@Data
@ToString
public class TestConfig {
private String name;
private String address;
}
新增注解@EnableConfigurationProperties
Consul-UI界面配置Key/map
1.配置key 2.配置value(你定义的配置类,以yaml格式)
config/ s e r v i c e − i d , {service-id}, service−id,{profile}/${data-key}
Finchley分支(springcloud-config-server)
对应springcloud-finchley分支,配置中心采用spring-cloud-config-server
Maven Dependency
<!--SpringBoot Parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springcloud.version>Finchley.RELEASE</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--Spring Cloud dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--动态更新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/ithuhui/hui-base-springcloud-config-repo
username: ithuhui
password:
basedir: D:\repo-github\hui-base-springcloud\out\basedir
fail-fast: true
eureka:
client:
service-url:
defaultZone: http://localhost:28761/eureka
instance:
appname: config-server
prefer-ip-address: true
server:
port: 9090
Code
package com.hui.base.springcloud.config;
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;
/**
* <b><code>ConfigApplication</code></b>
* <p/>
* Description:
* <p/>
* <b>Creation Time:</b> 2018/11/26 0:06.
*
* @author Hu Weihui
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
动态刷新配置文件
-
需要RabbitMq支持
-
引入包spring-cloud-starter-bus-amqp
-
配置类添加@RefreshScope
-
执行刷新 http://xxxxx:port/actuator/refresh
Author
作者:HuHui
转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢