分布式配置基础应用

配置中心服务

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. CentOS 7 内核RPM方式升级

    RPM包下载地址: https://elrepo.org/linux/kernel/el7/x86_64/RPMS/ 选择lt版本(长期支持) #下载内核RPM包,这里是kernel-lt-4.4.- ...

  2. Ethical Hacking - GAINING ACCESS(14)

    CLIENT SIDE ATTACKS Protecting against smart delivery methods Ensure you're not being MITM'ed -> ...

  3. C++算法 链式前向星存图

    这个东西恶心了我一阵子,那个什么是什么的上一个一直是背下来的,上次比赛忘了,回来有个题也要用,只能再学一遍,之前也是,不会为什么不学呢.我觉得是因为他们讲的不太容易理解,所以我自己给那些不会的人们讲一 ...

  4. DDD之5限界上下文-定义领域边界的利器

    上图是一张普通地图,最刺眼的就是边界? 非常好奇地图绘制工程师是如何描绘如此弯曲多变的边界的?强制行政区域还是人群历史原因自然的人以群分? 我们再换个视角,对工程师或者架构师来说,微服务的边界如何划分 ...

  5. DJANGO-天天生鲜项目从0到1-001-环境框架搭建

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  6. 0.9循环=lim(n趋于无穷大)(1-1/10的n次方),所以这是一个极限问题

    0.9循环=lim(n趋于无穷大)(1-1/10的n次方),所以这是一个极限问题 因为lim(...)(1-1/10的n次方)=1 这意味着维尔斯特拉斯发明极限定义之前,这个等号是不成立的,因为没有极 ...

  7. scrapy分布式浅谈+京东示例

    scrapy分布式浅谈+京东示例: 学习目标: 分布式概念与使用场景 浅谈去重 浅谈断点续爬 分布式爬虫编写流程 基于scrapy_redis的分布式爬虫(阳关院务与京东图书案例) 环境准备: 下载r ...

  8. 枚举-称硬币POJ1013

    #include <iostream> #include<string.h> using namespace std; char Lleft[][]; char Lright[ ...

  9. PHP 数据库 ODBC创建 ODBC 连接

    PHP 数据库 ODBC ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数 ...

  10. JavaScript正则表达式相关方法

    一.正则表达式方法 var str="abcdefabcdef"; (1)reg.test(str); 查看字符串是否有满足正则表达式的内容,并返回一个布尔值true/false ...