Spring Cloud Config如何实现详细集成步骤解析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1696个文字,预计阅读时间需要7分钟。
这篇文章主要介绍了Spring Cloud Config的集成过程,并通过示例代码详细展示了其使用方法。对于想要学习或工作的朋友来说,这是一份非常有价值的参考资料。以下是Spring Cloud Config的简要概述:
Spring Cloud Config是一个中央化的配置管理服务,它允许您集中管理应用程序的配置文件。通过使用Spring Cloud Config,您可以轻松地将配置信息从本地文件系统迁移到远程服务器,从而实现配置的集中管理和动态更新。
以下是一个简单的集成示例:
java// Spring Cloud Config 服务器配置@Configuration@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
// Spring Cloud Config 客户端配置@Configuration@EnableDiscoveryClientpublic class ConfigClientApplication { @Value(${example.config}) private String exampleConfig;
public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}
通过以上示例,您可以看到Spring Cloud Config的集成非常简单。首先,您需要配置一个Spring Cloud Config服务器,然后配置客户端应用程序以从服务器获取配置信息。
Spring Cloud Config对于学习和工作都非常有价值,特别是对于需要集中管理配置的大型项目。如果您需要进一步了解Spring Cloud Config,可以参考以下资源:
- [Spring Cloud Config官方文档](https://docs.spring.io/spring-cloud-config/reference//)- [Spring Cloud Config GitHub仓库](https://github.com/spring-cloud/spring-cloud-config)
希望这些信息对您有所帮助!
这篇文章主要介绍了spring cloud config集成过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Cloud Config 分为
- Config Server:
- 分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息
- Config Client:
- 通过指定配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息
Spring boot版本2.1.8.RELEASE
服务中心使用Consu,启动Consu
1.配置中心(服务端)
easy-config
(1)添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--配置中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
(2)配置
在resources下
A. 添加 bootstrap.properties
spring.profiles.active=native本地存储配置方式
也可以使用git方式
server.port=8091 spring.application.name=easy-config spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port} management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.profiles.active=native spring.cloud.config.server.native.search-locations=classpath:/config/
B. 添加config/easy-api-dev.properties
hello-string=我是来自配置中心的
(3)修改启动类
添加注解@EnableConfigServer开启配置服务支持
package com.tydt.easy.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class EasyConfigApplication { public static void main(String[] args) { SpringApplication.run(EasyConfigApplication.class, args); } }
启动easy-config
浏览器访问 localhost:8091/easy-api/dev
返回结果
{ "name": "easy-api", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/easy-api-dev.properties", "source": { "hello-string": "我是来自配置中心的" } } ] }
说明:
配置中心的配置文件会被转化成相应的web接口
- /{application}/{profile}[/{label}]
- /{application}/{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}/{profile}.properties
- /{label}/{application}-{profile}.properties
2.客户端
easy-api
(1)添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
(2)配置
添加配置bootstrap.properties
通过注册中心的发现服务,去配置中心查找配置
server.port=8083 spring.application.name=easy-api spring.profiles.active=dev spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=easy-config #设为true,如果无法连接config server,启动时会抛异常,并停止服务 spring.cloud.config.fail-fast=true
(3)测试方法
package com.tydt.engine.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
3.测试
启动easy-api
测试地址
localhost:8083/
返回结果
hello,easy-api,我是来自配置中心的
4.更新
修改了配置中心的配置后,如何读取到新的配置呢
(1)修改测试方法
添加注解 @RefreshScope
package com.tydt.easy.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
启动easy-config
启动easy-api
测试地址
localhost:8083/
返回结果
hello,easy-api,我是来自配置中心的
(2)修改配置
easy-config的resources/config/easy-api-dev.properties
hello-string=我是来自配置中心的111
重启easy-config
执行localhost:8083/actuator/refresh
输出
[ "hello-string" ] localhost:8083/
输出
hello,esay-api,我是来自配置中心的111
说明:
- 如果出现Connect Timeout Exception on Url - localhost:8888. Will be trying the next url if available
- 无论在 Config Server 中配置什么端口,Config Client 启动时,会去访问都默认的 8888 端口
- 出现这种情况可以删掉以前的配置文件
- 在resources文件夹下,新建 bootstrap.properties 文件( bootstrap.yml)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计1696个文字,预计阅读时间需要7分钟。
这篇文章主要介绍了Spring Cloud Config的集成过程,并通过示例代码详细展示了其使用方法。对于想要学习或工作的朋友来说,这是一份非常有价值的参考资料。以下是Spring Cloud Config的简要概述:
Spring Cloud Config是一个中央化的配置管理服务,它允许您集中管理应用程序的配置文件。通过使用Spring Cloud Config,您可以轻松地将配置信息从本地文件系统迁移到远程服务器,从而实现配置的集中管理和动态更新。
以下是一个简单的集成示例:
java// Spring Cloud Config 服务器配置@Configuration@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
// Spring Cloud Config 客户端配置@Configuration@EnableDiscoveryClientpublic class ConfigClientApplication { @Value(${example.config}) private String exampleConfig;
public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}
通过以上示例,您可以看到Spring Cloud Config的集成非常简单。首先,您需要配置一个Spring Cloud Config服务器,然后配置客户端应用程序以从服务器获取配置信息。
Spring Cloud Config对于学习和工作都非常有价值,特别是对于需要集中管理配置的大型项目。如果您需要进一步了解Spring Cloud Config,可以参考以下资源:
- [Spring Cloud Config官方文档](https://docs.spring.io/spring-cloud-config/reference//)- [Spring Cloud Config GitHub仓库](https://github.com/spring-cloud/spring-cloud-config)
希望这些信息对您有所帮助!
这篇文章主要介绍了spring cloud config集成过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Cloud Config 分为
- Config Server:
- 分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息
- Config Client:
- 通过指定配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息
Spring boot版本2.1.8.RELEASE
服务中心使用Consu,启动Consu
1.配置中心(服务端)
easy-config
(1)添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--配置中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
(2)配置
在resources下
A. 添加 bootstrap.properties
spring.profiles.active=native本地存储配置方式
也可以使用git方式
server.port=8091 spring.application.name=easy-config spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port} management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.profiles.active=native spring.cloud.config.server.native.search-locations=classpath:/config/
B. 添加config/easy-api-dev.properties
hello-string=我是来自配置中心的
(3)修改启动类
添加注解@EnableConfigServer开启配置服务支持
package com.tydt.easy.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class EasyConfigApplication { public static void main(String[] args) { SpringApplication.run(EasyConfigApplication.class, args); } }
启动easy-config
浏览器访问 localhost:8091/easy-api/dev
返回结果
{ "name": "easy-api", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/config/easy-api-dev.properties", "source": { "hello-string": "我是来自配置中心的" } } ] }
说明:
配置中心的配置文件会被转化成相应的web接口
- /{application}/{profile}[/{label}]
- /{application}/{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}/{profile}.properties
- /{label}/{application}-{profile}.properties
2.客户端
easy-api
(1)添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
(2)配置
添加配置bootstrap.properties
通过注册中心的发现服务,去配置中心查找配置
server.port=8083 spring.application.name=easy-api spring.profiles.active=dev spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=easy-config #设为true,如果无法连接config server,启动时会抛异常,并停止服务 spring.cloud.config.fail-fast=true
(3)测试方法
package com.tydt.engine.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
3.测试
启动easy-api
测试地址
localhost:8083/
返回结果
hello,easy-api,我是来自配置中心的
4.更新
修改了配置中心的配置后,如何读取到新的配置呢
(1)修改测试方法
添加注解 @RefreshScope
package com.tydt.easy.api.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class HelloController { @Value("${hello-string}") private String helloString; @RequestMapping("/") public String Hello(){ return "hello,easy-api,"+helloString; } }
启动easy-config
启动easy-api
测试地址
localhost:8083/
返回结果
hello,easy-api,我是来自配置中心的
(2)修改配置
easy-config的resources/config/easy-api-dev.properties
hello-string=我是来自配置中心的111
重启easy-config
执行localhost:8083/actuator/refresh
输出
[ "hello-string" ] localhost:8083/
输出
hello,esay-api,我是来自配置中心的111
说明:
- 如果出现Connect Timeout Exception on Url - localhost:8888. Will be trying the next url if available
- 无论在 Config Server 中配置什么端口,Config Client 启动时,会去访问都默认的 8888 端口
- 出现这种情况可以删掉以前的配置文件
- 在resources文件夹下,新建 bootstrap.properties 文件( bootstrap.yml)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

