确认了开发环境之后,我们再来添加相关的pom依赖。
1 2 3 4 5 6 7 8 9 10 | <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> |
1 2 3 4 5 6 | <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> |
1 2 3 4 5 | spring.application.name=springcloud-config-bus-eureka server.port= 8006 eureka.client.register-with-eureka= false eureka.client.fetch-registry= false eureka.client.serviceUrl.defaultZone=http: //localhost:${server.port}/eureka/ |
1 2 3 4 5 6 7 8 9 | @SpringBootApplication @EnableEurekaServer public class ConfigBusEurekaApplication { public static void main(String[] args) { SpringApplication.run(ConfigBusEurekaApplication. class , args); System.out.println( "config bus 注册中心服务启动..." ); } } |
服务端pom
配置、application.properties
配置和代码如下:
1 2 3 4 5 6 7 8 9 10 | <dependencies> <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-server</artifactId> </dependency> </dependencies> |
application.properties:
1 2 3 4 5 6 7 | spring.application.name=springcloud-config-server server.port= 9005 eureka.client.serviceUrl.defaultZone=http: //localhost:8005/eureka/ spring.cloud.config.server.git.uri = https: //github.com/xuwujing/springcloud-study/ spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo spring.cloud.config.server.git.username = spring.cloud.config.server.git.password = |
代码:
1 2 3 4 5 6 7 8 9 10 | @EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication. class , args); System.out.println( "配置中心服务端启动成功!" ); } } |
1 2 3 4 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
1 | management.endpoints.web.exposure.include=refresh |
1 2 3 4 5 6 7 8 9 10 11 12 | @RestController @RefreshScope public class ClientController { @Value ( "${word}" ) private String word; @RequestMapping ( "/hello" ) public String index( @RequestParam String name) { return name+ "," + this .word; } } |
测试
完成上述的代码开发后,我们来进行测试Spring-Config是否可以进行配置实时更新。 首先依次启动springcloud-config-bus-eureka
、springcloud-config-bus-server
和springcloud-config-bus-client
这三个项目。其中9005是服务端springcloud-config-bus-server
的端口,9006是第一个客户端springcloud-config-bus-client
的端口。 启动成功之后,在浏览器输入:
界面返回:
pancm,hello world!!
可以正常得到服务端configtest-pro.properties
的配置信息。
然后在把configtest-pro.properties
的配置更改为:
word=hello
然后我们再浏览器输入:
界面返回:
pancm,hello world!!
可以发现配置并没有实时的刷新,查阅官方文档得知,需要客户端通过POST方法触发各自的/refresh,所以这里我们就用Postman工具模拟post请求刷新,然后再查看信息。
使用POST请求如下地址:
返回:
-
[
-
"word"
-
]
说明完成了word配置的刷新,我们再浏览器输入:
界面返回:
pancm,hello
发现已经成功实现配置刷新了!
示例图:
标签:
Spring Cloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具