Spring Cloud Config使用入门(Finchley.RELEASE版)

前言
        在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
        在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

  1. 新建springboot工程,添加依赖
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-config-server</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>

   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>
  1. 在启动类中添加注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServiceApplication.class, args);
    }
}
  1. 配置application.yml文件(关键)
spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: https://github.com/******/config-repo
          username: ******
          password: ******
          basedir:  D:\Documents\GitHub\config-repo
eureka:
  client:
    service-url:
      default-zone: http://locahost:8761/eureka/

至此,服务端配置完成,接下来配置客户端:

  • 引入pom依赖

    org.springframework.cloud
    spring-cloud-config-client
  • application.yml改为bootstrap.yml
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
spring:
  cloud:
    config:
      discovery:
        # 开启服务配置中心发现
        enabled: true
        # 设置服务配置中心名称
        service-id: config-service
      # 设置环境
      profile: dev
  application:
    # 设置客户端服务名称
    name: product

客户端配置完成,经过测试可以正常获取git中提交的配置文件,下一篇讲解spring cloud config的热部署

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值