zoukankan      html  css  js  c++  java
  • 第六篇:配置中心(Spring Cloud Config)

    为什么要用配置中心
    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
    Spring Cloud Config支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中,可以通过git修改。
    在Spring Cloud Config组件中,有两个是重要的服务,一是config server,二是config client。

    搭建Spring Cloud Config配置中心

    新创建一个Maven父工程,父工程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>
    
        <groupId>com.niuben</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <packaging>pom</packaging>
    
        <!--父依赖-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/>
        </parent>
    
        <!--属性-->
        <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>Finchley.RELEASE</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <!--版本管理-->
        <dependencyManagement>
            <dependencies>
                <!--springcloud-->
                <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>

    构建config-server

    新创建model,config-server工程

    加载pom文件

    !--Web依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--spring-cloud-config依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>

    编写配置文件application.properties

    spring.application.name=config-server
    server.port=8888
    
    # 配置git仓库地址
    spring.cloud.config.server.git.uri=https://gitee.com/niugit/test01/
    # 配置仓库路径
    spring.cloud.config.server.git.searchPaths=test
    # 配置仓库的分支
    spring.cloud.config.label=master
    # 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
    # 访问git仓库的用户名
    #spring.cloud.config.server.git.username=
    # 访问git仓库的用户密码
    #spring.cloud.config.server.git.password=
    • spring.cloud.config.server.git.uri:配置git仓库地址
    • spring.cloud.config.server.git.searchPaths:配置仓库路径
    • spring.cloud.config.label:配置仓库的分支
    • spring.cloud.config.server.git.username:访问git仓库的用户名
    • spring.cloud.config.server.git.password:访问git仓库的用户密码

    如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。

    在启动类加@EnableConfigServer注解,开启配置服务器

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

    在远程仓库https://gitee.com/niugit_admin/test01中有一个test文件,文件下有config-client-dev.properties配置文件,其中有两个属性

    foo=now is 100
    democonfigclient.message=hello spring cloud config!

    启动程序:访问http://localhost:8888/config-client/dev,会在页面出现

    {"name":"config-client","profiles":["dev"],"label":null,"version":"441761abcdd8e01c6f063cc873857728db333bce","state":null,"propertySources":[{"name":"https://gitee.com/niugit/test01//test/config-client-dev.properties","source":{"foo":"now is 100","democonfigclient.message":"hello spring cloud config!"}}]}

    表明配置服务中心可以从远程程序获取配置信息了

    注:http请求地址和资源文件映射如下

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

    构建config-client

    创建新model,config-client工程

     <!--Web依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--spring-cloud-starter-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>

    编写bootstrap.properties配置文件

    spring.application.name=config-client
    # 远程仓库的分支
    spring.cloud.config.label=master
    # dev开发环境配置文件,test测试环境,pro正式环境
    spring.cloud.config.profile=dev
    # 配置服务中心的网址
    spring.cloud.config.uri= http://localhost:8888/
    server.port=8881

    创建controller层,TestClient类

    @RestController
    public class TestClient {
    
        @Value("${foo}")
        String foo;
    
        @RequestMapping(value = "/test")
        public String hi() {
            return foo;
        }
    }

    启动启动类,访问:http://localhost:8881/test,出现

    now is 100

    表明config-client从config-server获取了foo的属性,而config-server是从git仓库读取的

    源码:https://gitee.com/niugit_admin/spring-cloud-finchley

  • 相关阅读:
    linux之sed用法
    vim 设置tab空格个数
    centos 7远程登陆win10
    linux find命令学习
    CENTOS 7 修改默认启动内核
    Centos7更改默认启动模式
    centos 7创建桌面快捷方式
    修改centos中文为英文显示
    正则的sub
    超时或错误重试
  • 原文地址:https://www.cnblogs.com/niudaben/p/12436857.html
Copyright © 2011-2022 走看看