SpingCloud踩坑记(二)SpringCloud配置中心

springCloud配置中心

官网介绍如下

Spring Cloud Config provides server and client-side support for externalized configuration in a 
distributed system. With the Config Server you have a central place to manage external properties 
for applications across all environments. The concepts on both client and server map identically 
to the Spring Environment and PropertySource abstractions, so they fit very well with Spring 
applications, but can be used with any application running in any language. 
As an application moves through the deployment pipeline from dev to test and into 
production you can manage the configuration between those environments and be certain that applications 
have everything they need to run when they migrate. The default implementation of the server storage 
backend uses git so it easily supports labelled versions of configuration environments, as well as 
being accessible to a wide range of tooling for managing the content. It is easy to add alternative 
implementations and plug them in with Spring configuration.

简单来说:springCloud config项目,用来为分布式的微服务系统中提供集成式外部配置支持,分为客户端和服务端。并且通过配置服务中心集中配置不同环境的变量,方便管理和配置迁移。

大致流程如下

201912011435

涉及到三个角色:

配置中心服务端:为配置客户端提供对应的配置信息,配置信息的来源是配置仓库。应用启动时,会从配置仓库拉取配置信息缓存到本地仓库中。

配置中心客户端:应用启动时从配置服务端拉取配置信息。

配置仓库:为配置中心服务端提供配置信息存储,Spring Cloud Config 默认是使用git作为仓库的

本文配置仓库为本地文件服务为例

服务端实现

pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.smallstep</groupId>
    <artifactId>spring-cloud-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-config</name>
    <description>Demo project for spring-cloud-config</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动类增加@EnableConfigServer注解

增加@EnableConfigServer注解启动配置服务功能

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigApplication {

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

}

application.properties配置

server.port=9999

spring.cloud.config.server.native.search-locations=classpath:/properties/
spring.application.name=service-config
spring.profiles.active=native

配置说明如下

spring.profiles.active:为配置仓库的类型,本文以本地文件为例

spring.application.name:服务的名称

spring.cloud.config.server.native.search-locations:为配置路径地址

配置文件资源说明

资源形式如下

/ {application} / {profile} [/ {label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中application注入作为spring.config.name在SpringApplication(什么是通常application以规则的弹簧引导应用程序),profile是一个有效简表(或逗号分隔的属性列表),并且label是一个可选的标签(默认为master)。

服务端完整目录

例如配置一个application为FFBoot的开发环境完整服务端工程目录如下:

201912111454

FFBoot-dev.properties文件内增加如下内容

test=IT_LiGe

配置完成启动服务!

客户端实现

pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.smallstep</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>


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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类


@SpringBootApplication
@RestController
public class SpringCloudConfigApplication {

    @Value("${test}")
    public String test;

    @RequestMapping("/")
    public String home() {
        return test;
    }

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

}

application.properties配置

server.port=9998

bootstrap.properties 配置

spring.cloud.config.uri=http://127.0.0.1:9999/
spring.cloud.config.label=master
spring.application.name=FFBoot
spring.profiles.active=dev

配置说明如下

spring.cloud.config.uri:为配置服务方法地址

spring.cloud.config.label:为资源的label

spring.application.name:为资源的application

spring.profiles.active:为资源的profiles

启动服务

启动服务,访问 http://127.0.0.1:9998/ ,界面显示IT_LiGe即完成客户端配置

延伸思考

配置修改自动刷新问题?

解决方案:可根据消息机制通知客户端,客户端获取消息后主动拉取新配置。

源码地址:https://gitee.com/LeeJunProject/spring_cloud_learning/tree/master/config

END

欢迎扫描下图关注公众号 IT李哥,公众号经常推送一些优质的技术文章

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值