• SpringCloud配置中心config


    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config

    Apollo 实现分布式配置中心

    zookeeper:实现分布式配置中心,主要是通过持久节点存储配置信息加上事件通知

    Apollo:实现分布式配置中心,主要是通过mysql 数据库存储配置信息,通过有图形界面可以管理配置文件信息

    srpingcloud config: 实现分布式配置中心,主要是通过版本控制器(git/svn)存储配置文件信息,没有后台

    2,搭建springcloud-config 分布式配置中心

          1,搭建码云(git),开源的git 服务器,创建账号,创建仓库,创建文件 

                https://gitee.com/aiyuesheng/springcloud-config/blob/master/config/config-client-dev.properties

                这个放在git 上的配置文件信息,有参数age=10333

                配置文件名:config-client-dev.properties

                配置文件名称规范:服务名-环境.properteis   例如开发环境是:config-client-dev.properties,生产环境:config-client-prd.properties

    3,  搭建eureka 注册中心,这个之前已经写过,需要将springcloud-config 以及 配置中心的客户端注册上去

         最后就是以下三个服务都起来,然后springcloud-config-client 通过springcloud-server 读取git 上的配置文件

    4,搭建springcloud-config(配置中心)

         maven依赖:

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
        <!-- 管理依赖 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.M7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <!--spring-cloud 整合 config-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
            <!-- SpringBoot整合eureka客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
        </dependencies>
        <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/libs-milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    View Code

         application.yml: springcloud-config(配置中心) 这个服务注册到eureka 上的别名:config-server, git环境地址,以及文件夹搜索配置文件信息都有

    ###服务注册到eureka地址
    eureka:
      client:
        service-url:
               defaultZone: http://localhost:8100/eureka
    spring:
      application:
        ####注册中心应用名称
        name: config-server
      cloud:
        config:
          server:
            git:
              ###git环境地址 仓库
              uri: https://gitee.com/aiyuesheng/springcloud-config.git
              ####搜索目录
              search-paths:
                - config  
          ####读取分支      
          label: master
    ####端口号      
    server:
      port: 8888

    启动类:

    @EnableEurekaClient
    @EnableConfigServer
    @SpringBootApplication
    public class ConfigApp {
        public static void main(String[] args) {
            SpringApplication.run(ConfigApp.class, args);
        }
    }

    5,搭建springcloud-client 端,也就是正常的服务,需要从配置中心上面读取配置文件信息

    maven:

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
        <!-- 管理依赖 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.M7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-client</artifactId>
            </dependency>
            <!-- SpringBoot整合eureka客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!-- actuator监控中心 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
        <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/libs-milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    View Code

    配置文件信息:name:应用名称,如果git 上没有这个应用开头的名字,会找不到,profile:dev:git 上配置文件的环境名,service-id:config-server, 是配置中心服务器注册到eureka 上的别名

    spring:
      application:
        ####注册中心应用名称
        name:  config-client
      cloud:
        config:
        ####读取后缀
          profile: dev
          ####读取config-server注册地址
          discovery:
            service-id: config-server
            enabled: true
    #####eureka服务注册地址    
    eureka:
      client:
        service-url:
               defaultZone: http://localhost:8100/eureka    
    server:
      port: 8882
     
    #配置手动实时刷新 
    #managementendpoints.web.exposure.include=*
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    测试类:

    @Component
    @Data
    @RefreshScope
    public class Parameter {
        
        @Value("${age}")
        private String age;
    
    }
    @RestController
    public class IndexService {
    
        @Autowired
        private Parameter parameter;
        
        @RequestMapping("/getAge")
        private String getAge() {
            System.out.println(parameter.getAge());
            return parameter.getAge();
        }
    
    }

    启动类:

    @SpringBootApplication
    @EnableEurekaClient
    public class ConfigClientApp {
        
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApp.class, args);
        }
    
        
    }

    测试完,可以读取数据

    5,刷新数据----手动刷新

    如果git 上配置文件信息修改了,本地缓存的配置文件信息是不会立即刷新的。在上面的例子中,我将读取的参数、,放入到了一个单独的Parameter 中,加上了注解@RefreshScope,同时,需要客户端需要加上actuator 监控中心

            <!-- actuator监控中心 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

    同时,配置文件增加吗,每个接口都实时监控:

    #配置手动实时刷新 
    #managementendpoints.web.exposure.include=*
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    每次修改完毕,需要手动发送post 请求,我是在post man 上发送

    http://127.0.0.1:8882/actuator/refresh

    如果有更新,会有提示,没有,则返回【】

    再次刷新,配置文件信息,就刷新了

    本来,我是将@RefreshScope 放入到了控制层的类上,但是就是读取为null ,很坑。。。。。。

    实时刷新采用SpringCloud Bus消息总线,可以实时刷新,但是影响性能。。。

         

  • 相关阅读:
    实验9: 静态路由和默认路由
    实验8:路由器IOS升级2
    实验7:交换机IOS升级
    实验6:路由器IOS升级
    实验5: IOS的升级与恢复
    实验4: 路由器的密码恢复
    实验3: DHCP 基本配置
    Linux用户的基本操作3 (组的基本管理,用户提权)
    拓展练习:(用户的基本管理及用户提权部分)
    linux用户的基本操作2 用户密码管理
  • 原文地址:https://www.cnblogs.com/pickKnow/p/11389147.html
Copyright © 2020-2023  润新知