SpringCloud学习之快速搭建分布式配置
一. 关于spring-cloud中的分布式配置
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性 ,通常情况下我们可以把需要管理的配置文件放置在svn或者git上进行做统一的配置仓库。
二 创建git远程仓库远程仓库中创建所需的配置文件
本例子中是demo-local.yml , 配置文件内容如下:
student:
name: test
age: 28
三 创建config-server端
1) 创建gradle模块config-server并添加gradle依赖
dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
}
2)编辑application.yml配置
server:
port: 8000
spring:
cloud:
config:
server:
git:
uri: git@gitlab.com:xxxx/config.git
enabled: true
profiles:
active: local
其中spring.cloud.config.server.git是配置远程仓库的地址,如果不使用SSH协议请配置username和password属性
3)编写启动类
package com.bdqn.lyrk.config; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigServer { public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
注意在启动类上添加@EnableConfigServer注解
四 创建config-client端
1)创建gradle模块demo-server并添加gradle依赖
dependencies{
compile('org.springframework.cloud:spring-cloud-starter-config')
}
添加bootstrap.yml (Bootstrap.yml(bootstrap.properties)在application.yml(application.properties)之前加载)
server:
port: 8001
spring:
cloud:
config:
uri: http://localhost:8000
profile: local
application:
name: demo
注意几点:
配置服务从 /{name}/{profile}/{label} 提供属性源,客户端应用程序中的默认绑定
“name”= ${spring.application.name}
“profile”= ${spring.profiles.active} (实际上是 Environment.getActiveProfiles() )
“label”=“master”
这里面的spring.application.name与远程仓库的配置文件名demo-local对应
编写DemoConfig类
package com.bdqn.lyrk.server.demo.config; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; @Configuration
@ConfigurationProperties(prefix = "student")
public class DemoConfig {
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
注意 @ConfigurationProperties(prefix = "student")注解,该注解作用是:获取yml配置文件的前缀student,配置文件中余下部分用javabean的属性替换即可
编写启动类:
package com.bdqn.lyrk.server.demo; import com.bdqn.lyrk.server.demo.config.DemoConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.context.ApplicationContext; @SpringBootApplication
public class DemoProvider { public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(DemoProvider.class, args);
DemoConfig demoConfig = applicationContext.getBean(DemoConfig.class);
System.out.println(demoConfig.getName());
}
}

运行后得到上述结果,此时我们已经从远程配置中获取到所需的信息了
SpringCloud学习之快速搭建分布式配置的更多相关文章
- springCloud学习-高可用的分布式配置中心(Spring Cloud Config)
1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...
- SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...
- 白话SpringCloud | 第七章:分布式配置中心的使用
前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...
- SpringCloud第六步:搭建分布式配置中心
什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud con ...
- SpringCloud搭建分布式配置中心(基于git)
1.简介 Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用 ...
- 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
- Spring Boot 学习(一) 快速搭建SpringBoot 项目
快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...
随机推荐
- Spring MVC Restful Put方法无法获取参数值
Spring MVC Restful 无法通过@ReqeustParam获取参数值 原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentF ...
- 读取.properties的内容1
属性文件方便于项目进行更改,在项目开发中用的也是非常的普遍,在这里就把属性文件的读取通过代码进行一个小结: package com.oyy.test; import java.io.BufferedI ...
- lua保存table到文件并从文件解析成table
require("json") result = { ["ip"]="192.168.0.177", ["date"]= ...
- 关于win10系统1709版本安装JDK出现变量配置正确但仍有“java不是内部或外部命令”的解决办法
背景:联想拯救者R720笔记本,系统一键还原了,需要重新安装一部分软件,最基本的就是JDK,但今天在安装时遇到了问题,之前安装的1.8版本,没有仔细配置环境变量,这一次安装的是1.7版本的,仔仔细细配 ...
- Hadoop学习笔记一(HDFS架构)
介绍 Hadoop分布式文件系统(HDFS)设计的运行环境是商用的硬件系统.他和现存的其他分布式文件系统存在很多相似点.不过HDFS和其他分布式文件系统的区别才是他的最大亮点,HDFS具有高容错的特性 ...
- 新概念英语(1-101)A Card From Jimmy
Lesson 101 A card from Jimmy 吉米的明信片 Listen to the tape then answer this question. Does Grandmother s ...
- Spring Security入门(3-9)Spring Security登录成功以后
- python 开发之路 -MySQL
阅读目录 第一篇 : 数据库 之 基本概念 第二篇 : MySQL 之 库操作 第三篇 : MySQL 之 表操作 第四篇 : MySQL 之 数据操作 第五篇 : MySQL 之 视图.触发器.存储 ...
- Mysql变量列表
变量表解释 (https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html)
- Java-Maven(六):Eclipse中Maven插件的命令操作
之前几个章节学习了maven的概念,及maven插件安装后如何创建工程,那么maven工程中是如何使用maven命令呢?本章节将会学习这个话题. 在pom.xml中配置maven命令插件 如果向在ma ...