【springCloud基础篇-9】Spring Cloud Config 配置中心之服务端搭建及讲解和多种搭建方式

demo代码地址:https://download.csdn.net/download/qq_33333654/12014918

环境:

jdk1.8

maven3.0

idea

springboot1.5.3

首先需要启动自己的注册中心,具体可参考我之前的注册中心高可用博客:

https://blog.csdn.net/qq_33333654/article/details/102636388

在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。

本片文章中的项目使用的是本地模式创建服务端,也会讲解下使用其他的模式。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git。真实项目中还是建议使用git

接下来创建一个普通的springboot项目,这里我使用了1.5.3的版本,如果使用2.0+的时候,配置文件注解会找不到,具体原因后续会研究,喜欢的人也可以自行研究。

项目创建完之后修改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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 注意三个部分:

注册中心依赖

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

springcloud环境

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

以及本篇文章必不可少的maven依赖,也是springCloudConfig的核心依赖:

 

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

 接下来是在启动类上添加注解:

@EnableConfigServer

然后修改配置文件:

server:
  port: 9020
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/
spring:
  application:
    name: spring-cloud-config-server
  profiles:
    active: native


 讲解:

9020是端口好,eureka下是我的注册中心,spring-cloud-config-server是项目名称也是注册中心中的名称,

profiles:
  active: native

这个就比较特殊了,这个是代表config扫描的设置,这个是设置扫描项目本地路径,也就是resources文件夹下的配置文件。

当然还有其他两种。

Spring Cloud Config提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=nativeConfig Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

git配置的例子:

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git仓库的地址
          search-paths: config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username:                                             # git仓库的账号
          password:                                             # git仓库的密码

 补充下,还有一个lable属性,这个属性可以设置git的分支

 

绝对路径的配置例子:

#配置中心端口
server.port=8887

spring.application.name=config-server

logging.config=classpath:log4j2.xml

 

spring.profiles.active=native

#申明本地配置文件的存放位置

spring.cloud.config.server.native.searchLocations=file:D:\\etc\\native

#注册中心

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

 这个是网上找的,设置绝对路径的时候,可以在D盘创建多个文件,如图:

 

三种模式都讲解了。言归正传,我这次试用native.

Pom文件修改完之后,我们在resources文件夹下创建一个配置文件名字为:native-mysql.properties

配置文件内容为: native.hello=hello_i_im_mysql

接下启动项目。

访问路径:

http://localhost:9020/native/mysql

输出内容如下:

{
    "name": "native",
    "profiles": ["mysql"],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [{
        "name": "classpath:/native-mysql.properties",
        "source": {
            "native.hello": "hello_i_im_mysql"
        }
    }]
}

配图:

从返回的json文件上我们应该就能看出,本次访问的是native空间下mysql文件,配置文件路径为本地路径classpath,配置文件的内容就是souorce的内容。

同样的道理,如果你创建一个native-test.properties文件,再次访问:

http://localhost:9020/native/test 观察下效果。

由图中我们可看出,里面所有的文件的命名规则都遵循:{application}-{profile}.properties

所以我们客户端bootstrap.properties文件中的spring.cloud.config.name=native参数,实际上是为了匹配到这些配置文件的{application}前缀

那么其他两种方式呢?是否一样呢? 建议亲自试一下,这里简单说明下规则:

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

这个就是对应的config访问的规则。

到此,springCloudConfig的服务端创建完成

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不能吃辣的JAVA程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值