SpringCloud Conf 搭建配置中心

pringCloud Conf 搭建配置中心

更多干货

Spring Cloud Config支持在Git, SVN和本地存放配置文件,使用Git或者SVN存储库可以很好地支持版本管理,Spring默认配置是使用Git存储库。在本案例中将使用OSChina提供的Git服务存储配置文件。为了能让Config客户端准确的找到配置文件,需要了解application, profile和label的概念

  • server 服务器的配置
  • client 客户端的配置

Server 端代码

pom 依赖 config-server

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

应用入口引入SpringBootApplication

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

配置

配置appllication.yml 文件,设置服务器端口号和配置文件Git仓库的链接

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/csyeva/eductoconfig
          username: 
          password: 

访问方式

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

client 客户端

pom 依赖

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

配置 bootstrap.yml

在bootstrap.yml中设置客户端名称spring.application.name和Config服务器的地址

  • label 当configserver的后端存储是Git时,默认就是master
  • 如果本地和远程都配置了 那么以远程为准
spring:
  cloud:
    config:
      uri: http://localhost:8080
      profile: dev
      label: master
  application:
    name: provider

动态刷新配置

  • 无需重新启动客户端,即可更新Spring Cloud Config管理的配置
  • 获取配置文件中的profile 的值
@RestController
public class ConfigClientController {

  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String getProfile() {
    return this.profile;
  }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值