spring cloud 笔记7 springcloud config 配置中心

0. 先决条件,本机下载安装好Git 软件

        config-dev.yaml 文件 

config:
  info: "master branch,springcloud-config/config-dev.yaml version=1"

        修改本地host:  127.0.0.1 config-3344.com

1. Gitee/Github 新建项目

        https://gitee.com/bogerpeng/springcloud-config

2. 配置为ssh读取方式
        Windows10下github生成SSH公钥_学习笔记icon-default.png?t=N7T8https://blog.csdn.net/weixin_41998265/article/details/88320262        添加个人公钥

SSH Key 突然失效问题解答及处理办法 - Gitee.comicon-default.png?t=N7T8https://gitee.com/help/articles/4352#article-header1​​3. 本地使用一个目录当代码仓库
        3.1 Git bash
        3.2 git clone git@gitee.com:bogerpeng/springcloud-config.git

4. 新建微服务 cloud-config-center 
        4.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.pyh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center3344</artifactId>

    <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-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.pyh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

        4.2 yaml 
                private-key: |           ----这里添加了一个 | 符号

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git@gitee.com:bogerpeng/springcloud-config.git
          ##### 搜索目录
          search-paths:
            - springcloud-config
          strict-host-key-checking: false
          ignore-local-ssh-settings: true
          private-key: |
            -----BEGIN RSA PRIVATE KEY-----
            MIIJKgIB.....xxxxxx
            -----END RSA PRIVATE KEY-----
      ##### 读取分支
      label: master
eureka:
  client:
    register-with-eureka: true    # false: 不把 eureka 自身注册在注册中心里面, 不作高可用的情况下使用这个
    fetch-registry: true         # false: 不从 eureka 上获取服务的注册信息
    service-url:
      #      defaultZone: http://127.0.0.1:7001/eureka/     # 单例版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版

        4.5 配置文件格式

      4.5 验证
        启动 注册中心,启动config 配置中心:
        http://config-3344.com:3344/master/config-dev.yaml

5. 配置客户端配置文件 cloud-config-client

        5.1 初始化 配置客户端 module
                pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.pyh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client3355</artifactId>

    <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-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.pyh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

        bootstrap.yaml

         主启动类:

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

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

        Controller 类 

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

        结果: config 的客户端也能加载到主 config的内容.
        但是这个改动是静态的,每次主config刷新了内容,config客户端还是得重新启动才能加载到最新的配置内容

         5.2 引入 config 客户端的手动版刷新
        引入 actuator 并暴露监控端点

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

        Controller添加注解 @RefreshScope

@RefreshScope
public class ConfigClientController 

        每次配置文件修改的时候,手动调用方法去刷新 config客户端的配置,避免了服务重启

curl  -X POST "http://localhost:3355/actuator/refresh"

         假如有多个微服务config客户端3355、3366、3377呢?可否广播?
一次通知,处处生效。 可以看 bus消息总线!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值