iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringCloudConfig分布式配置中心使用教程介绍
  • 665
分享到

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

SpringcloudConfig配置中心SpringcloudConfig分布式配置中心 2022-12-09 12:12:50 665人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、简介二、使用三、热刷新四、spring cloud Bus(消息总线)一、简介 spring Cloud Config为分布式系统中的配置提供服务器端和客户端支持。可以集中管

一、简介

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>
<dependencies>
    <!-- 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)编写启动类


@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来说可以是Application 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)编写启动类

@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 ConfigClientService {
    @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:
    host: 192.168.91.128
    username: bjsxt
    password: bjsxt
management:
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: bus-refresh,info,health

(3)测试

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

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

--结束END--

本文标题: SpringCloudConfig分布式配置中心使用教程介绍

本文链接: https://www.lsjlt.com/news/174657.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作