SpringCloud Config Git Manager

SpringCloud config git

原作者地址: https://yq.aliyun.com/articles/680731

  1. gitHub中创建项目并存放配置文件
  2. 搭建一个注册中心
  3. 搭建一个服务git仓库进行连接
  4. 搭建一个服务通过仓库连接服务调用配置文件

架构图

在这里插入图片描述

gitHub中创建项目并存放配置文件

在这里插入图片描述

搭建一个注册中心 :: 服务注册中的地址
eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/
搭建一个服务git仓库进行连接

pom 文件中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

添加application.yaml配置文件

server:
  port: 8000

spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/jiangruyi/SpringCloud.git
          search-paths: spring-cloud-config-core
          username: 2491920818@qq.com
          password: xxx

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7070/eureka/

编写SpringBoot启动类

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}
搭建一个服务通过仓库连接服务调用配置文件

添加 pom 依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

编写 bootstrap.yaml 配置文件

spring:
  cloud:
    config:
      name: application
      profile: dev
      uri: http://localhost:8000/
      label: master

server:
  port: 9000

spring.cloud.config.uri :: 与git连接的服务地址

编写基本配置文件 application.yaml

spring:
  application:
    name: config-client-git

server:
  port: 9000

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7070/eureka/

编写 SpringBoot 启动类读取git上的配置文件

@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class Application {

	@Value("${com.znsd.config}")
	private String gitValue;
	
	public void setGitValue(String gitValue) {
		this.gitValue = gitValue;
	}
	
	@GetMapping("hello")
	public String hello () {
		return gitValue;
	}
	
	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}

配置热部署

在调用配置服务端 pom 文件中添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在要动态热部署的配置类中添加: @RefreshScope 注解

POST 方式访问URL http://localhost:9000/refresh刷新配置

注意:

  • 返回消息中包含: Full authentication is required to access this resource.
    • 解决方案:
      • 将安全认证关掉: management.security.enabled=false
      • 配置一个安全认证
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值