最近在看SpringCloud,为了帮助自己学习和记忆,所以写下这篇文章。

从SpringCloud官方文档上看SpringCloudConfig其实为我们提供配置外部化的一个服务,可以理解成就是个web服务,我们通过请求到web服务上获取我们所需要的配置文件。

我们首先在github上创建一个项目,然后创建几个配置文件

我创建了3个配置文件,分别对应开发,测试和生产三个环境,

我们配置文件建好之后,开始创建SpringCloudConfig服务

我先放一下我的项目结构,不然pom.xml有些小伙伴可能不理解

my-spring-cloud就是最外层项目的pom.xml

<?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>jipeng.com</groupId>
<artifactId>my_spring_cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>my_spring_cloud_config_service</module>
<module>my_spring_cloud_config_client</module>
</modules> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <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>Edgware.SR3</spring-cloud.version>
</properties> <dependencyManagement>
<dependencies>
<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> <repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories> <name>my_spring_cloud</name>
</project>

这里我们比正常的springboot项目多了一个 spring-cloud-dependencies

这是SpringCloud主包,后面我们SpringCloudConfig的包需要依赖这个,所以版本问题大家需要注意。

下面看下我们my_spring_cloud_config_service项目的pom.xml
<?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">
<parent>
<artifactId>my_spring_cloud</artifactId>
<groupId>jipeng.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>my_spring_cloud_config_service</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies> </project>

很简单比我们正常的web项目多了一个  spring-cloud-config-server

下面再看下我们 spring-cloud-config-server 的application.properties 文件

#服务端口
server.port=8091
#服务名称
spring.application.name=configService #服务对应的git地址,就是你git项目地址,你clone的那个地址
spring.cloud.config.server.git.uri=https://github.com/xxx/xxx.git
#git仓库地址下的相对地址,可以配置多个,用,分割。
spring.cloud.config.server.git.search-paths=/**
#配置文件所在的分支
spring.cloud.config.label=master
#git仓库用户名
spring.cloud.config.username=xxx
#git仓库密码
spring.cloud.config.password=xxx

我们创建一个 ConfigServiceApplication 代码如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; /**
* @Author: jipeng
* @Description:
* @Date: Created in 2018/6/7 7:54
*/
@EnableConfigServer
@SpringBootApplication
public class ConfigServiceApplication { public static void main(String[] args){
SpringApplication.run(ConfigServiceApplication.class,args); }
}

比正常的web项目多了一个 EnableConfigServer 注解,这是一个SpringCloud注解,有了这个注解及时告诉项目这是一个配置服务。好了,到此我们的配置服务已经好了,我们可以启动项目通过postman来检验下我们的成果

看下我们的请求地址  http://localhost:8091/configTestService/dev

再看下github中的配置文件

大家是不是发现了什么

/configTestService/dev   就是根据我们github中的配置文件名来的  configTestService 为我们的应用名称   dev 为 环境

现在config服务端搭好了,我们搭建客户端

客户端的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">
<parent>
<artifactId>my_spring_cloud</artifactId>
<groupId>jipeng.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>my_spring_cloud_config_client</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies> <build>
<finalName>my_spring_cloud_config_client</finalName>
</build>
</project>

比正常的web多了一个 spring-cloud-starter-config

客户端的application.properties 就两个配置

server.port:8081
spring.applicaton.name=configclient

客户端多了一个bootstrap的配置文件,里面配置的是SpringCloudConfig的相关配置

#配置应用名称
spring.cloud.config.name=configTestService
#配置文件的版本
spring.cloud.config.profile=test
#配置服务的地址
spring.cloud.config.uri=http://localhost:8091/
#配置文件所在的分支
spring.cloud.config.label=master

创建一个 WebApplication ,代码如下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @Author: jipeng
* @Description:
* @Date: Created in 2018/6/8 7:03
*/
@SpringBootApplication
public class WebApplication {
public static void main(String[] args){
SpringApplication.run(WebApplication.class,args);
}
}

我们创建一个 TestController

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Author: jipeng
* @Description:
* @Date: Created in 2018/6/8 7:52
*/
@RestController
public class TestController {
@Value("${configtest.version}")
private String version; @RequestMapping("/test")
public String from() {
return this.version;
}
}

都加好了我们启动项目,留意下启动日记,就是启动日记刚开始打印的时候

2018-06-10 21:45:07.420  INFO 8376 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8091/
2018-06-10 21:45:11.347 INFO 8376 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=configTestService, profiles=[dev], label=master, version=9d66adb8d2b7ace9e4933177051b167a7ef49c1a, state=null
2018-06-10 21:45:11.348 INFO 8376 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/jploveslife/springcloudtest.git/configTestService-dev.properties'}]]
2018-06-10 21:45:11.352 INFO 8376 --- [ main] com.ji.WebApplication : No active profile set, falling back to default profiles: default
2018-06-10 21:45:11.369 INFO 8376 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3a5ecce3: startup date [Sun Jun 10 21:45:11 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@10db82ae
2018-06-10 21:45:11.855 INFO 8376 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=0537ae00-a03a-35d0-830a-1277fd4d74d0
2018-06-10 21:45:11.913 INFO 8376 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$4fec7b02] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

我会发现刚启动的时候是使用我们的配置服务去获取的配置文件,这里面的原理下次讲

我们来通过postman验证下,看配置有没有过来,我们请求test接口

返回1.0.0 和我们配置文件中一直,说明配置生效了。

到此就结束了,没讲多少,都是直接上的代码,希望这个文章对大家了解SpringCloudConfig有所帮助。实际生产中比这个复杂,大家可以通过不同的场景搭建不同的配置服务。

基于Github搭建SrpingCloudConfig详解的更多相关文章

  1. 大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 图文详解

    引言 在之前的大数据学习系列中,搭建了Hadoop+Spark+HBase+Hive 环境以及一些测试.其实要说的话,我开始学习大数据的时候,搭建的就是集群,并不是单机模式和伪分布式.至于为什么先写单 ...

  2. 基于模型的特征选择详解 (Embedded & Wrapper)

    目录 基于模型的特征选择详解 (Embedded & Wrapper) 1. 线性模型和正则化(Embedded方式) 2. 基于树模型的特征选择(Embedded方式) 3. 顶层特征选择算 ...

  3. spring mvc 框架搭建及详解

    现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...

  4. Mybatis简介、环境搭建和详解

    简介: 1.Mybatis  开源免费框架,原名叫iBatis,2010在google code,2013年迁移到github 2.作用: 数据访问层框架 2.1  底层是对JDBC的封装 3.myb ...

  5. Spring MVC框架搭建及其详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解

    当网站访问量达到一定时,如何做Memcached集群,又如何高可用,是接下来要讨论的问题. 有这么一段文字来描述“Memcached集群” Memcached如何处理容错的? 不处理!:) 在memc ...

  7. Git学习系列之如何正确且高效地将本地项目上传到Github(图文详解)

    不多说,直接上干货! 首先你需要一个Github账号,所以还没有的话先去注册吧! https://github.com/ 见 如何走上更高平台分享传递干货知识:(开通个人Github面向开源及私有软件 ...

  8. 实战!基于lamp安装wordpress详解-技术流ken

    简介 LAMP 是Linux Apache MySQL PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行动态的脚本文件.现在基于lamp搭建wor ...

  9. Java基于回调的观察者模式详解

    本文由“言念小文”原创,转载请说明文章出处 一.前言 什么是回调?回调如何使用?如何优雅的使用?本文将首先详解回调的原理,然后介绍回调的基本使用方法,最后介绍基于回调的“观察者模式”实现,演示如何优化 ...

随机推荐

  1. 【Oracle 12c】CUUG OCP认证071考试原题解析(29)

    29.choose the best answer Evaluate the following query: SQL> SELECT promo_name || q'{'s start dat ...

  2. 【OCP-12c】CUUG最新考试原题整理及答案(071-12)

    12.(5-12)choose two:Examine the data in the CUSTOMERS table:You want to list all cities that have mo ...

  3. OC中NSClassFromString()与NSStringFromClass()的用法及应用场景

    1.NSClassFromString()利用一个字符串创建一个类,我是在标签控制器中 UITabBarController中创建它的子控制器中使用的 - (void)viewDidLoad { [s ...

  4. static修饰的方法不能被重写可以被继承

    今天我们谈谈为什么抽象类中不能有静态的抽象方法以及static修饰的方法不能被重写可以被继承 1 static修饰的方法不能被重写可以被继承我们知道static修饰的方法为静态方法,可以直接使用类名. ...

  5. Word2007文档中怎么输入上标下标

    1.Word中输出Z = X2 + Y2 此公式流程: 首先在Word中写入:Z = X2 + Y2: 方法1:选中X后面的2,再按组合键“Ctrl+Shift+加号键”即可,如此操作Y后面的2即可.

  6. [Objective-C语言教程]扩展(30)

    类扩展与类别有一些相似之处,但它只能添加到编译时具有源代码的类中(类与类扩展同时编译). 类扩展声明的方法是在原始类的实现块中实现的,因此不能在框架类上声明类扩展,例如Cocoa或Cocoa Touc ...

  7. gradle 常用命令参数

    参考文档 https://docs.gradle.org/current/userguide/userguide gradle -q 任务名(task ) 执行指定某一个task gradle -q ...

  8. JavaBean简介及使用

    JavaBean简介及使用  望星空 https://blog.csdn.net/joywy/article/details/7773503   一.JavaBean简介 JavaBean是使用Jav ...

  9. solr 服务搭建

    1.  linux 中 安装jdk,  tomcat, 2. 下载 solr-4.10.3.tgz.tgz 并解压 解压后文件夹:solr-4.10.3 3. 将 solr-4.10.3/dist 下 ...

  10. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498 ...