![Spring Cloud之Config分布式配置应⽤,spring cloud,分布式,spring](https://imgs.yssmx.com/Uploads/2023/07/556549-1.png)
在yx-cloud-config⼯程的pom.xml⽂件中引⼊以下依赖坐标(需要将⾃⼰注册到Eureka)。
<dependencies>
<!-- Eureka Client客户端依赖引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
<!-- Config配置中心服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
package com.yx.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer // 开启配置服务器功能
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
server:
port: 9400
# 注册到Eureka服务中心
eureka:
client:
service-url:
defaultZone: http://YXCloudEurekaServerC:9200/eureka,http://YXCloudEurekaServerD:9201/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: yx-service-config
cloud:
config:
server:
git: #用于配置git仓库信息:uri,
uri: https://gitee.com/zhengchunbo/yx-config.git
username: **
password: *****
search-paths: #表示仓库的名称
- yx-config
label: master # 读取分⽀
5.启动yx-cloud-config⼯程,访问http://127.0.0.1:9400/master/application-dev.yml地址进⾏测试
<dependency><groupId> org.springframework.cloud </groupId><artifactId> spring-cloud-config-client </artifactId></dependency>
在page消费者微服务的application.yml⽂件名称修改为bootstrap.yml。并在为bootstrap.yml⽂件中对config 进⾏配置。
spring:
# ⽅式1:暴露指定refresh端⼝
management:
endpoints:
web:
exposure:
include: refresh
application:
name: yx-service-page
cloud:
config:
# config客户端配置和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个⽂件中
name: application #表示获取ConfigServer 中的配置文件名称(application-dev)
profile: dev # 后缀名称
label: master # 分⽀名称
uri: http://localhost:9400 # ConfigServer配置中⼼地址
package com.yx.page.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("config")
@RestController
public class ConfigController {
@Value("${mysql.user}")
private String mysqlUser;
@Value("${person.name}")
private String personName;
@RequestMapping("remote")
public String getRemoteConfig() {
return "mysqlUser=" + mysqlUser + ", personName=" + personName;
}
}
![Spring Cloud之Config分布式配置应⽤,spring cloud,分布式,spring](https://imgs.yssmx.com/Uploads/2023/07/556549-4.png)
在消费者的collection类上使⽤到配置信息的类上添加@RefreshScope注解
![Spring Cloud之Config分布式配置应⽤,spring cloud,分布式,spring](https://imgs.yssmx.com/Uploads/2023/07/556549-6.png)
![Spring Cloud之Config分布式配置应⽤,spring cloud,分布式,spring](https://imgs.yssmx.com/Uploads/2023/07/556549-7.png)
Spring Cloud Config+Spring Cloud Bus实现⾃动刷新
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2..在Config Server服务端(yx-cloud-config模块)和客户端(消费者)(yx-service-page模块)的yml配置⽂件中添加 rabbitmq服务的配置信息
spring:
rabbitmq:
host: 192.168.48.67
username: admin
password: 123456
3.在Config Server微服务(yx-cloud-config模块)和消费者page的application.yml⽂件中暴露端⼝
文章来源:https://www.toymoban.com/news/detail-556549.html
4.重启各个服务,更改配置之后,向配置中⼼服务端发送POST请求,各个客户端配置即可⾃动刷新http://127.0.0. 1:9400/actuator/bus-refresh。 文章来源地址https://www.toymoban.com/news/detail-556549.html
到了这里,关于Spring Cloud之Config分布式配置应⽤的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!