spring cloud config 统一配置管理
Spring Cloud为开发人员提供了在分布式系统中快速构建一些常见模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话)等等20多个项目。同时它采用了springboot进行快速搭建和开发,非常简洁易用。今天主要记录springcloud eureka调用配置文件管理的使用。
项目结构
项目 | 介绍 |
springcloud-registry | Eureka 注册中心 |
config-server | 配置管理服务 |
config-client | 调用配置的为服务 |
中间调用情况:
Euraka server 注册中心
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>com.wcy.register</groupId><artifactId>springcloud-registry</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springcloud-registry</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.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><spring-cloud.version>Edgware.RELEASE</spring-cloud.version></properties><dependencies><!--start jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.3.0</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.0</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!--end jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</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-actuator</artifactId></dependency></dependencies><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
简单的配置文件:
spring.application.name=eboss-eureka-server
server.port=3000
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/eureka.server.evictionIntervalTimerInMs=5000
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=2000#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
config-server 配置中心
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><artifactId>config-server</artifactId><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.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><spring-cloud.version>Finchley.M8</spring-cloud.version></properties><dependencies><!--start jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.3.0</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.0</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!--end jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR3</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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>
启动类代码:
package com.wcy.config.server;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/**** @Description: <br>* @Project: eboos <br>* @CreateDate: Created in 2018/4/29 17:01 <br>* @Author: <a href="836327318@qq.com">wcy</a>*/
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringBootConfigServerApplication {public static void main(String[] args) {SpringApplication.run(SpringBootConfigServerApplication.class, args);}
}
配置文件 bootstrap.properties
spring.application.name=spring-config-server
spring.profiles.active=native
server.port=3001
eureka.client.serviceUrl.defaultZone=http://localhost:3000/eureka/
spring.cloud.config.server.native.searchLocations=file:///D:/config
本地文件的目录
application-test.properties 内容
server.port=3002
spring.config.demo=hello,springcloud-config
启动服务后url访问的路径:http://192.168.0.103:3001/application/test
如果能把配置文件打开就成功了。
config-client 微服务配置调用项目
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><artifactId>config-client</artifactId><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.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><spring-cloud.version>Finchley.M8</spring-cloud.version></properties><dependencies><!--start jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.3.0</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.0</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!--end jdk9需要引入的jar包,jdk8不需要--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</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-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR3</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><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>
</project>
项目启动类:
package com.wcy.config.client;import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/**** @Description: <br>* @Project: springcloud config <br>* @CreateDate: Created in 2018/4/29 19:17 <br>* @Author: <a href="836327318@qq.com">wcy</a>*/
@SpringBootApplication
@RestController
@EnableEurekaClient
public class SpringBootConfigClientApplication {@Value("${spring.config.demo}")String configInfo;@RequestMapping("/config")public String config(){return "configInfo:"+configInfo;}public static void main(String[] args) {SpringApplication.run(SpringBootConfigClientApplication.class, args);}
}
项目启动配置bootstrap.properties
spring.application.name=spring-config-client
//eureka注册中心的服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:3000/eureka/
//配置中心服务的ip和端口
spring.cloud.config.uri=http://localhost:3001/
//如果配置文件是abc-test.properties,下面参数为test,
//如果配置文件为abc-d-test.properties,下面参数为d-test
spring.cloud.config.profile=test
启动项目之后我们会发现虽然我们没配置端口,但是项目启动的端口是3002,所以是从配置文件里面读取到有:server.port=3002
然后我们调用接口:http://localhost:3002/config
当前项目git地址:https://github.com/yilishuku/springcloud-config.git
spring cloud config 统一配置管理相关推荐
- 第十二章 Spring Cloud Config 统一配置中心详解
目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...
- python自助电影售票机_Spring Cloud版——电影售票系统六使用 Spring Cloud Config 统一管理微服务配置...
一. 为什么要统一管理微服务配置 在传统的单体应用,常使用配置文件管理所有配置.比如,一个 Spring Boot 开发的单体应用,可将配置内容放在 application.yml 文件中.如果需要切 ...
- Spring Cloud Config统一管理微服务配置
一Spring Cloud Config背景及简介 # 集中管理的需求:一个使用微服务架构的应用系统可能会包括成百上千个微服务,因此集中管理很有必要 # 不同环境不同配置:例如数据源在不同的环境(开发 ...
- Spring Cloud Config采用数据库存储配置内容
在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...
- Spring Cloud Config采用数据库存储配置内容【Edgware+】
在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...
- spring vault_微服务–第2部分:使用Spring Cloud Config和Vault进行配置管理
spring vault 在使用Spring Boot和Spring Cloud的MicroServices –第1部分:概述中 ,我们简要介绍了什么是微服务以及如何使用SpringBoot和Spri ...
- 为Spring Cloud Config插上管理的翅膀
最近一致在更新Spring Cloud Config的相关内容,主要也是为这篇埋个伏笔,相信不少调研过Spring Cloud Config的用户都会吐槽它的管理能力太弱.因此,就有了下面为讲推荐的这 ...
- Spring Cloud Config采用Git存储时两种常用的配置策略
由于Spring Cloud Config默认采用了Git存储,相信很多团队在使用Spring Cloud的配置中心时也会采用这样的策略.即便大家都使用了Git存储,可能还有各种不同的配置方式,本文就 ...
- 【SpringCloud】四、Spring Cloud Config
Spring Cloud Config 前言 一.什么是配置中心 1. 为什么需要分布式配置中心 2.常用分布式配置中心框架 二.什么是Spring Cloud Config? 1.Springclo ...
最新文章
- 51单片机串口通信(字符串接收和发送)
- 【必看】如何在 Linux 上恢复误删除的文件或目录
- html 5 video audio
- 跟我学Java(配光盘)(跟我学)
- 理请求时出现未知错误.服务器返回的状态码为: 500,react-native
- day18 java的数组
- ICT学习笔记(3)IP编址
- android开发 转跳功能,如何在Android中利用Intent实现一个页面跳转功能
- vscode配置python2和python3_VS Code中配置python版本以及Python多版本
- 【论文笔记】Joint Cascade Face Detection and Alignment
- 软件项目管理案例教程(第三版)课后习题及答案
- navicat中看sql执行计划
- 适合于初学者—软路由全探索系列(二):探索群晖 VMM 虚拟机旁路由安装及设置
- loadrunner 集合点lr_rendezvous 规则以及操作使用
- OSChina 周六乱弹 —— 女菩萨,你可愿做贫僧的……
- 使用kubeadm在CentOS上搭建Kubernetes1.14.3集群
- java实现deflate数据压缩和gzip数据压缩
- 警方通报“济南1家6口死亡”案:男子杀害亲人后放火跳楼
- 故障效果,制作抖音效果的幻影海报
- 犹太裔大陆籍华人的故事-值得一读