zoukankan      html  css  js  c++  java
  • SpringCloud-06-SpringCloudConfig分布式配置


    为微服务提供集中化的外部配置支持
    包括服务端和客户端两部分

    服务端

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.2.4.RELEASE</version>
    </dependency>
    <!--actuator完善监控信息-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    配置

    server:
      port: 3344
    spring:
      application:
        name: springcloud-config-server
      # 连接远程仓库
      cloud:
        config:
          server:
            git:
              uri: https://github.com/ChenCurry/springcloud-config.git

    开启注解

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

    远程application.yml

    spring:
      profiles:
        active: dev
    ---
    spring:
      profiles: dev
      application:
        name: springcloud-config-dev
    ---
    spring:
      profiles: test
      application:
        name: springcloud-config-test

    访问远程配置

    http://localhost:3344/application-dev.yml

    访问资源的方式

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

    客户端

    先准备远程配置

    spring:
      profiles:
        active: dev
    ---
    server:
      port: 8201
    #spring配置
    spring:
      profiles: dev
      application:
        name: springcloud-provider-dept
    #eureka
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
    ---
    server:
      port: 8202
    #spring配置
    spring:
      profiles: test
      application:
        name: springcloud-provider-dept
    #eureka
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
    config-client.yml

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--actuator完善监控信息-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.2.4.RELEASE</version>
    </dependency>

    系统配置(访问“读远程配置的服务端”)

    #系统级别的配置,区别于用户级别的配置(application.yml)
    spring:
      cloud:
        config:
          name: config-client #需要从git上读取的资源名称,不需要后缀
          profile: test
          label: master
          uri: http://localhost:3344

    用户配置(标识当前服务)

    # 用户级别的配置
    spring:
      application:
        name: springcloud-config-client-3355

    访问远程配置

    @RestController
    public class ConfigClientController {
    
        @Value("${spring.application.name}")
        private String applicationName;
        @Value("${eureka.client.service-url.defaultZone}")
        private String eurekaServer;
        @Value("${server.port}")
        private String port;
    
        @RequestMapping("/config")
        public String getConfig(){
            return "applicationName:"+applicationName
                    +"eurekaServer:"+eurekaServer
                    +"port:"+port;
        }
    
    }

    http://localhost:8202/config


    实战测试

    解决集群的配置问题

    对应远程配置文件

    spring:
      profiles:
        active: dev
    ---
    server:
      port: 7001
    #spring配置
    spring:
      profiles: dev
      application:
        name: springcloud-config-eureka
    #Eureka
    eureka:
      instance:
        hostname: eureka7001.com  #服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #false表示自己为注册中心
        service-url: #监控页面
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #集群(关联)
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    ---
    server:
      port: 7001
    #spring配置
    spring:
      profiles: test
      application:
        name: springcloud-config-eureka
    #Eureka
    eureka:
      instance:
        hostname: eureka7001.com  #服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #false表示自己为注册中心
        service-url: #监控页面
          #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #集群(关联)
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    config-eureka.yml
    spring:
      profiles:
        active: dev
    ---
    server:
      port: 8001
    #mybatis配置
    mybatis:
      type-aliases-package: com.gfpz.springcloud.pojo
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    #spring配置
    spring:
      profiles: dev
      application:
        name: springcloud-config-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db01?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        username: root
        password: root
    #eureka
    eureka:
      client:
        service-url:
          #往Eureka单机发布 defaultZone: http://localhost:7001/eureka/
          #往Eureka集群发布
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: Springcloud-provider-dept8001
    #info 配置
    info:
      app.name: gfpz-springcloud
      company.name: possible2dream.cn
    
    ---
    server:
      port: 8001
    #mybatis配置
    mybatis:
      type-aliases-package: com.gfpz.springcloud.pojo
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    #spring配置
    spring:
      profiles: test
      application:
        name: springcloud-config-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db02?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        username: root
        password: root
    #eureka
    eureka:
      client:
        service-url:
          #往Eureka单机发布 defaultZone: http://localhost:7001/eureka/
          #往Eureka集群发布
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: Springcloud-provider-dept8001
    #info 配置
    info:
      app.name: gfpz-springcloud
      company.name: possible2dream.cn
    config-dept.yml
    http://localhost:3344/master/config-eureka-dev.yml(先保证服务端能够读取到)
    http://localhost:7001/(7001端口是在远程文件里配置的,能访问成功,说明已经读到了)
    http://localhost:8001/dept/get/1(服务端集群对应配置文件搞定)

    https://github.com/ChenCurry/springcloud.git


    击石乃有火,不击元无烟!!
  • 相关阅读:
    HDU4628+状态压缩DP
    Javascript 去掉字符串前后空格的五种方法
    Javascript 数组之判断取值和数组取值
    ASP.NET MVC 出现错误 “The view 'XXX' or its master was not found or no view engine support”
    ASP.NET MVC 页面调整并传递参数
    ASP.NET MV3 部署网站 报"Could not load file or assembly ' System.Web.Helpers “ 错的解决方法
    ASP.NET MVC 控制器向View传值的三种方法
    CSharp 如何通过拼接XML调用存储过程来查询数据
    SQLServer : EXEC和sp_executesql的区别
    关于SQLServer2005的学习笔记—异常捕获及处理
  • 原文地址:https://www.cnblogs.com/rain2020/p/13537549.html
Copyright © 2011-2022 走看看