一. 关于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学习之快速搭建分布式配置的更多相关文章

  1. springCloud学习-高可用的分布式配置中心(Spring Cloud Config)

    1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...

  2. SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...

  3. 白话SpringCloud | 第七章:分布式配置中心的使用

    前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...

  4. SpringCloud第六步:搭建分布式配置中心

    什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud con ...

  5. SpringCloud搭建分布式配置中心(基于git)

    1.简介 Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用 ...

  6. 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  7. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  8. 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...

  9. Spring Boot 学习(一) 快速搭建SpringBoot 项目

    快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...

随机推荐

  1. NOIP2012 提高组 Day 2

    http://www.cogs.pro/cogs/page/page.php?aid=16 期望得分:100+100+0=0 实际得分:100+20+0=120 T2线段树标记下传出错 T1 同余方程 ...

  2. windows 7 netsh wlan命令连接wifi

    显示本机保存的profiles,配置文件是以wifi的ssid命名的. netsh wlan show profiles 用netsh wlan connect name=00_1111 连接其中一个 ...

  3. 记一下webstorm快键键

    #####新建文件````ctrl+alt+insert````#####结构速写````div>ul>li*4>p | div>h1+p | input:text | div ...

  4. markdown最基本的几种语法

    1.标题 # 相当于<h1></h1> ## 相当于<h2></h2> ### 相当于<h3></h3> #### 相当于< ...

  5. Andrew Ng机器学习第一章——初识机器学习

    机器学习的定义 计算机程序从经验E中学习,解决某一任务T.进行某一性能度量P,通过P测定在T上的表现因E而提高. 简而言之:程序通过多次执行之后获得学习经验,利用这些经验可以使得程序的输出结果更为理想 ...

  6. 移动端300ms与点透总结

    300ms,主要发生在mobile 为啥会出现300ms延迟现象 浏览器想知道用户是否dobule tap(双击缩放) 下列情况不会出现300ms延迟 桌面浏览器 meta的viewport设置了us ...

  7. Python内置函数(65)——staticmethod

    英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...

  8. linux下的Shell编程(7)使用-x和-n调试shell程序

    我们也可以在Shell下调试Shell Script脚本,当然最简单的方法就是用echo输出查看变量取值了.Bash也提供了真正的调试方法,就是执行脚本的时候用-x参数. sh -x filename ...

  9. POJ1015 && UVA - 323 ~Jury Compromise(dp路径)

    In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...

  10. Django—中间件

    中间件简介 什么是中间件 中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功 ...