Spring Cloud Config三种配置服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/it_lihongmin/article/details/90669347

目录

一、配置服务端和客户端的服务搭建

1、在start.spring.io中添加Web、Actuator、Eureka Discovery、Config Server、Config Client模块

2、在项目中添加config-server-file模块

1)、在启动类中添加@EnableConfigServer注解

2)、在resources在添加configs目录,并在目录下创建三个Spring profiles的文件

3)、application.properties配置文件

4)、初始化file仓库

5)、启动项目等待客户端的调用

3、在项目中添加config-server-github模块

1)、在启动类中添加@EnableConfigServer注解

2)、application.properties配置文件

3)、远程github配置

4)、启动项目等待配置客户端调用

4、在项目中添加config-server-mysql模块

5、在项目中添加config-client模块

1)、创建一个测试的Controller

2)、application.properties配置文件

3)、bootstrap.properties配置文件

4)、启动客户端服务

二、遇到的问题

1、Fetching config from server at : http://localhost:8888

2、当github远程仓库属性进行更改时,不能获取到更新的值


    项目地址为: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/spring-cloud-config

    虽然业界都觉得Spring Cloud Config不好用,并且很少公司直接使用,但是还是应该自己搭建并学习后才进行评价。Spring Cloud Config的服务端默认使用git远程服务(GitHub或者GitLab),也可以使用文件系统、mysql数据库等作为服务端。该篇只进行各种服务端实现的搭建,后面进行说明。先来一张图查看概览:

一、配置服务端和客户端的服务搭建

项目结构如下:

config-server-file: 本地文件系统实现的配置服务端

config-server-github: 远程github实现的配置服务端

config-server-mysql:mysql数据库实现的配置服务端

config-client:配置客户端

1、在start.spring.io中添加Web、Actuator、Eureka Discovery、Config Server、Config Client模块

2、在项目中添加config-server-file模块

1)、在启动类中添加@EnableConfigServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *	文件类型的配置服务器端
 *
 * @author kevin
 * @date 2019/5/24 9:27
 * @since
 */
@EnableConfigServer
@SpringBootApplication
public class ConfigServerFileApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerFileApplication.class, args);
	}
}

2)、在resources在添加configs目录,并在目录下创建三个Spring profiles的文件

其中内容分别是: kevinName = dev-kevin    kevinName = pro-kevin    kevinName = test-kevin    

3)、application.properties配置文件

为了搭建和理解方便,将所有的配置信息放到application.properties中,就不创建bootstrap.properties

# 配置客户端应用名称
spring.application.name=config-server-file
# 启动端口
server.port=9000
# 启用端点 env
management.endpoint.env.enabled=true
# 暴露端点 env 配置多个,隔开
management.endpoints.web.exposure.include=*
# 配置文件的地址
spring.cloud.config.server.git.uri = file://${user.dir}/config-server-file/src/main/resources/configs
# 激活的profiles
spring.profiles.active=dev,test,pro

4)、初始化file仓库

根据上面的配置,我们在resources文件夹目录下创建了几个Spring Profiles的配置文件,现在需要在该目录下进行git的初始化

cd 到configs目录

git init

git add *.properties

git commit -m "测试提交"

5)、启动项目等待客户端的调用

http://localhost:9000/config-client/test

3、在项目中添加config-server-github模块

1)、在启动类中添加@EnableConfigServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerGithubApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerGithubApplication.class, args);
	}
}

2)、application.properties配置文件

# 服务名称
spring.application.name=config-server-github
# 配置服务端口
server.port=9050
# git的远程仓库
spring.cloud.config.server.git.uri = https://github.com/kevin-lihongmin/demo.git
# 是否强制从远程获取
spring.cloud.config.server.git.force-pull = true

3)、远程github配置

     根据配置的远程github(或者gitLab等)地址:https://github.com/kevin-lihongmin/demo.git,在该远程目录下也放置刚才的三个配置文件config-client-dev.properties、config-client-pro.properties、config-client-test.properties。其中config-client为配置客户端的spring.application.name配置的名称。

4)、启动项目等待配置客户端调用

http://localhost:9050/config-client/test

4、在项目中添加config-server-mysql模块

先暂位。。。

5、在项目中添加config-client模块

1)、创建一个测试的Controller

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.RestController;

/**
 *
 *
 * {@link RefreshScope } 开启更新功能
 *
 * @author kevin
 * @date 2019/5/24 11:09
 * @since
 */
@RefreshScope
@RestController
public class ConfigController {

    @Value("${kevinName}")
    private String kevinName;

    @GetMapping("kevinName")
    public String getProperties() {

        return kevinName;
    }
}

2)、application.properties配置文件

# 配置客户端应用名称
spring.application.name=config-client
server.port=9090
# 启用端点 env
management.endpoint.env.enabled=true
# 暴露端点 env 配置多个,隔开
management.endpoints.web.exposure.include=*

3)、bootstrap.properties配置文件

注意:

1、客户端调用config-server-file服务端的地址为:spring.cloud.config.server.git.uri = http://127.0.0.1:9000

2、客户端调用config-server-github服务端的地址为:spring.cloud.config.uri = http://127.0.0.1:9050

3、spring.cloud.config.* 相关的配置放到bootstrap.properties(或者yml)中,不能放到 application.properties(或者yml)中。

# 关联的{application}, 如果该名称没有配置,则直接或者spring.application.name
spring.cloud.config.name = config-client
# 关联的{profile}
spring.cloud.config.profile = test
# 关联的{label}
# spring.cloud.config.label=master
# 配置服务器的Uri
spring.cloud.config.server.git.uri = http://127.0.0.1:9000/3

4)、启动客户端服务

    启动配置客户端后,由于上面开启的spring boot的endpoints,所以调用: 

    http://127.0.0.1:9090/actuator/env/   或者    http://127.0.0.1:9090/actuator/env/kevinName

二、遇到的问题

1、Fetching config from server at : http://localhost:8888

    在启动客户端调用服务的时候,遇到这个问题,可能有两个问题:

1)、服务端是文件系统,在则客户端需要使用spring.cloud.config.server.git.uri,而服务端是git的远程服务时使用          spring.cloud.config.uri

2)、spring.cloud.config 相关的配置放到了application.properties(或者application.yml)而不是 bootstrap.properties(或者bootstrap.yml)

2、当github远程仓库属性进行更改时,不能获取到更新的值

    当git的远程仓库进行修改时,配置服务端配置了spring.cloud.config.server.git.force-pull = true 强制从远程获取数据,客配置客户端获取到的也是旧数据,可以有三种方式:

1)、使用actuator接口进行刷新 http://127.0.0.1:9090/actuator/refresh

2)、@RefreshScope修饰需要配置Bean类、或者接口Controller

3)、若使用了spring cloud bus,则可以调用刷新链接: curl -X POST http://localhost:8888/actuator/bus/refresh

4)、查看refresh接口底层是调用了org.springframework.cloud.context.refresh.ContextRefresher # refresh() ,则可以在服务开启一个定时任务(Schedule), 定时去调用该方法

猜你喜欢

转载自blog.csdn.net/it_lihongmin/article/details/90669347