码迷,mamicode.com
首页 > 编程语言 > 详细

【Spring Cloud学习之五】配置中心

时间:2019-08-14 14:13:34      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:get   管理   run   查询   环境   用法   上传   tee   ase   

环境
  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、使用spring-boot-starter-actuator的refresh
2、与github的webhook、svn的hook进行配合刷新;

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


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

 

【Spring Cloud学习之五】配置中心

标签:get   管理   run   查询   环境   用法   上传   tee   ase   

原文地址:https://www.cnblogs.com/cac2020/p/11351544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!