栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud Config快速入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud Config快速入门

        在微服务系统中,每个微服务不仅仅只有代码,他还需要连接其他资源,例如数据库的配置或功能性的开关等等。随着微服务系统的不断迭代,这些资源配置会越来月多, 整个微服务系统会成为一个网状结构,这个时候就要考虑整个微服务系统的扩展性、伸缩性、耦合性等等。其中一个很重要的环节就是配置管理的问题。

        Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。

快速入门

   参考代码: GitHub - PNZBEIJINGL/spring-cloud-lab  

服务IP端口
config-server127.0.0.18007配置中心服务(使用的GitHub上的仓库)
config-client127.0.0.18006模拟客户端服务

搭建配置中心

服务器端也称为分布式系统的配置中新, 它是一个独立的微服务应用,用来连接配置仓库Git并未客户端提供获取配置信息的访问接口,例如上图的Config Server

1. 创建SpringBoot工程命名为config-server, pox中依赖参考如下,spring-cloud-config-server用于引入相关的依赖

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
    

2. 启动主类增加 @EnableConfigServer 启动配置中心服务端功能

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

3. 修改application.properties配置文件,添加配置中心服务的相关信息并增加

server.port=8006

#服务的名称
spring.application.name=CONFIG-SERVER

############ Spring Cloud Config ################
#GIT仓库位置
spring.cloud.config.server.git.uri=https://github.com/PNZBEIJINGL/spring-cloud-lab/
#仓库路径下的相对位置,可配置多个
spring.cloud.config.server.git.searchPaths=/config-server/src/main/resources/config
#GIT仓库用户名
spring.cloud.config.server.git.username=用户名隐藏啦:)
#GIT仓库密码
spring.cloud.config.server.git.password=密码隐藏啦:)


############  Spring Cloud Config ################0

spring.cloud.config.server.git.uri配置的是Git仓库的项目名,参考截图如下

spring.cloud.config.server.git.searchPath 配置的是项目中配置文件的路径,参考截图

 配置文件内容随便填写例如3个文件分别写

  • from=git-default-1.0
  • from=git-dev-1.0
  • from=git-test-1.0

4.启动config -server 微服务测试配置中心信息

可以使用 postman或者curl工具测试,   curl工具简单安装与使用参考: 如何使用CURL工具及简单介绍_学然后知不足!-CSDN博客

 curl http://127.0.0.1:8006/paramspace/dev  查看paramspace-dev资源文件信息

D:Softcurl-7.80.0-win64-mingwbin>curl http://127.0.0.1:8006/paramspace/dev
{"name":"paramspace","profiles":["dev"],"label":null,"version":"4545c5c1c842b7cce1bd0fde68857682b5b9002b","state":null,"propertySources":[{"name":"https://github.com/PNZBEIJINGL/spring-cloud-lab/config-server/src/main/resources/config/pa
ramspace-dev.properties","source":{"from":"git-dev-1.0"}},{"name":"https://github.com/PNZBEIJINGL/spring-cloud-lab/config-server/src/main/resources/config/paramspace.properties","source":{"from":"git-default-1.0"}}]}

curl http://127.0.0.1:8006/paramspace/test 查看paramspace-test.properties资源信息

D:Softcurl-7.80.0-win64-mingwbin>curl http://127.0.0.1:8006/paramspace/test
{"name":"paramspace","profiles":["test"],"label":null,"version":"4545c5c1c842b7cce1bd0fde68857682b5b9002b","state":null,"propertySources":[{"name":"https://github.com/PNZBEIJINGL/spring-cloud-lab/config-server/src/main/resources/config/p
aramspace-test.properties","source":{"from":"git-test-1.0"}},{"name":"https://github.com/PNZBEIJINGL/spring-cloud-lab/config-server/src/main/resources/config/paramspace.properties","source":{"from":"git-default-1.0"}}]}
D:Softcurl-7.80.0-win64-mingwbin>

配置服务器先从Git上获取配置服务, 会储存一份在config-server 的文件系统上,当访问paramspace-test.properties的时候也会将默认配置paramspace.properties保存到本地, 通过AnnotationConfigApplicationContext对象实现自动刷新

2021-12-17 19:33:50.954  INFO 8364 --- [nio-8006-exec-2] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2bd32f0b: startup date [Fri Dec 17 19:33:50 CST 2021]; root of context hierarchy
2021-12-17 19:33:50.962  INFO 8364 --- [nio-8006-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/10001874/AppData/Local/Temp/config-repo-9485094686146340644/config-server/src/main/resources/config/paramspace-test.properties
2021-12-17 19:33:50.963  INFO 8364 --- [nio-8006-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/10001874/AppData/Local/Temp/config-repo-9485094686146340644/config-server/src/main/resources/config/paramspace.properties

搭建客户端

 客户端是微服务价格中的各个微服务应用或者基础设置,它通过配置中心来管理应用资源和相关的配置内容例如上图中的Service A.

1. 创建SpringBoot应用, 命名为config-client , pom.xml参考如下

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
    

2. 修改启动主类

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {

        new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);
    }

}

3. 创建访问controller,启动 @RefreshScope用于自动刷新bean,  @Value("${from}")绑定配置服务中的属性

@RefreshScope
@RestController
public class ConfigController {

    //绑定配置
    @Value("${from}")
    private String from;

    @Autowired
    private Environment env;

    @RequestMapping("/fromByConf")
    public String from(){
        return this.from;
    }
    @RequestMapping("/fromByEnv")
    public String getFromByEnv(){
        return this.env.getProperty("from","undefined");
    }
}

3. 创建配置文件bootstrap.properties

############ Spring Cloud Config ################
#服务的名称
spring.application.name=paramspace

#对应配置文件规则中的{profile}
spring.cloud.config.profile=dev
#对应配置文件规则中的{label}部分
spring.cloud.config.label=
#配置中心config-server的地址
spring.cloud.config.uri=http://127.0.0.1:8006/
############  Spring Cloud Config ################0

4. 修改applicaiton.properties 配置文件

server.port=8007

启动t测试

先启动config-server和 configclient, 访问服务http://127.0.0.1:8007/fromByConf  时会获取到config-server 配置中心的配置信息

测试: http://127.0.0.1:8007/fromByConf

 也可通过Environment对象来获取配置

测试  http://127.0.0.1:8007/fromByEnv

常见问题

1. Could not resolve placeholder 'from' in value "${from}"

文章总结
  1. Config Server为分布式配置中心,在工程中指定Git仓库位置以及登录信息,ConfigServer文件系统中, 每次客户请求获取配置信息,Config Server 都会从Git仓库通过 git clone克隆方式获取最新的配置到本地,然后读取本地中的Git仓库中的信息,当远程仓库无法访问的时候直接从本地仓库获取配置信息
  2. Config Client 即客户端, 客户端想要读取配置的时候,先根据bootstrap.properties 中配置的应用名和环境名向配置中心Config Server发送请求,Config Server先从git仓库上获取配置到本地然后 创建ApplicationContext 示例从本地仓库读取配置文件并将配置内容返回给Config Client,Config Client 再将其加载到自己的ApplicationContext中

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号