Spring Cloud 之 Config Server搭建

首先还是给出官网地址:https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html,能看官网尽量看官网。

以下进入实践环节

1、Config Server默认使用git(支持git、svn、本地)作为配置仓库,本文首先在github创建一个public的git项目,https://github.com/t5721654/config-server,增加confs目录,并添加了几个properties文件。

注意文件命名要求格式如下,否则会无法访问!

2、通过https://start.spring.io/创建项目,Dependencies选择Eureka Server、Config Server,点击生成并下载,导入到STS

3、导入后的项目结构如下:

4、编辑application.properties,也可以写到bootstrap.properties(yml)中(加载优先级高于application.*,一般用于配置系统参数),bootstrap.properties(yml)只有在使用spring cloud时才有效(由spring cloud context加载)。

spring.application.name=config-server
server.port=8888

# 注册到Eureka
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/

# 配置仓库的分支
spring.cloud.config.label=master
# 配置git仓库uri
spring.cloud.config.server.git.uri=https://github.com/t5721654/config-server
# 配置仓库路径,可配置成${profile}根据运行环境读取不同目录文件  
spring.cloud.config.server.git.searchPaths=confs
# 访问git仓库的用户名,公开项目可填空
spring.cloud.config.server.git.username=your username
# 访问git仓库的用户密码
spring.cloud.config.server.git.password=your password

5、修改Application.java,增加@EnableConfigServer注解,开启ConfigServer能力

@EnableConfigServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

6、运行Application.java

首先打开http://127.0.0.1:8761/ ,在Eureka Server控制台可以看到Application栏中多了CONFIG-SERVER,说明实例已经注册成功。

PS: 在application.properties中配置的注册地址是http://127.0.0.1:8761/eureka/,但是在http://127.0.0.1:8762/中也能看到CONFIG-SERVER,这就是Eureka集群的作用了。

先将8761端口的Eureka Server停掉测试下Eureka Server集群的可用性,刷新http://127.0.0.1:8762/,可以看到CONFIG-SERVER依然是UP状态。

好了,继续说Config Server的事

7、通过浏览器打开:http://127.0.0.1:8888/cache-dev.properties 可直接看到cache-dev.properties中的内容如下:

cache.local.expired: 300
cache.local.length: 2048
name: dev

8、通过Config Client的方式读取配置,

    (1)依然在https://start.spring.io/新建config.client项目,选择依赖: Eureka Server、Web、Config Client,下载之后导入STS。

    (2)修改applications.properties如下:

server.port=8881
spring.application.name=config-client

spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri=http://localhost:8888/

eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/

上面注释了spring.cloud.config.uri,Config Client将通过配置的注册中心找到Config Server。

配置中的.name、.label、.profile三个属性决定了将读取git中master分支下config-client-dev.properties(yml)文件中的属性!

    (3)修改Application.java,并运行。

@SpringBootApplication
@RestController
public class Application {
    
    @Value("${name}")
    String name;

    @RequestMapping(value = "/hi")
    public String hi() {
        return name;
    }

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

    (4)访问http://127.0.0.1:8881/hi,返回内容为king,成功读取了git中config-client-dev.properties中name=king配置!别急,到这还没完!

    接下来修改配置name=king1,通过git push上去,重新访问Client接口http://127.0.0.1:8881/hi,发现返回依然是king,直接访问Config Server http://127.0.0.1:8888/config-client-dev.properties返回内容已经更新!说明Server已经更新了数据,但未同步到客户端。

    要实现客户端配置同步更新,需要通过配置github webhook及spring bus来完成,以后再来补充。

    (吐个槽:要实现配置同步代价还不小,通过文件管理方式交互也不友好,只适合做技术上一些参数配置,不适合运营人员配置业务数据,仅仅做技术配置要多维护一套MQ和Config Server又有点尴尬,个人觉得不如统一开发一套配置后台,然后使用zookeeper做数据同步,封装一个sdk给各客户端订阅参数变更来得更省事。)以上。

猜你喜欢

转载自blog.csdn.net/t5721654/article/details/88534975