spring cloud config 搭建配置中心

9 篇文章 0 订阅
1 篇文章 0 订阅

简介:

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git存放配置文件。以git仓库为例做一套示例,与大家一起学习。

项目结构:

在这里插入图片描述
首先搭建一个服务注册中心,用来注册config server与config client

在pom.xml文件添加依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

application.yml配置文件配置如下:

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启始类中添加标签@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

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

}

服务注册中心搭建完成。

搭建config server:

pom.xml文件添加依赖:

    <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>
    <version>2.1.1.RELEASE</version>
</dependency>

bootstrap.yml配置文件的配置如下:

server:
  port: 8881
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: cloud-config-server

  cloud:
    config:
      server:
        git:
          #仓库地址
          uri: https://github.com/BeiMingHFishNameK/paascloud-master-config-rep.git
          username:
          password:
          #仓库在本地的缓存地址
          basedir: d:/paascloud-master-config-rep
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

:关于这里以及接下来config client为什么用bootstrap.yml而不用application.yml或者application.properties,是因为bootstrap.yml优先于 application.yml或者application.properties 被加载,如果直接写在application.yml会导致项目启动时无法读取到配置中心配置文件导致报错。

关于Git仓库,可自行百度在GitHub创建一个或者引用原有的仓库,将仓库地址复制过来就行了,需参照本文配置的格式引入。

启始类中添加标签:@EnableConfigServer、@EnableEurekaClient、
@EnableDiscoveryClient

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigServerApplication {

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

}

配置中心搭建完成。

搭建config client:

pom.xml文件添加依赖:

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

bootstrap.yml配置文件配置如下:

server:
  port: 8801
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-client
  cloud:
    config:
      discovery:
        enabled: true
        #在这里指向配置中心,才能读取到配置中心的配置文件,只有配置中心向注册中心注册了才能这么写
        service-id: cloud-config-server
      profile: dev
    bus:
      enabled: true
      trace:
        enabled: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

启始类中添加标签:@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {

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

    }

}

写一个控制器测试读取配置中心的配置

@RestController
public class ConfigClientController {

    @Value("${server.version}")
    private String version;

    @RequestMapping("/version")
    public String sayHi(){
        return version;
    }
}

config client 在配置中心的配置如下
在这里插入图片描述

关于config client 在配置中心中的配置文件命名规则如下

{application}-{profile}.yml:其中{application}对应客户端的应用名称(spring.application.name),{profile}对应客户端的spring.profiles.active

项目启动:

先启动eurekaserver,再启动config server,最后启动config client。

访问http://localhost:8801/version

显示如图所示即为成功。
在这里插入图片描述
这样就可以将必要的配置放到配置中心,不能再去定位到某个项目中去修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值