基于Github搭建SrpingCloudConfig详解
最近在看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详解的更多相关文章
- 大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 图文详解
引言 在之前的大数据学习系列中,搭建了Hadoop+Spark+HBase+Hive 环境以及一些测试.其实要说的话,我开始学习大数据的时候,搭建的就是集群,并不是单机模式和伪分布式.至于为什么先写单 ...
- 基于模型的特征选择详解 (Embedded & Wrapper)
目录 基于模型的特征选择详解 (Embedded & Wrapper) 1. 线性模型和正则化(Embedded方式) 2. 基于树模型的特征选择(Embedded方式) 3. 顶层特征选择算 ...
- spring mvc 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...
- Mybatis简介、环境搭建和详解
简介: 1.Mybatis 开源免费框架,原名叫iBatis,2010在google code,2013年迁移到github 2.作用: 数据访问层框架 2.1 底层是对JDBC的封装 3.myb ...
- Spring MVC框架搭建及其详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解
当网站访问量达到一定时,如何做Memcached集群,又如何高可用,是接下来要讨论的问题. 有这么一段文字来描述“Memcached集群” Memcached如何处理容错的? 不处理!:) 在memc ...
- Git学习系列之如何正确且高效地将本地项目上传到Github(图文详解)
不多说,直接上干货! 首先你需要一个Github账号,所以还没有的话先去注册吧! https://github.com/ 见 如何走上更高平台分享传递干货知识:(开通个人Github面向开源及私有软件 ...
- 实战!基于lamp安装wordpress详解-技术流ken
简介 LAMP 是Linux Apache MySQL PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行动态的脚本文件.现在基于lamp搭建wor ...
- Java基于回调的观察者模式详解
本文由“言念小文”原创,转载请说明文章出处 一.前言 什么是回调?回调如何使用?如何优雅的使用?本文将首先详解回调的原理,然后介绍回调的基本使用方法,最后介绍基于回调的“观察者模式”实现,演示如何优化 ...
随机推荐
- “全栈2019”Java第八十九章:接口中能定义内部类吗?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Java中 IO类 - File类中的判断功能
package com.hxzy.IOSer;import java.io.*;public class Demo05 { public static void main(String[] args) ...
- java下载文件注意点
前台: 不建议使用ajax,可以使用window.location.href 后台: 三个参数--> response path filename--filename如果要防止乱码,可以用Str ...
- BZOJ4283: 魔法少女伊莉雅(最短路径图+最短路径树)
题面 传送门 题解 太长了不想写了→_→ 题解 //minamoto #include<bits/stdc++.h> #define R register #define inf 0x3f ...
- php请求远程url内容方法
php请求远程url内容有两个方法fopen/file_get_contents和curl. 1,fopen/file_get_contents与curl的差异 (1)fopen /file_get_ ...
- Elasticsearch(八)【NEST高级客户端--分析器】
分析 分析是将文本(如任何电子邮件的正文)转换为添加到反向索引中进行搜索的tokens或terms的过程. 分析由analyzer执行,分析器可以是内置分析器或每个索引定义的定制分析器. 书写分析器测 ...
- HTML5语义化标签总结
1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> < ...
- 【算法笔记】A1039 Course List for Student
https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416 题意: 有N个学生,K节课.给出选择每门课的 ...
- 01背包-记忆化搜索到成型的DP
记忆化搜索 #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,W; int dp[105][ ...
- vs2010启动越来越慢解决方法
自己用的电脑中vs2010启动总是越来越慢,耽误时间,用了下面的方法试了一下,效果还不错,如果你的vs2010也是,遇到这种问题不妨试一试: 1.重新设置了vs2010的环境(在vs2010命令提示符 ...