开发者

SpringCloud Config分布式配置中心使用教程介绍

目录
  • 一、简介
  • 二、使用
  • 三、热刷新
  • 四、Spring Cloud Bus(消息总线)

一、简介

Spring Cloud Config为分布式系统中的配置提供服务器端和客户端支持。可以集中管理所有环境中应用程序的配置文件。其服务器端存储的默认实现使用GIT。

优势

  • 提供服务端和客户端支持(spring cloud config server和spring cloud config client)
  • 集中式管理分布式环境中的配置信息(所有配置文件统一放在了GIT仓库中)
  • 基于Spring环境提供配置管理,与Spring系列框架无缝结合
  • 可用于任何语言开发环境,基于Http协议。
  • 默认基于GIT仓库实现版本控制。

二、使用

1.搭建配置文件仓库

2.搭建Eureka-Server注册中心服务器

3.搭建Config-Server分布式配置中心服务器

(1)导入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.12.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR12</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependenehypKhIWdcies>
    <!-- spring cloud系列技术中,唯一命名特殊的启动器依赖。 -->
    <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>
</dependencies>

(2)编写配置文件

server:

  port: 8888

# 增加分布式配置中心服务端配置。连接什么GIT仓库

spring:

  application:

    name: config-server

  cloud: # spring cloud常用配置前置

    config: # 分布式配置中心配置前置

      server: # 服务端配置

        git: # git文件仓库配置

          uri: https://gitee.com/bjsxt_test/config.git # git仓库具体地址

          #username: bjsxt_test # 私有仓库必须配置用户名和密码。

          #password: 123456789 # 公开仓库可以省略用户名和密码配置。

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/

(3)编写启动类

/**
 * EnableConfigServer - 开启Spring Cloud Config Server的注解。
 *  提供分布式配置中心服务端功能。
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }
}

4.搭建Config Client

Config Client对于Spring Cloud Config是客户端,对于Eureka来说可以是Applipythoncation Server 也可以是Application Client。

(1)导入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- spring cloud config分布式配置中心客户端依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

(2)编写配置文件

# 新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。

# 配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境

spring:

  application:

    name: Config-Client

  cloud:

    config: # spring cloud config 客户端配置

      uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888

      name: bjsxt # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application

      profile: default # 要读取的配置文件环境是什么,默认default

      label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

(3)服务接口

public interface ConfigClientService {
    String test();
}

(4)服务实现

@Service
public class ConfigClientServiceImpl implements ConfigClientService {
    @Value("${my.content}")
    private String content;
    @Override
    public String test() {
        System.out.println("content = " + content);
        return content;
    }
}

(5)编写控制器

@RestController
public class ConfigClientController {
    @Autowired
    private ConfigClientService configClientService;
    @RequestMapping("/test")
    public String test(){
        return configClientService.test();
    }
}

(6)编javascript写启动类

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

三、热刷新

(1)导入依赖(和优雅关闭的依赖一样)

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

(2)修改配置文件

management:

  endpoints:

    web:

      exposure:

        include: refresh,info,health

(3)修改服务实现(在类上添加@RefreshScope注解)

@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClwww.devze.comientService {
    @Value("${my.content}")
    private String content;
    @Override
    public String test() {
        System.out.println("content = " + content);
        return content;
    }
}

(4)测试热刷新

http://localhost:8080/actuator/refresh

四、Spring Cloud Bus(消息总线)

(1)导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 总线技术中的amqp相关依赖。 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

(2)修改配置文件

spring:

  rabbitmq:

    hos开发者_C培训t: 192.168.91.128

    username: bjsxt

    password: bjsxt

management:

  endpoints:

    enabled-by-default: true

    web:

      exposure:

        include: bu编程客栈s-refresh,info,health

(3)测试

http://localhost:8080/actuator/bus-refresh

到此这篇关于SpringCloud Config分布式配置中心使用教程介绍的文章就介绍到这了,更多相关Springcloud Config配置中心内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜