SpringCloud Config分布式配置中心使用教程介绍
2022-12-09 12:08:56 来源:易采站长站 作者:
目录一、简介二、使用三、热刷新四、SpringCloudBus(消息总线)一、简介SpringCloudConfig为分布式系统中的配置提供服务器端和客户端支持。可以集中管理所有环境中应用程序...
目录
一、简介二、使用
三、热刷新
四、Spring Cloud Bus(消息总线)
一、简介
Spring Cloud Config为分布式系统中的配置提供服务器端和客户端支持。可以集中管理所有环境中应用程序的配置文件。其服务器端存储的默认实现使用GIT。
优势
提供服务端和客户端支持(spring cloud config server和spring cloud config client)集中式管理分布式环境中的配置信息(所有配置文件统一放在了GIT仓库中)
基于Spring环境提供配置管理,与Spring系列框架无缝结合
可用于任何语言开发环境,基于Http协议。
默认基于GIT仓库实现版本控制。
二、使用
1.搭建配置文件仓库
2.搭建Eureka-Server注册中心服务器
3.搭建Config-Server分布式配置中心服务器
(1)导入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring cloud系列技术中,唯一命名特殊的启动器依赖。 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
(2)编写配置文件
server:
port: 8888# 增加分布式配置中心服务端配置。连接什么GIT仓库
spring:
application:
name: config-server
cloud: # spring cloud常用配置前置
config: # 分布式配置中心配置前置
server: # 服务端配置
git: # git文件仓库配置
uri: https://gitee.com/bjsxt_test/config.git # git仓库具体地址
#username: bjsxt_test # 私有仓库必须配置用户名和密码。
#password: 123456789 # 公开仓库可以省略用户名和密码配置。eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
(3)编写启动类
/** * EnableConfigServer - 开启Spring Cloud Config Server的注解。 * 提供分布式配置中心服务端功能。 */ @SpringBootApplication @EnableConfigServer public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class, args); } }
4.搭建Config Client
Config Client对于Spring Cloud Config是客户端,对于Eureka来说可以是Application Server 也可以是Application Client。
(1)导入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- spring cloud config分布式配置中心客户端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
(2)编写配置文件
# 新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
# 配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境spring:
application:
name: Config-Client
cloud:
config: # spring cloud config 客户端配置
uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888
name: bjsxt # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
profile: default # 要读取的配置文件环境是什么,默认default
label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。
(3)服务接口
public interface ConfigClientService { String test(); }
(4)服务实现
@Service public class ConfigClientServiceImpl implements ConfigClientService { @Value("${my.content}") private String content; @Override public String test() { System.out.println("content = " + content); return content; } }
(5)编写控制器
@RestController public class ConfigClientController { @Autowired private ConfigClientService configClientService; @RequestMapping("/test") public String test(){ return configClientService.test(); } }
(6)编写启动类
@SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class,args); } }
三、热刷新
(1)导入依赖(和优雅关闭的依赖一样)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
(2)修改配置文件
management:
endpoints:
web:
exposure:
include: refresh,info,health
(3)修改服务实现([email protected])
@Service @RefreshScope public class ConfigClientServiceImpl implements ConfigClientService { @Value("${my.content}") private String content; @Override public String test() { System.out.println("content = " + content); return content; } }
(4)测试热刷新
http://localhost:8080/actuator/refresh
四、Spring Cloud Bus(消息总线)
(1)导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 总线技术中的amqp相关依赖。 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
(2)修改配置文件
spring:
rabbitmq:
host: 192.168.91.128
username: bjsxt
password: bjsxt
management:
endpoints:
enabled-by-default: true
web:
exposure:
include: bus-refresh,info,health
(3)测试
http://localhost:8080/actuator/bus-refresh
到此这篇关于SpringCloud Config分布式配置中心使用教程介绍的文章就介绍到这了,更多相关Springcloud Config配置中心内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
如有侵权,请发邮件到 [email protected]
最新图文推荐
相关文章
-
Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法
什么是SkyWalking 查看官网https://skywalking.apache.org/ 分布式系统的应用程序性能监视工具,专为微服务、云原生架构和基于容器(Docker、K8s、Mesos)架构而设计。 安装 进入下载页面https://2020-06-18
-
成功解决IDEA2020 Plugins 连不上、打不开的方法
IntelliJ IDEA 2020.1 插件中心一直打不开,鉴于有部分同学反馈设置http proxy不能解决,所以可按以下顺序检查 一、设置 http proxy—勾上Auto-detect proxy setting,参照下图,加上地址 http://127.0.02020-06-25
-
IDEA2020 1.1中Plugins加载不出来的问题及解决方法
进入File-Setting 如图,取消勾选,点击确认后重启,点击了以后等一会就可以正常显示 ps:下面看下解决IDEA 2020.1.1 找不到程序包和符号 问题描述 IDEA 2020.1.1 maven项目build的时候报错,找2020-06-28
-
springboot + rabbitmq 如何实现消息确认机制(踩坑经验)
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷2020-07-01
-
JetBrains IntelliJ IDEA 2020安装与使用教程详解
对于JetBrains IntelliJ IDEA 2020的认识 IntelliJ IDEA 2020是一款JAVA编程软件,捷克IntelliJ公司研发推出。该软件提供了一个非常强大的JAVA集成开发环境,不仅添加了对Records的完整代码洞察支持,2020-06-28