天天看点

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

Spring-Cloud组件之配置中心Config

  • Config是什么?
  • config入门
  • config访问配置文件规则
  • 客户端通过config访问配置文件
  • config高可用

Config是什么?

一个配置文件管理中心。我们一个项目包含很多个微服务,如果某一个微服务改动后,其他微服务的配置文件也得跟着改,那么我们得该多少配置文件,改了之后还得重新部署项目。想想都可怕,但是我们把配置文件交给config-server处理那就好很多了,我们更改就直接改配置文件就行,其他都不用动了。

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

config入门

新建一个config-server项目。

pom.xml:

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config_server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
           

配置文件application.yml:

server:
  port: 7000

spring:
  application:
    name: config-server #此实例name
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/up_gujunjie/config.git #配置文件在码云上的地址
          earch-paths: '*'  #Configserver会在 Git仓库所有目录中查找配置文件。
          clone-on-start: true #启动时就clone仓库到本地,默认是在配置被首次请求时,config server才会clone git仓库
          #native:
            #search-locations: classpath:/config #若配置中心在本地,本地的地址
           

我的这个仓库是公开了的,所有不需要账户密码,如果是私有仓库,需要在uri 同级写上username 和passowrd。

启动类:

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

我们看看码云上的仓库:

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用
Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

我这里是把user的这个pom文件拷贝过来的。

项目现在先启动吧。但是先看完下面这个内容后再去访问。

config访问配置文件规则

config访问配置文件,是需要一个具体的访问规则的,我们可以在官网找到:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

application是配置文件的名字,我们这里就是叫test

profile 就是对应的环境,就是你定义的 spring.application.name: user,我们这里是user

label就是不同的分支 默认为master。我们这里就是master分支。

对于yml 和properties类型config可以完美转换, 也就是说你存的是yml 但是可以读取为properties类型的反过来也是如此

既然我们知道如何访问配置文件了,我们先来试一试吧:

我这里采用的是第二种方式访问的,至于其他访问方式你们可以自行测试。

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

这就说明我们的config-server成功了。

这里只是服务端配置,我们接着看一下服务端吧:

客户端通过config访问配置文件

新建一个config-server-test项目

pom.xml:

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server-test</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
           

eureka目前是没有用的,只是先放进来,过会儿做集群用的。

配置文件application.yml:

spring:
  application:
    name: test-server #此实例名称
           

但是这里我们需要在application.yml同级下面新建一个bootstrap.yml文件。

简单聊一聊,spring cloud有一个“引导上下文"的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml或 properties)中的属性不同,引导上下文加载(bootstrap.)中的属性。配置在 bootstrap.*中的属性有更高的优先级,因此默认情况下它们不能被本地配置

spring:
  cloud:
    config:
      name: test #这是我们要读取的配置文件名
      profile: user #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      uri: http://localhost:7000/ #这就是我们config server的一个地址
           

为了方便测试,写一个controller

@RestController
public class TestController {

    @RequestMapping("/test")
    public String test(){
        return "我是微服务配置中心测试项目";
    }
}
           

在写一个简单的启动类

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

好了,我们启动一下,我们在控制台可以看见:

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

等启动成功后我们访问一下我们写的测试接口

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

这就证明我们这个客户端读取到配置文件了。

还记得我们开始的图吗,就是微服务B,微服务C共同配置文件。这个是怎么弄的呢?

Spring-Cloud组件之配置中心ConfigConfig是什么?config入门config访问配置文件规则客户端通过config访问配置文件config高可用

这种格式写,然后我们调整一下config-server-test的bootstrap.yml

spring:
  cloud:
    config:
      name: config #这是我们要读取的配置文件名
      profile: dev #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      uri: http://localhost:7000/ #这就是我们config server的一个地址
           

重启一下,就发现我们的端口变为8082了。就这么简单咯,你如果要8081端口,把 profile这个值改为prd就可以了。

config高可用

这里演示的是通过eureka来达到config高可用的效果,还可以通过nginx等其他方式,这里不做演示。

改一下config-server的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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config_server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
           

配置文件:

server:
  port: 7000

spring:
  application:
    name: config-server #此实例name
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/up_gujunjie/config.git
#          uri: https://github.com/513667225/my-spring-cloud-config.git
          #配置文件在github上的地址
          earch-paths: '*'  #Configserver会在 Git仓库根目录、 foo子目录,以及所有以 bar开始的子目录中查找配置文件。
          clone-on-start: true #启动时就clone仓库到本地,默认是在配置被首次请求时,config server才会clone git仓库
          #native:
            #search-locations: classpath:/config #若配置中心在本地,本地的地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka3000.com:3000/eureka #Eureka服务端提供的注册地址

  instance:
    instance-id: config-server #此实例注册到Eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP
    lease-renewal-interval-in-seconds: 10 #Eureka 客户端需要多长时间发送心跳给Eyreka服务器,表明他还活着 ,默认为30 秒 (与下面配置的单位都是秒)
    lease-expiration-duration-in-seconds: 30 #Eureka 服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
           

然后在启动类上加上@EnableEurekaClient。

拷贝一下config-server项目叫做config-server7001,并把端口改为7001。

然后改一下config-server-test项目的配置文件bootstrap.yml:

spring:
  cloud:
    config:
      name: config #这是我们要读取的配置文件名
      profile: dev #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
#      uri: http://localhost:7000/ #这就是我们config server的一个地址
      discovery:
        enabled: true
        service-id: CONFIG-SERVER

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:3000/eureka/
           

差不多就这样啦,启动一下,其实本机也看不到什么效果。但这个就是config的高可用,就是通过eureka服务注册中心实现的。

继续阅读