spring cloud 分布式配置(spring cloud config)

spring cloud config简介


    Spring Cloud Config就是我们通常意义上的配置中心。Spring Cloud Config-把应用原本放在本地文件的配置抽取出来放在中心服务器,本质是配置信息从本地迁移到云端。从而能够提供更好的管理、发布能力。
        Spring Cloud Config分服务端和客户端,服务端负责将git(svn)中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。

但客户端并不能主动感知到配置的变化,从而主动去获取新的配置,这需要每个客户端通过POST方法触发各自的/refresh。

 

操作图:

 

 

 

基本原理:

git 上存放我们的远程配置文件 
config-server 连接到 git 
config-client 连接到config-server 
当我们启动config-client 服务的时候,client 会通过连接的 config-server 拿到远程git 上面的配置文件,然后通过 Spring 加载到对象中。 

 

 

一个简单的案例

需要在github上创建一个项目  把连接数据的四要素(properties文件)放在该项目下    该配置文件分为三个阶段

jdbc-dev.properties======开发版  ip配置localhost
jdbc-test.properties======测试版  ip配置局域网
jdbc-pro.properties=======上线版  ip配置公网地址

 

一、配置服务端

    pom.xml中的配置

<!--继承springBoot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <!--继承springCloud-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--分布式配置中心服务端的核心配置-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>


  在resource中创建一个application.yml文件  配置如下

 

#设置该服务的端口
server:
  port: 8089

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yhl2574405253/config  #配置git所在的url
#          username:    配置git的用户名
#          password:    配置git的密码
  • spring.cloud.config.server.git.uri:配置git仓库位置

  • spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个

  • spring.cloud.config.server.git.username:访问git仓库的用户名

  • spring.cloud.config.server.git.password:访问git仓库的用户密码

注意:git如果没有加密,就不需要配置用户名跟密码

 


 添加一个启动服务端的类 需要加上一个 @EnableConfigServer  注解

 

package cn.et;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.config.server.EnableConfigServer;  
/*  
 * 启动分布式配置中心  读取远程git仓库配置文件  
 *   
 * 所有spring的配置中心的配置文件 都有以下几种格式组成  
 *   1 配置文件属于哪个应用的(Application)  
 *   2 配置文件属于哪个阶段(profile)     开发阶段   测试阶段   产品阶段  
 *   
 *   
 *   
 */  
@EnableConfigServer  
@SpringBootApplication  
public class Main {  
    public static void main(String[] args) {  
        SpringApplication.run(Main.class, args);  
    }  
}  


检查是否能正常使用

 

 

二、客户端的配置

 

    pom.xml配置

    <!--继承springBoot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <!--继承springCloud-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement> 
    <dependencies>
        <!--分布式配置中心客户端的核心配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency> 
    </dependencies>  

 

创建bootstrap.properties配置,来指定config server的路径

spring:
  cloud:
    config:
      uri: http://localhost:8089
      name: jdbc
      profile: dev

 

 

  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

上面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties

 

添加一个启动服务端的类

package cn.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 

 

添加一个测试类测试

package cn.et.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

public class Test {
    @Value("${username}")
    private String userName;

    @RequestMapping("test1")
    public String test1(){
        return userName;
    }

}

 

 

 

运行效果

 

这个时候就给远程的数据取到了,就可以操作你的具体业务了。想学习更多,请参考文档

中文文档:https://springcloud.cc/

官方文档:http://projects.spring.io/spring-cloud/#quick-start

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值