对应两条分支
springcloud-greenwich集成consul-config + springboot2.1.x
springcloud-finchley集成springcloud-config + springboot2.0.x
分布式微服务架构中,服务数量剧增,如果还是手动去实现配置信息的修改或数据的迁移等,效率是很低的,而且手动操作配置也极有可能出现错误的情况:
这种背景下,中心化的配置服务即配置中心应运而生。配置中心就是一种统一管理各种应用配置的基础服务组件
对应springcloud-greenwich分支,配置中心采用spring-cloud-consul-config
Consul里面已经集成了,在UI界面的Key/Map可以进行配置
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
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
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;
/**
* ConfigApplication
*
* Description: 配置中心DEMO
*
* Creation Time: 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;
}
1.配置key 2.配置value(你定义的配置类,以yaml格式)
config/ s e r v i c e − i d , {service-id}, service−id,{profile}/${data-key}
对应springcloud-finchley分支,配置中心采用spring-cloud-config-server
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<springcloud.version>Finchley.RELEASEspringcloud.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${springcloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-bus-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
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
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;
/**
* ConfigApplication
*
* Description:
*
* Creation Time: 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);
}
}
不同版本对应不同分支
springcloud-greenwich采用了consul-config,不需要下方的hui-base-springcloud-config-repo
【SpringCloud】完整工程 https://github.com/ithuhui/hui-base-springcloud
【Config-Server】配置中心配置文件:https://github.com/ithuhui/hui-base-springcloud-config-repo
作者:HuHui
转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢