• SpringCloud-Config


    一、介绍

    (1)服务注册中心
    (2)管理各个服务上的application.yml,支持动态修改,但不会影响客户端配置
    (3)一般将application.yml文件放在git上,客户端通过http/https方式拉取

    二、项目搭建

    (1)建个远程仓库,创建两个配置文件application.yml
    在这里插入图片描述
    (2)编写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>demo20220821</artifactId>
            <groupId>com.wsh.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-config-config3344</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>com.wsh.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    (3)编写application.yml文件

    server:
      port: 3344
    
    spring:
      application:
        name: cloud-config-service
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/xxx/config.git
          label: master
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: config3344
        prefer-ip-address: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (4)编写启动类

    package com.wsh.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     * @ClassName ConfigMain3344
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/15
     * @Version V1.0
     **/
    @SpringBootApplication
    @EnableEurekaClient
    @EnableConfigServer
    public class ConfigMain3344 {
        public static void main(String[] args) {
            SpringApplication.run(ConfigMain3344.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (5)运行
    在这里插入图片描述

    三、客户端读取

    (1)application.yml是用户级的,bootstrap.yml是系统级的,优先级更高
    (2)编写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>demo20220821</artifactId>
            <groupId>com.wsh.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>com.wsh.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    (3)编写bootstrap.yml

    server:
      port: 3355
    
    spring:
      application:
        name: cloud-config-client-service
      cloud:
        config:
          label: master #分支名
          name: application # - 号前缀
          profile: prod # - 号后缀
          uri: http://localhost:3344 #配置中心地址
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: configClient3355
        prefer-ip-address: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (4)编写启动类

    package com.wsh.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     * @ClassName ConfigMain3344
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/15
     * @Version V1.0
     **/
    @SpringBootApplication
    @EnableEurekaClient
    public class ConfigClientMain3355{
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientMain3355.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (5)编写Controller

    package com.wsh.springcloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName ConfigClientController
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/15
     * @Version V1.0
     **/
    @RestController
    public class ConfigClientController {
    
        @Value("${teset.name}")
        private String name;
    
        @RequestMapping("/getConfig")
        public String getConfig(){
            return name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (6)运行
    在这里插入图片描述

    四、手动刷新客户端配置

    (1)暴露actuator

    server:
      port: 3355
    
    spring:
      application:
        name: cloud-config-client-service
      cloud:
        config:
          label: master #分支名
          name: application # - 号前缀
          profile: prod # - 号后缀
          uri: http://localhost:3344 #配置中心地址
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: configClient3355
        prefer-ip-address: true
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    (2)Controller增加注解@RefreshScope

    package com.wsh.springcloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName ConfigClientController
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/15
     * @Version V1.0
     **/
    @RestController
    @RefreshScope
    public class ConfigClientController {
    
        @Value("${teset.name}")
        private String name;
    
        @RequestMapping("/getConfig")
        public String getConfig(){
            return name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    (3)执行命令,必须是POST请求

    curl -X POST "http://localhost:3355/actuator/refresh"
    
    • 1
  • 相关阅读:
    第3章:运行时数据区概述及线程 详细详解
    Solr安装使用教程
    qml+QQuickPaintedItem笛卡尔坐标和屏幕坐标的转换
    python机器学习与深度学习在气象领域中的实践技术应用
    k8s强制删除一个 Pod
    中国多主数据库:压强投入,期待破茧
    【2023_10_22计算机热点知识分享】:人工智能
    面试知识点1(硬件、Linux、虚拟化、网络、openstack、Docker、KVM)
    MR素数测试及 pycryptodome库下 已知MR伪素数以及强伪证 生成指定伪随机数生成器绕过素性检测
    [答疑]改善系统的性能,用得着业务建模吗
  • 原文地址:https://blog.csdn.net/qq_25243147/article/details/133834396