随笔 - 367  文章 - 0  评论 - 20  阅读 - 63万 

 

 

 确认了开发环境之后,我们再来添加相关的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-eurekaspringcloud-config-bus-serverspringcloud-config-bus-client这三个项目。其中9005是服务端springcloud-config-bus-server的端口,9006是第一个客户端springcloud-config-bus-client的端口。 启动成功之后,在浏览器输入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world!!

可以正常得到服务端configtest-pro.properties的配置信息。

然后在把configtest-pro.properties的配置更改为:

word=hello 

然后我们再浏览器输入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world!!

可以发现配置并没有实时的刷新,查阅官方文档得知,需要客户端通过POST方法触发各自的/refresh,所以这里我们就用Postman工具模拟post请求刷新,然后再查看信息。

使用POST请求如下地址:

http://localhost:9006/actuator/refresh

返回:

  1.  
    [
  2.  
    "word"
  3.  
    ]

说明完成了word配置的刷新,我们再浏览器输入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello 

发现已经成功实现配置刷新了!

示例图: 在这里插入图片描述在这里插入图片描述在这里插入图片描述

 

posted on   巨象  阅读(69)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示