SpringCloud 配置中心

2019-09-26  本文已影响0人  qyfl

基础使用

1. 新增依赖

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

2. 在启动类上加上注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {

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

3. 将配置文件同步到 Git

4. 新增 config server 的配置

spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          # git 路径
          uri: https://gitlab-demo.com/SpringCloud_Sell/config-repo.git
          username: lly835@163.com
          password: nKfm9JMUfWEh
          # 从 git 下载的配置文件存放的本地路径
          basedir: /xxx/xxx/xxx

这个时候启动 config server 就能在浏览器中访问 http:localhost:port/服务名-环境名-分支.yml看到配置。分支可以不填。

其他服务使用 config server 里的配置

1. 新增配置文件 bootstrap.yml

spring:
  application:
    name: order
  cloud:
    config:
      discovery:
        enabled: true
        # config server 的服务名
        service-id: CONFIG
      profile: test
eureka:
  client:
    service-url:
      defaultZone: http://xxx/xxx

这么配置,会先去找注册中心,通过注册中心找到 config server,再找 order.yml 和 order-test.yml 文件。再把两个文件的配置合到一起。作为当前 order 服务的配置。

2.配置依赖

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

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

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

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

高级配置

使用 SpringCloudBus 动态自动刷新配置

1. 新增依赖

config server 和 client 都要新增

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

2. config server 新增配置

management:
  endpoints:
    web:
      expose: "*"

3.修改代码

引入配置的地方

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties("env")
@RefreshScope
public class envConfig {
    private String name;
}

使用配置的地方

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/env")
public class OrderController {
    @Autowired
    private envConfig envConfig;

    @GetMapping("/print")
    public String print() {
        return envConfig.getName();
    }
}

4.使用

修改 git 配置文件之后调用 http://configserver:port/bus/refresh

上一篇 下一篇

猜你喜欢

热点阅读