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 统一配置管理相关推荐

  1. 第十二章 Spring Cloud Config 统一配置中心详解

    目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...

  2. python自助电影售票机_Spring Cloud版——电影售票系统六使用 Spring Cloud Config 统一管理微服务配置...

    一. 为什么要统一管理微服务配置 在传统的单体应用,常使用配置文件管理所有配置.比如,一个 Spring Boot 开发的单体应用,可将配置内容放在 application.yml 文件中.如果需要切 ...

  3. Spring Cloud Config统一管理微服务配置

    一Spring Cloud Config背景及简介 # 集中管理的需求:一个使用微服务架构的应用系统可能会包括成百上千个微服务,因此集中管理很有必要 # 不同环境不同配置:例如数据源在不同的环境(开发 ...

  4. Spring Cloud Config采用数据库存储配置内容

    在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...

  5. Spring Cloud Config采用数据库存储配置内容【Edgware+】

    在之前的<Spring Cloud构建微服务架构:分布式配置中心>一文中,我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储.这一设计巧妙的利用Gi ...

  6. spring vault_微服务–第2部分:使用Spring Cloud Config和Vault进行配置管理

    spring vault 在使用Spring Boot和Spring Cloud的MicroServices –第1部分:概述中 ,我们简要介绍了什么是微服务以及如何使用SpringBoot和Spri ...

  7. 为Spring Cloud Config插上管理的翅膀

    最近一致在更新Spring Cloud Config的相关内容,主要也是为这篇埋个伏笔,相信不少调研过Spring Cloud Config的用户都会吐槽它的管理能力太弱.因此,就有了下面为讲推荐的这 ...

  8. Spring Cloud Config采用Git存储时两种常用的配置策略

    由于Spring Cloud Config默认采用了Git存储,相信很多团队在使用Spring Cloud的配置中心时也会采用这样的策略.即便大家都使用了Git存储,可能还有各种不同的配置方式,本文就 ...

  9. 【SpringCloud】四、Spring Cloud Config

    Spring Cloud Config 前言 一.什么是配置中心 1. 为什么需要分布式配置中心 2.常用分布式配置中心框架 二.什么是Spring Cloud Config? 1.Springclo ...

最新文章

  1. 51单片机串口通信(字符串接收和发送)
  2. 【必看】如何在 Linux 上恢复误删除的文件或目录
  3. html 5 video audio
  4. 跟我学Java(配光盘)(跟我学)
  5. 理请求时出现未知错误.服务器返回的状态码为: 500,react-native
  6. day18 java的数组
  7. ICT学习笔记(3)IP编址
  8. android开发 转跳功能,如何在Android中利用Intent实现一个页面跳转功能
  9. vscode配置python2和python3_VS Code中配置python版本以及Python多版本
  10. 【论文笔记】Joint Cascade Face Detection and Alignment
  11. 软件项目管理案例教程(第三版)课后习题及答案
  12. navicat中看sql执行计划
  13. 适合于初学者—软路由全探索系列(二):探索群晖 VMM 虚拟机旁路由安装及设置
  14. loadrunner 集合点lr_rendezvous 规则以及操作使用
  15. OSChina 周六乱弹 —— 女菩萨,你可愿做贫僧的……
  16. 使用kubeadm在CentOS上搭建Kubernetes1.14.3集群
  17. java实现deflate数据压缩和gzip数据压缩
  18. 警方通报“济南1家6口死亡”案:男子杀害亲人后放火跳楼
  19. 故障效果,制作抖音效果的幻影海报
  20. 犹太裔大陆籍华人的故事-值得一读

热门文章

  1. HTML+CSS聚光灯效果动画
  2. Easyrecovery13 for mac 易恢复软件 官方中文版下载
  3. Ubuntu系统下解决中文显示异常问题
  4. 2021年材料员-通用基础(材料员)考试题库
  5. JVM 垃圾回收详解
  6. 实现1V1音视频实时互动直播系统 十二、第九节 直播客户端的实现
  7. 如何围绕某一轴(不是xyz)旋转——transform.RotateAround
  8. 阿里nlp算法实习记录
  9. Web前端JQuery入门实战案例
  10. H5互动游戏平台推荐:盘点那些超火的多人小游戏互动对战平台