一. 关于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. Hibernate之Hibernate的下载与安装

    Hibernate用法十分简单,当我们在Java项目中引入Hibernate框架之后,就能以面向对象的方式来操作关系数据库了. 下载: 登陆Hibernate官网,下载Hibernate压缩包,win ...

  2. HNOI 2012 永无乡

    codevs 1477 永无乡 http://codevs.cn/problem/1477/ 2012年湖南湖北省队选拔赛  时间限制: 1 s  空间限制: 128000 KB   题目描述 Des ...

  3. OO前三次作业总结

    一.第一次作业 1.程序设计分析 ![img](s1.ax1x.com/2018/04/02/CSgoSU.png) 图1 第一次作业类图 ![name](https://images2018.cnb ...

  4. animation & @keyframes 实现loading效果

    效果图截图如下: 直接上代码: html <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...

  5. BizTalk Server 2010高可用方案

    BizTalk Server 2010高可用方案 本文介绍了 Microsoft BizTalk Server 中通过对主机的各层进行扩展提供高可用性的方案. 分隔各个区域的功能分为不同的主机和中的层 ...

  6. 新概念英语(1-129)Seventy miles an hour

    Lesson 129 Seventy miles an hour 时速70英里 Listen to the tape then answer this question. What does Ann ...

  7. redis入门(05)redis的key命令

    一.什么是redis键命令 Redis 键(key):Redis 键命令用于管理 redis 的键. Redis 键命令的基本语法: redis 127.0.0.1:6379> COMMAND ...

  8. 转:NLP+句法结构(三)︱中文句法结构(CIPS2016、依存句法、文法)

    NLP+句法结构(三)︱中文句法结构(CIPS2016.依存句法.文法)转自:https://www.cnblogs.com/maohai/p/6453389.html 摘录自:CIPS2016 中文 ...

  9. python列表基础操作

    Python列表基本操作 记住一句话,叫做顾首不顾尾 首先我们来定义一个列表 name = ["jixuege","dajiba","boduoye& ...

  10. 安卓手机USB共享网络给PC上网

    开端 哈哈,最近我又发现了一个校园网的漏洞,但是只能手机连接,于是就想手机连接之后通过usb共享给电脑上网. 在手机上连接校园网WiFi,开启USB网络共享并且连接电脑之后,却发现电脑十分的卡顿!CP ...