美文网首页
spring cloud config

spring cloud config

作者: 刘小刀tina | 来源:发表于2020-03-11 13:12 被阅读0次

产生的背景:


image.png

定义


image.png

配置读取规则


Springcloud config 搭建分类客户端 和服务端

(1)服务端
pom.xml 

    <dependencies>
        <!--   引入eureka客户端     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--引入spring cloud config 服务端 的依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>
=========================================================
启动类
@SpringBootApplication
@EnableEurekaClient//表明是eureka客户端
@EnableConfigServer //开启spring cloud config
public class ConfigCenterApplication3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication3344.class,args);
    }
}
=========================================================
application.yml  配置文件
#配置端口号
server:
  port: 3344
#配置服务名
spring:
  application:
    name: springcloud-config-center

  cloud:
    config:
      server:
        git:
          uri: https://github.com/liuyanan920921/springcloud-config.git #github上面的git仓库名字
          search-paths:
            - springcloud-config #搜索目录
      #读取分支
      label: master

#注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://springcloud-eureka-server7001:7001/eureka,http://springcloud-eureka-server7002:7002/eureka
  instance:
    instance-id: springcloud-config-center
    prefer-ip-address: true  #访问路径可以显示IP地址
    lease-renewal-interval-in-seconds: 1  #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除

#开启eureka负载均衡策略
ribbon:
  eureka:
    enabled: true
=========================================================

(2)客户端
pom.xml 
    <dependencies>

        <!--引入spring cloud config 客户端 的依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--   引入eureka客户端的依赖包     -->
        <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>

        <!--添加common 通用包-->
        <dependency>
            <artifactId>springcloud-api-common</artifactId>
            <groupId>com.tina.springcloud</groupId>
            <version>1.0-SNAPSHOT</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>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>
=========================================================
启动类
@SpringBootApplication
@EnableEurekaClient
@RefreshScope //开启实时监控
public class ConfigClientApplication3355 {

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication3355.class,args);
    }
}
=========================================================
bootstrap.yml 配置文件
#配置端口号
server:
  port: 3355
#配置服务名
spring:
  application:
    name: springcloud-config-client
  cloud:
    #config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件的名称
      profile: dev #读取后缀名出 ,上述配置综合:master分支上config-dev.yml
      uri: http://localhost:3344 #配置中心的地址
#注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://springcloud-eureka-server7001:7001/eureka
  instance:
    instance-id: springcloud-config-client
    prefer-ip-address: true  #访问路径可以显示IP地址
    lease-renewal-interval-in-seconds: 1  #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除

#开启eureka负载均衡策略
ribbon:
  eureka:
    enabled: true

#暴露监控的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
=========================================================
控制类
@RestController
public class ConfigClientController {

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

    @GetMapping(value = "/configInfo")
    public String getConfigInfor(){
        return configInfo;
    }

}
=========================================================

a) github上的配置文件变动,

会直接影响到spring cloud config 服务端的数据跟着变动;
但是不会影响到spring cloud config 服务端的数据;

b) 确保spring cloud config 服务端的数据跟github上的配置文件始终保持一致,有如下两种方案

(1) 重启spring cloud config 服务端
(2) 参考如上spring cloud config 服务端 的配置的基础上,
运维人员每次修改github上的配置文件同时,须额外给对应的每个spring cloud config 服务端发送一个post请求 ,如此服务端不须重启 即可更新到最新的数据;

相关文章

网友评论

      本文标题:spring cloud config

      本文链接:https://www.haomeiwen.com/subject/fezfjhtx.html