为什么用spring cloud config 写道
一个Application中不只是代码,还需要连接资源和其它应用,经常有很多需要外部设置的项去调整Application行为,如切换不同的数据库,i18n国际化 等.应用中的会经常见到的xml,properties,yaml等就是配置信息.
常见的实现信息配置的方法:
● 硬编码(缺点:需要修改代码,风险大)
● 放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
● 文件系统中(缺点:依赖操作系统等)
● 环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
● 云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
常见的实现信息配置的方法:
● 硬编码(缺点:需要修改代码,风险大)
● 放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
● 文件系统中(缺点:依赖操作系统等)
● 环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
● 云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
真正的数据存在Git等repository中,Config Server去获取相应的信息,然后开发给Client Application,相互间的通信基于HTTP,TCP,UDP等协议.微服务框架落地云平台,如果配置文本地化,将来多实例化后,极难维护。
// =================配置仓库(git/svn/native)=================
属性文件属名
格式:name-profile.properties/yml,例如:
vs-dev.properties
vs-test.properties
vs-prod.properties
仓库创建
略
// =================MAVEN(项目构建)=================
服务端
<?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> <artifactId>config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>1.7</java.version> <project.encoding>UTF-8</project.encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.5.RELEASE</spring.version> </properties> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <relativePath/> </parent> <dependencies> <!--spring cloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- SVN Repos --> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <optimize>true</optimize> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--mvn spring-boot:run 命令--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>客户端:
<?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>spring-cloud-in-action</artifactId> <groupId>com.nobodyiam</groupId> <version>1.0-SNAPSHOT</version> </parent> --> <modelVersion>4.0.0</modelVersion> <artifactId>config-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>1.7</java.version> <project.encoding>UTF-8</project.encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.5.RELEASE</spring.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.3.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> --> </dependencies> </dependencyManagement> <dependencies> <!--spring cloud config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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-client</artifactId> </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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <optimize>true</optimize> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> </configuration> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--mvn spring-boot:run 命令--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>// =================编码======================
服务端
package com.spring.cloud.config.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.context.annotation.Configuration; /** * Created by Jason on 2/24/16. */ @Configuration @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }配置文件可以采用yml或properties格式,远程仓库可以采用git/svn/native,远程仓库只支持Http协议,所以svn必须以HTTP协议开放,否则连接不上,客户无法读取配置文件进行初始化工作。
management.context_path= /admin server.port= 8086 #server.contextPath= /ConfigServer #Git or SVN(subversion) or native , by default git(value config default) spring.profiles.active= default #1.Git repos configuration spring.cloud.config.server.git.uri= https://github.com/yangyangmyself/config-repo #spring.cloud.config.server.git.username=oyyl #spring.cloud.config.server.git.password=oyyl #spring.cloud.config.server.git.cloneOnStart=true #spring.cloud.config.server.git.basedir=target/class/repos #2.SVN repos configuration #spring.cloud.config.server.svn.uri= https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.username=oyyl #spring.cloud.config.server.svn.password=oyyl #spring.cloud.config.server.svn.default-label=branches #spring.cloud.config.server.svn.searchPaths=configs/test #spring.cloud.config.server.svn.cloneOnStart=true #2.1Multi sub repos configuration(Maven project is must add svnkit jar file) #Math name(or applicaion) #spring.cloud.config.server.svn.repos.r1.pattern=vs1* #spring.cloud.config.server.svn.repos.r1.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r1.username=oyyl #spring.cloud.config.server.svn.repos.r1.password=oyyl #spring.cloud.config.server.svn.repos.r1.default-label=branches #spring.cloud.config.server.svn.repos.r1.searchPaths=configs/test #spring.cloud.config.server.svn.repos.r2.pattern=vs2* #spring.cloud.config.server.svn.repos.r2.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r2.username=oyyl #spring.cloud.config.server.svn.repos.r2.password=oyyl #spring.cloud.config.server.svn.repos.r2.default-label=branches #spring.cloud.config.server.svn.repos.r2.searchPaths=configs/test #Math profile #spring.cloud.config.server.svn.repos.r3.pattern=vs2/* #spring.cloud.config.server.svn.repos.r3.uri=https://192.168.110.44/svn/config-repos #spring.cloud.config.server.svn.repos.r3.username=oyyl #spring.cloud.config.server.svn.repos.r3.password=oyyl #spring.cloud.config.server.svn.repos.r3.default-label=branches #spring.cloud.config.server.svn.repos.r3.searchPaths=configs/test #3.Read Native Config #3.1Load local system file #spring.profiles.active=native #spring.cloud.config.server.native.searchLocations=file://D:/etc/cas/config #3.2Load application classpath #spring.profiles.active=native #spring.cloud.config.server.native.searchLocations=classpath\:/repos
验证
http://localhost:8086/name/profiles/label
http://localhost:8086/name-profiles.properties/http://localhost:8086/name-profiles.yml,如下:
http://localhost:8086/vs-dev.yml
客户端
package com.spring.cloud.config.client; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Configuration @EnableAutoConfiguration @ComponentScan @RefreshScope @RestController public class ConfigClientApplication { @Value("${redis.server}") private String paraName; @RequestMapping(value = "/cf/redis") public String showRedis() { return "Reids IP: " + paraName; } private String getRequestStr(HttpServletRequest request){ String servletPath = request.getServletPath(); String requestURL = request.getRequestURL().toString(); return requestURL.substring(0, requestURL.indexOf(servletPath)); } @RequestMapping(value = "/admin/config/refresh") public String refresh(HttpServletRequest request) { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(getRequestStr(request) + "/admin/refresh"); try { client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "refresh"; } @RequestMapping(value = "/admin/config/restart") public String restart(HttpServletRequest request) { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(getRequestStr(request) + "/admin/restart"); try { client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "restart"; } public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }bootstrap.properties配置文件,bootstrap优先application加载,配置连接spring cloud config server端及读取属性文件
如:读取环境文件有:
vs.properties
vs-dev.properties
vs-test.properties
配置说明:
spring.application.name 为属性文件name片段
spring.cloud.config.profile 为属性文件profile片段
spring.cloud.config.uri 为config server端
如读取开发环境配置文件vs-dev.properties,则按如下配置:
spring.application.name=vs
spring.cloud.config.profile=dev
如果profile=default,则读取vs.properties属性文件
management.context_path= /admin server.contextPath= /vs server.port= 8087 #Propertis name(name of name-profile-lable.yml) spring.application.name= vs #1.Read Remote Config spring.cloud.config.uri= http://localhost:8086 #Profile(configed value to default or dev or test or prod and so on) # is come from propertis name(profile of name-profile-lable.yml) spring.cloud.config.profile: default// ==============高可用多库配置说明(服务端)==================
info: component: Config Server management: context_path: /admin server: port: 8888 spring: cloud: config: server: svn: password: oyyl uri: https://192.168.110.44/svn/config-repos // 只支持HTTP、HTTPS其它协议测试未通过 username: oyyl default-label: branches // 默认使用分支 searchPaths: configs/test //多级目录表示 cloneOnStart: true // 启动里复制到本地服务器 repos:... //多库配置 profiles: active: subversion //表示使用SVN, 默认使用git 【注意】uri 指定repos根目录即可,否则无法找到文件 SVN: default-label: trunk/branches GIT: default-label: master/branchesYML
info: component: Config Server management: context_path: /admin server: port: 8888 spring: cloud: config: server: svn: password: oyyl uri: https://192.168.110.44/svn/config-repos // 默认配置库 username: oyyl default-label: branches searchPaths: configs repos: vs1: pattern: vs1* // 此配置只匹配application, 如果为 vs1/* ,则匹配profiles uri: https://192.168.110.44/svn/config-repos // 配置库1 password: oyyl username: oyyl default-label: branches searchPaths: configs vs2: pattern: vs2* uri: https://192.168.110.44/svn/config-repos // 配置库2 password: oyyl username: oyyl default-label: branches searchPaths: configs profiles: active: subversion
目前实现远程仓库配置文件修改后,需要调用客户的Endpoint接口,进行手动刷新属性值,下面就是启动Endpoint管理接口
// 启动管理接口,如refresh 等,需要spring-boot-starter-actuatorr <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
客户端通过@Value({"key"})实现读取属性,只支持实例属性,不支持类静态属性赋值
相关推荐
在Spring Cloud 2.0版本中,Eureka Server的配置相比1.x版本确实有了一些显著的变化,尤其是在结合Spring Security进行安全设置时。Spring Cloud Eureka是Netflix Eureka的Spring Boot实现,它为微服务架构提供了...
CAS Spring Cloud Configuration Server覆盖模板通用CAS Spring Cloud Configuration Server WAR叠加。版本号< cas>6.0.x要求JDK 11建造要查看构建脚本可用的命令,请运行: ./build.sh help 要打包最终的Web应用...
- Spring Cloud建立在Spring Boot之上,提供了服务发现(如Eureka)、配置管理(如Config Server)、熔断机制(如Hystrix)、API网关(如Zuul)等服务治理功能,使开发者能够轻松构建云原生应用。 5. **Spring ...
### Spring Cloud Gateway全局异常处理详解 #### 一、引言 在微服务架构中,网关作为服务入口,承担着路由转发、限流熔断、鉴权认证等职责。Spring Cloud Gateway作为一款基于Spring Framework 5、Project Reactor...
本教程将深入探讨如何在基于Spring Cloud的微服务架构中集成Flowable工作流引擎,提升系统的流程管理能力。 首先,Spring Cloud是构建分布式系统的一系列工具集,包括服务发现、配置管理、熔断机制等,它为微服务...
- **SpringCloud ConfigServer**:配置服务器组件。 - **Environment Repository**:环境仓库。 - **Git Backend**:Git后端。 - **Version Control Backend Filesystem Use**:版本控制后端使用文件系统。 - *...
在本文中,我们将深入探讨如何将Spring Cloud与Nacos整合,以便实现动态配置管理,并将服务注册到Nacos服务中心。Spring Cloud是微服务架构中的一个关键组件,它提供了多种服务发现、配置、熔断等工具。而Nacos是...
- **Config Server**:它是 Spring Cloud Config 的服务器端,负责存储和管理配置文件。可以是本地文件系统、Git 或者 SVN 等版本控制系统。 - **Config Client**:客户端是应用的一部分,它们通过配置中心获取...
use distributed configuration with Spring Cloud Config in your applications. You will learn how to enable different backend repositories of property sources and push change notifications using Spring ...
每个知识点都可进一步展开为一系列配置选项、编程接口(API)的使用说明以及与Spring Boot、Spring Cloud Config、Eureka等其他组件的集成方法。这些内容对于使用Spring Cloud进行微服务架构设计和开发的开发者而言...
在提供的压缩包文件"springcloud-idea-chapter03-demo1-c1"中,可能包含了实现以上步骤的一个简单示例项目。该项目可能包括了服务提供者和服务消费者两个部分,展示了如何配置Ribbon以及如何在代码中发起负载均衡的...
- `eureka-server`: 这是Eureka服务器的实现,Eureka作为Spring Cloud的服务注册与发现组件,为服务间的通信提供了基础。 - `hystrix-dashboard`: Hystrix Dashboard是Hystrix的监控界面,它可以展示Hystrix命令的...
《SpringCloud OAuth2 Security:构建安全微服务架构》 OAuth2是目前广泛应用于授权和访问控制的开放标准,尤其在微服务架构中扮演着至关重要的角色。Spring Cloud Security结合了OAuth2,为开发者提供了一套便捷的...
4. **配置中心(Config Server)**:Spring Cloud Config 提供了集中式的外部化配置,便于管理和推送配置更新。 5. **服务调用(Service-to-Service Calls)**:Hystrix用于实现服务间的熔断和降级,提高系统的容错...
Spring Cloud的强大之处在于它能够很好地与其他Spring项目集成,并且支持多种第三方服务。 #### 四、项目结构 为了实现上述目标,我们将构建一个包含以下四个主要服务的Spring Cloud项目: 1. **Eureka Server** -...
5. **配置Spring Cloud Gateway**:在Spring Boot应用中,创建一个配置类,使用`@EnableDiscoveryClient`和`@Configuration`注解,并实现`RouteLocatorBuilderConfigurer`接口,以从Nacos动态加载路由规则。...
<name>springcloud-nacos-config-sample :: Nacos 服务配置中心案例 <!-- 添加Spring Boot、Spring Cloud Alibaba 和 Nacos 相关依赖 --> <groupId>org.springframework.boot <artifactId>spring-boot-...