分布式配置基础应用

配置中心服务

spring-config-server

pom.xml


<?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>org.galsang.cloud</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-config-server</name>
<description>配置中心服务</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-test</artifactId>
<scope>test</scope>
</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>

application-dev.yml

spring:
profiles: dev
cloud:
inetutils:
preferred-networks:
- 192.168
config:
server:
git:
uri: https://gitee.com/vimx86/SpringCloud-Learning/ # git 配置仓库地址
search-paths: galsang-z-docs/config-repo
# 由于是公开git 仓库,故不使用账号密码
# username:
# password: # git管理配置
# /{application}/{profile}[/{label}]
# /{application}-{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties # http://www.galsang.org:9001/application-dev/dev/master
# http://www.galsang.org:9001/application/dev/master # http://www.galsang.org:9001/application-dev.properties 默认使用master分支中的配置文件
# http://www.galsang.org:9001/dev/application-beta.properties 使用指定分支中的配置文件 server:
port: 9001
context-path: /
display-name: www.galsang.org # 服务注册配置
eureka:
# 作为服务进行注册
client:
register-with-eureka: true
fetch-registry: true
enabled: true
service-url:
defaultZone: http://www.galsang.org:9000/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.display-name}:${server.port}
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2

SpringConfigServerApplication.java


package org.galsang.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer // 激活配置中心服务
@EnableDiscoveryClient // 激活Eureka中的DiscoveryClient 服务发现
public class SpringConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
}
}

配置客户服务

spring-config-client

pom.xml

<?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>org.galsang.cloud</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-config-client</name>
<description>配置客户服务</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<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>${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>

bootstrap.yml

spring:
application:
name: spring-config-client
cloud:
config:
profile: dev # 环境
label: master # git 仓库分支
uri: http://www.galsang.org:9001/ # 配置中心地址
server:
port: 9002
context-path: /
display-name: www.galsang.org eureka:
client:
register-with-eureka: true
fetch-registry: true
enabled: true
service-url:
defaultZone: http://www.galsang.org:9000/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.display-name}:${server.port}
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2

SpringConfigServerApplication.java


package org.galsang.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class SpringConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
}
}

TestConfigController.java


package org.galsang.cloud.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Description:测试 使用 配置中心 配置文件的 接口
* <br /> Author: vimx86
*/
@RestController
public class TestConfigController { @Value("${from}")
private String from; @RequestMapping("/from")
public String from() {
return from;
} }

效果预览

浏览器打开 http://www.galsang.org:9002/from

说明访问git仓库中的配置成功。

源码请移步: https://gitee.com/vimx86/SpringCloud-Learning

参考资料

Spring Cloud 之分布式配置基础应用的更多相关文章

  1. Spring Cloud Config 分布式配置中心使用教程

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

  2. Spring Cloud 2-Config 分布式配置中心(七)

    Spring Cloud  Config  1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...

  3. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  4. Spring Cloud (5) 分布式配置中心

    Spring Cloud Config 在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,使用Spring Cloud ...

  5. spring cloud 集成分布式配置中心 apollo(单机部署apollo)

    一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...

  6. Spring Cloud之分布式配置中心

    用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.boot. ...

  7. Spring Cloud (6) 分布式配置中心-高可用

    高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...

  8. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  9. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

随机推荐

  1. 数据可视化之PowerQuery篇(十七)Power BI数据分析应用:水平分析法

    https://zhuanlan.zhihu.com/p/103264851 ​本文为星球嘉宾"海艳"的PowerBI数据分析工作实践系列分享之一,她深入浅出的介绍了PowerBI ...

  2. Ubuntu虚拟机设置共享文件夹

    Ubuntu虚拟机设置共享文件夹 注:个人笔记,小白笔记. 点击设置 进入到Ubuntu 中 cd /mnt/hgfs/ 后   我们会看到自己设置的共享文件夹.

  3. Centos7安装ftp服务

    本文介绍的ftp是可以使用匿名用户登录,且默认路径是根路径,私人使用非常方便,公开使用具有一定的风险,不安全. # .安装 yum install -y vsftpd # .配置 vim /etc/v ...

  4. odoo12数据库自动化备份

    数据库自动备份模块地址 https://github.com/Yenthe666/auto_backup#8.0 目前支持8以上的版本 odoo12的配置步骤 1.下载模块到自己的模块目录 2.登录o ...

  5. 题解 UVA501 【Black Box】

    思路与中位数一题,解决方案比较像,使用对顶堆来解决. 具体实现为,使用两个堆,大根堆维护较小的值,小根堆维护较大的值,即小根堆的堆顶是较大的数中最小的,大根堆的堆顶是较小的数中最大的. 将大于大根堆堆 ...

  6. 毫不留情地揭开 ArrayList 和 LinkedList 之间的神秘面纱

    先看再点赞,给自己一点思考的时间,思考过后请毫不犹豫微信搜索[沉默王二],关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有技术大佬整理的面试题, ...

  7. java判断当前系统是win还是linux

    private static final boolean isWin = System.getProperty("os.name").toLowerCase().contains( ...

  8. Vue脚手架创建项目出现 (Failed to download repo vuejs-templates/webpack: Response code 404)

    搭建好(脚手架2.X版本)环境像往常一样使用vue init webpack xxxx 创建项目可以是没多久就开始报错了 报错结果就是:vue-cli · Failed to download rep ...

  9. sed打印包含一个字符串的行到包含另一个字符串的行解答

    sed -n '/字符串1/,/字符串2/p' filename  这个命令为什么有时候打印不出来想要的东西,例如:sed -n '/root/,/adm/p'  /etc/passwd      我 ...

  10. 图表可视化seaborn风格和调色盘

    seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,包括一些高级图表可视化等工具. 使用seaborn需要先安装改模块pip3 install seaborn ...