zoukankan      html  css  js  c++  java
  • 【Spring Cloud学习之五】配置中心

    环境
      eclipse 4.7
      jdk 1.8
      Spring Boot 1.5.2
      Spring Cloud 1.2

    一、什么是配置中心
    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库(或者SVN仓库)中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

    二、远程配置仓库
    配置文件可以选择Git、Svn、本地目录,GitHub、GitLab、gitee都是基于web的Git仓库,Spring Cloud默认使用Git,如果使用Svn需要引入额外依赖org.tmatesoft.svnkit。

    这里使用GitHub:
    新建仓库:config-repo
    上传配置文件:application-dev.yml       内容:version:0.0.1-SNAPSHOT


    三、搭建config-server
    1、新建maven工程


    2、pom.xml

    <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.wjy</groupId>
        <artifactId>config-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</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>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    </project>

    3、application.yml

    server:
        port: 8889
    spring:
        application:
            name: config-server
        cloud:
            config:
                label: master
                server:
                    git:
                        password:
                        searchPaths: config-repo
                        uri: https://github.com/cac2020/config-repo.git
                        username:

    4、启动类

    package com.wjy;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApp.class, args);
        }
    
    }

    5、测试验证
    查询配置中心:http://localhost:8889/config-repo/dev

    查询配置文件:http://localhost:8889/application-dev.yml

    四、搭建config-client

    1、新建maven工程


    2、pom.xml

    <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.wjy</groupId>
        <artifactId>config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.RC1</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>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    </project>

    3、bootstrap.yml   

    应用启动先读取bootstrap.yml  ,然后读取远程配置中心配置,然后再读取application.yml配置。

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

    注意点:yml配置文件冒号后面要跟一个空格。

    4、controller

    package com.wjy.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ConfigController {
        
        @Value("${version}")
        String versionstr;
        
        @RequestMapping(value = "/getVersion")
        public String getVersion () {
            return versionstr;
        }
    
    
    }

    启动类

    package com.wjy;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ConfigClientApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApp.class, args);
        }
    
    }

    5、测试验证

    http://localhost:8881/getVersion

    五、配置免重启自动刷新
    1、使用spring-boot-starter-actuator的refresh
    2、与github的webhook、svn的hook进行配合刷新;

    六、配置中心高可用
    将config-server、config-client都可以注册到注册中心作为微服务管理。


    参考:
    Config 配置中心与客户端的使用与详细
    Spring Cloud 配置中心的基本用法
    配置自动更新

  • 相关阅读:
    加载数据量大,页面卡死解决办法
    [存档]开启window7的隐藏功能虚拟wifi
    IIS发布Asp.Net网站注意事项
    [转载]总结几种C#窗体间通讯的处理方法
    调整和删除Win7休眠文件Hiberfil.sys的方法技巧,释放系统空间! ...
    [存档]Div+Css布局中经常使用的小技巧合集
    Android AndroidManifest.xml 结构详解
    Android权限详细说明
    Activity 生命周期详解
    程序员的文采
  • 原文地址:https://www.cnblogs.com/cac2020/p/11351544.html
Copyright © 2011-2022 走看看