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

搭建spring cloud config

时间:2016-12-12 23:23:17      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:imp   产品   为我   需要   code   mave   针对   客户端   master   

很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了。

Spring Cloud简介

Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

Spring Cloud Config 配置管理组件

不知道spring cloud之前,本来想自己开发一个配置管理服务器。后来了解到spring cloud,接触到spring cloud config,知道其作为配置管理,可以使用git、svn和本地文件读取的三种方式,结合我们正在使用的git非常好,天生具备了配置修改日志记录、回滚和适合给运维工程师使用的特点。于是果断学习spring cloud config用来作为我们的配置管理服务器。

其优点:1. 能够记录配置文件的change log

    2. 能够快速回滚

    3. 修改配置后可通知配置使用端更新配置

    4. 如果是使用内置tomcat,重启tomcat也是可行的

    5. 搭建服务简单快捷

下面简单介绍下如何快速搭建配置服务:

首先是启动类:加上@EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
public class CloudServerDemoApplication {

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

其次是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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>your.groupId</groupId>
	<artifactId>your.artifactId</artifactId>
	<version>your.version</version>
	<packaging>war</packaging>

	<name>cloud-server-demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.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>
		<tomcat.version>8.5.5</tomcat.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-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR2</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>


</project>

  注意点:1. 如果想使用内置tomcat启动,我当前使用的boot版本为1.4.2,其内置tomcat版本为8.5.6, 8.5.6的tomcat有bug,因此要改为8.5.5.

      2. 如果想调用/refresh等接口接口,需要引入spring-boot-starter-actuator依赖。如果报404,请先确定是用post请求。

application.properties文件

讲配置之前先说明下git中文件名和git分支对应请求路径

HTTP服务资源的构成:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application是SpringApplicationspring.config.name,(一般来说‘application‘是一个常规的Spring Boot应用),profile是一个active的profile(或者逗号分隔的属性列表),label是一个可选的git标签(默认为"master")。这几项值都对应了配置服务消费工程中的配置,后面的文章会讲。

 

配置服务中最基本的一项配置为指定git:

第一种:(最基础的,指定之后可以使用最简单的配置服务了)

spring.cloud.config.server.git.uri

在正常环境中,配置服务给多个项目提供服务,git中都放一起就会很乱,配置服务提供了几种区分各个项目的配置:

第二种:

spring.cloud.config.server.git.uri: https://github.com/spring-cloud-samples/config-repo
spring.cloud.config.server.git.repos.simple: https://github.com/simple/config-repo
spring.cloud.config.server.git.repos.special.pattern: special*/dev*,*special*/dev*
spring.cloud.config.server.git.repos.special.uri: https://github.com/special/config-repo
spring.cloud.config.server.git.repos.local.pattern: local*
spring.cloud.config.server.git.repos.local.uri: file:/home/configsvc/config-repo

如果 {application}/{profile}不能匹配任何表达式,那么将使用"spring.cloud.config.server.git.uri"对应的值。在上述配置中,对于 "simple" 配置库,匹配模式是simple/* (也就说,无论profile是什么,它只匹配application名称为"simple"应用系统)。"local"库匹配所有application名称以"local"开头任何应用系统,不管profiles是什么(因没有配置对profile的匹配规则,/*后缀会被自动的增加到任何的匹配表达式中 ),special配置库只能匹配profiles为dev*的,大家应该看懂了,simple、special、local可以自己配置。

第三种:

spring.cloud.config.server.git.uri=https://github.com/vincent-ren/spring-boot-profile.git
spring.cloud.config.server.git.searchPaths={application}

git地址是我的真实使用过的地址(可看),配置此项”spring.cloud.config.server.git.searchPaths={application}“,配置客户端获取配置会根据自己设置的的application名去git仓库中根目录下的对应/{application}文件夹去寻找配置。

 

作为配置服务安全问题一定会很重要。

下篇文章接着说配置服务的安全问题。

 

搭建spring cloud config

标签:imp   产品   为我   需要   code   mave   针对   客户端   master   

原文地址:http://www.cnblogs.com/vincent0928/p/6146901.html

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