在微服务应用中,甚至在一个拥有复杂结构的微服务及分布式应用中,开发难度和运维难度都大大增加。其中最重要的就是配置文件的维护。一般情况下,每一个微服务都需要配置一个application配置文件,以不同场景为例,分为开发环境,测试环境,正式环境等。每一次变更都需要一个个修改并重启服务,操作繁琐并且容易出错。SpringCloud-config是统一的服务配置中心,可以实现一处更新,处处更新。
直接撸起来(本次服务注册中心采用Eureka)
一、创建并配置config-center服务端
config-center服务端主要用于接收git上的配置信息
1、创建config配置中心服务端
yml配置
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/****/****.git #git地址
search-paths: cloud-config #相对路径
#默认master
#label: master
#username:
#password:
#跳过SSL证书验证
#skip-ssl-validation: true
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
主启动类(注意加上@EnableConfigServer)
package com.oolee.cloud;
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 ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
二、创建并配置config-center客户端
1、创建config配置中心客户端
2、yml配置
server:
port: 3355
spring:
application:
name: cloud-config-client
cloud:
config:
label: master
name: cloud-config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3、主启动类
package com.oolee.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author oolee
* @date 2020/9/13 22:43
* @apiNote
*/
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
4、请求接口
package com.oolee.cloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author oolee
* @date 2020/9/13 22:58
* @apiNote
*/
@RestController
@RequestMapping
@Slf4j
@RefreshScope //请求时刷新
public class ConfigController {
/** 获取config中心拉取的结点信息 */
@Value(value = "${config.info}")
private String config;
@GetMapping(value = "/config")
public String getConfig(){
return this.config;
}
}
三、试验
依次启动Eureka7001、conig服务端及config客户端
首先看一下我们的config在git中的结构
从图中看到我们有三个配置文件,分别对应开发环境、测试环境、正式环境
同时在master分支下
相对路径为cloud-config
这里我们以dev环境为例,dev配置内容为:
config:
info: config for dev version:3.0
分别访问config服务端和config客户端接口,如下
config服务端:http://localhost:3344/master/application-dev.yml
config客户端:http://localhost:3355/config
现在更新一下我们git上的config,将version从3.0更新成4.0
config:
info: config for dev version:4.0
这时在刷新一下config服务端和客户端,如下
config服务端
config客户端
可以发现,只有config服务端发生了更新,客户端并没有。这是因为服务端是直接请求git仓库的,当git发生变化时能够及时更新,而config客户端是不会自动刷新的
POST手动刷新
打开Windows控制台
执行
curl -X POST "http://localhost:3355/actuator/refresh"
重新执行以下config客户端接口
最后config客户端也更新到最新版本了!