Spring Cloud之Config分布式配置应⽤

. 右键⽗⼯程【 yx-parent 】选择【 New - Module 】选项,然后选择创建【 Maven 】类型项⽬(不勾选模 板),将项⽬名称设置为【yx-cloud-config 】。

 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>
com.yx.config 包下创建 ConfigApplication 启动类,使⽤注解 @EnableConfigServer 开启配置中⼼服务器功 能。

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);
 }
}
yx-cloud-config ⼯程的 resources ⽬录下创建 application.yml 配置⽂件并添加以下配置。
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地址进⾏测试

3. 构建 Client 客户端
在page消费者微服务 pom.xml ⽂件中添加 config-client 依赖坐标
<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;
 }
}
Config 配置⼿动刷新
1.在消费者模块添加 springboot-starter-actuator 依赖(已在⽗⼯程中添加)
2. Client 客户端(消费者 模块)的 bootstrap.yml(前面步骤已经把application.yml改为了booststrap.yml) ⽂件中添加配置(暴露通信端点,已添加)

 在消费者的collection类上使⽤到配置信息的类上添加@RefreshScope注解

4. ⼿动向 Client 客户端发起 POST 请求 http://localhost:9100/actuator/refresh ,即可完成刷新配置信息

 Spring Cloud Config+Spring Cloud Bus实现⾃动刷新

1. Config Server 服务端( yx-cloud-config 模块)和客户端( yx-service-page 模块)添加消息总线⽀持,引⼊ bus-amp依赖
<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⽂件中暴露端⼝

 

 4.重启各个服务,更改配置之后,向配置中⼼服务端发送POST请求,各个客户端配置即可⾃动刷新http://127.0.0. 1:9400/actuator/bus-refresh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值