Spring Cloud(三)spring cloud config+git配置中心

Spring Cloud(三)spring cloud config+git配置中心

  1. 在git上创建文件夹,放上开发,测试,生产环境的配置文件
  2. 在本地创建config-server-git项目
  3. 完成application.yml配置
  4. 启动ConfigServerGitApplication.java

spring-config原理参考

Server端
- GitHub目录结构
github上config管理目录结构
- 本地创建项目config-server-git
config-server-git

  • application.yml
server:
  port: 8000   #注册中心端口是8001

spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chouxin/spring-cloud-config-repo/
          search-paths: config-repo
          username: 342xxxxx@qq.com
          password: xxxxxxxx
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • ConfigServerGitApplication.java
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerGitApplication {

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

测试

config-server

Client端

  1. 创建config-client-git项目
  2. 创建bootstrap.yml文件
  3. 创建测试demo

spring-config-client项目目录结构
config-client项目目录结构

**特别注意:上面这些与spring-cloud相关的属性必须配置在
bootstrap.properties中,config部分内容才能被正确加载。因为config
的相关配置会先于application.properties,而bootstrap.properties
的加载也是先于application.properties。**
  • application.yml
spring:
  application:
    name: config-client-git

server:
  port: 9000


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • bootstrap.yml
spring:
  cloud:
    config:
      name: cn-qx-config
      profile: dev
      uri: http://localhost:8000/
      label: master
server:
  port: 9000

#spring.application.name:对应{application}部分
#spring.cloud.config.name:对应配置文件名称 cn-qx-config-dev.yml
#spring.cloud.config.profile:对应{profile}部分
#spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
#spring.cloud.config.uri:配置中心的具体地址
#spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。
  • 启动类
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientGitApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientGitApplication.class, args);
    }
}
  • 浏览器调用
@RestController
public class HelloController {

    @Value("${qx.hello}")
    private String gitConfigName;

    @GetMapping("/hello")
    public String fromGitHubServer(){
        return gitConfigName;
    }

}
  • 测试

浏览器输入
http://localhost:9000/hello
config客户端调用config服务端,
客户端调用服务端

完成啦

spring-cloud-config代码示例完整

spring-cloud-config资源文件

  • 问题
  • 问题1:spring-cloud-config-git-client不能动态的感应服务端配置文件的改变,即服务端的配置文件改变了,客户端不能实时获取到(只有在启动的时候才会去获取资源配置文件)
  • 解决:Spring Cloud Config分服务端和客户端,服务端负责将git(svn)中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,springcloud已经给我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh
  • 修改spring-cloud-config-client-git项目可以达到refresh的功能
  • 添加依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
#增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。
  • 开启更新机制
  • 网上资料显示使用 post请求访问 http://localhost:9000/refresh 即可完成,但是试验发现,spring boot 2.0不能使用,亟待解决
  • 接下来会用更加厉害的 spring cloud bus来实现config的配置化,方便管理和集群开发
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值