[java]微服务架构连载No7 配置中心Config

配置中心:为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, 包括环境,动态配置...

工程结构  spring-cloud-06-config-server  配置中心

spring-cloud-06-config-client  微服务


spring-cloud-06-config-server  配置中心

第一步: pom.xml 导入相关依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>


第二步: (1) 配置端口 7001

     (2) 配置git 地址: https://gitee.com/zhouguang666/springconfig

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zhouguang666/springconfig   #相当prefix
#          search-paths:
#            - /user-service  # 相对路径
#            - /role-service

server:
  port: 7001
  context-path: /


git 地址目录结构如下:


config_client-dev.properties 内容

from=git-dev

config_client.properties 内容

from=git-default


第三步: 开启配置中心

@EnableConfigServer //开启配置中心
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

spring-cloud-06-config-client  微服务

第一步: 导入相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>


第二步: 配置文件 bootstrap.yml

(1) 端口 7002

(2)  指向配置中心 7001 端口

注意: application.name +"-"+ config.profile  = git上配置文件名, spring约定大于配置, 不能随便写,要保持一致

        (3)关闭自动刷新权限验证 security.enabled, 保证手动自动刷新接口 /refresh 不需要用户密码 

spring:
  application:
    name: config_client
  cloud:
    config:
      uri: http://localhost:7001/ ## 表示配置中心地址
      profile: dev  # 环境
      label: master # 分支

server:
  context-path: /
  port: 7002

management:
  security:
    enabled: false   # spring 安全验证 , fasle:自动刷新不用输入密码

第三步:开启springboot 项目配置

@SpringBootApplication
public class ClientApplication {


    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

第四步:  (1) 配置自动刷新作用域

      (2) 使用配置文件属性 from

@RefreshScope //动态刷新 >刷新作用域
@RestController
public class FromController {

    @Value("${from}")
    private String from;


    @GetMapping("/from")
    public String from(){
        return "from is :"+from;
    }

}

期望结果

截图 1: 调用服务 http://localhost:7002/from  返回 from值 为  git-dev

截图2: 修改 git 配置文件config_client-dev.properties  from=git-dev-0.1 并提交

截图2: 调用自动刷新接口 http://localhost:7002/refresh 刷新配置

截图4: 调用服务 http://localhost:7002/from  返回 from值 为  git-dev-0.1


截图1:


截图2:



截图3



截图4:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值