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. Oracle DBA学习互联网化的内容
  2. Task01:青少年软件编程(Scratch)等级考试模拟卷(一级)
  3. spring中的事务配置
  4. 【BFS宽度优先搜索】
  5. sql select
  6. Python3学习笔记2:简易Web爬虫
  7. 常用选择器(CSS+JQuery)
  8. 将python中的小数直接进位的函数_python保留小数位的三种实现方法
  9. Linux设备驱动:DMA 接口API
  10. Vue 基础的开发环境
  11. 如何改变maven项目的pom文件中默认的主代码目录 以及默认的测试代码目录?
  12. 根据图像连接数判别不同像素所处的位置
  13. 广告点击率模型中,LR, GBDT+LR, FM, DNN等模型的优点和缺点?实际效果如何?
  14. vbs if 不等于_(四)if 判断与逻辑运算符
  15. vue项目部署的一些配置和流程
  16. 电力系统matlab实验报告,电力系统分析潮流实验报告
  17. MySQL基础(适合新手入门)
  18. 微信小程序实战之登录页面制作
  19. 新媒体运营之如何低成本,有效地进行企业公众号拉新,获取10万+潜在用户? 黎想
  20. 电脑与云服务器的区别吗,云服务器和普通电脑有什么不同区别?

热门文章

  1. jzoj 1307. Jail
  2. [分享]敏感内容自动评审类库及辅助工具
  3. win10蓝牙无法连接,可以尝试在此Windows设备上打开蓝牙
  4. 渣渣本科的2021届秋招总结-泪目
  5. matlab绘四叶玫瑰线,玫瑰线 - calculus的日志 - 网易博客
  6. 南大计算机博士黄鑫,南京大学软件学院张贺教授团队在经验软件工程方法学研究中取得重要成果...
  7. 第九篇:真正理解虚拟 DOM:React 选它,真的是为了性能吗?
  8. 鸿蒙如何用JS开发智能手表App
  9. 用jQuery添加dragstart,dragover和drop事件,实现拖拽效果
  10. 闪击6偏大偏小?_WEN开箱 | 开箱李宁闪击5季后赛版探讨对比普通版和闪击3怎么选...