spring-cloud-config的使用教程-详细步骤

一、  分布式配置中心的简介:

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在本地,也支持放在远程Git仓库中。本次学习是将配置中心放于github上进行学习。在spring cloud config 组件中,分两个角色,一是configserver,二是config client。

1.1配置中心可以按不同环境进行分别配置,

   1.开发环境:dev

   2.测试环境:test

   3.正式环境:pro

   以下学习,我使用的为dev环境学习

二、  构建spring config server:

2.1:首先创建一个spring-boot的项目,在pom文件中加入以下依赖。

<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>cn.sun</groupId>
  <artifactId>eurekaTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <dependencyManagement>
    <dependencies>
    	<dependency>
  		  	<groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		    <version>1.3.0.RELEASE</version>
		</dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
 </dependencyManagement>
 <dependencies>
 	  <!--表示为web工程-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-config</artifactId>
        </dependency>
 </dependencies>
	
</project>

2.2:创建一个springboot的启动类,必须要加入@enableConfigServer这个注解

package eurekaTest.cn.sun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


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

2.3:创建application.properties;配置如下:

server.port=8010
spring.cloud.config.server.default-application-name=config-server

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/WjianGang/testGit.git
spring.cloud.config.server.git.searchPaths=config-client-dev
# 访问git仓库的用户名
spring.cloud.config.server.git.username=xxxxoooo
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=xxxxoooo

在geihub的远程仓库https://github.com/WjianGang/testGit中的文件config.properties配置了一个属性


三:启动spring config server程序

启动好之后,访问下面地址:http://localhost:8010/configTest/dev,就可以看到这样的一串Json数据,“{"name":"configTest","profiles":["dev"],"label":null,"version":"456cba44e9d058ee044f42074c47ad3c3e6c9b80","state":null,"propertySources":[]}”,证明服务中心可以从远程程序获取配置信息

四:构建一个configclient

    4.1重新创建一个springboot项目,取名为config-client,pom文件引入依赖:

      
<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>cn.sun</groupId>
  <artifactId>eurekaTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <dependencyManagement>
    <dependencies>
    	<dependency>
  	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-devtools</artifactId>
	    <version>1.3.0.RELEASE</version>
	</dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
 </dependencyManagement>
 <dependencies>
 	  <!--表示为web工程-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 </dependencies>
	
</project>

4.2bootstrop.properties文件的配置内容为下:

    

# 和git里的文件名对应
spring.application.name=config-client
# dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
# 指明配置服务中心的网址
spring.cloud.config.uri=http://localhost:8010
#配置此服务的端口号
server.port=8020

4.3:创建一个启动类

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

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


4.4:创建一个controller

package controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RefreshScope//指示Config客户端在服务器参数刷新时,也刷新注入的属性值
public class clientController {
		
	 @Value("${configTest}")//获取到GitHub中的配置文件里的key
	 private String configTest;
	
	 @RequestMapping(value = "/helloConfig")
	 public String helloConfig(){
	      return configTest;
	 }
}


五:最后先把之前的config-server项目启动起来,然后再启动config-client项目,使用http://localhost:8020/helloConfig访问,如果访问成功,且能看到controller中所获取的value,说明功能实现了。

六:特别注意,在config-client中必须使用的是bootstorp.properties(使用application.properties会获取不到属性)这个配置文件来对github中的配置进行访问,否则会报错,无法获取到github上的属性值。

 

 

猜你喜欢

转载自blog.csdn.net/WuJiangang5112/article/details/80281723
今日推荐