在整个微服务体系中,除了注册中心具有非常重要的意义之外,还有一个注册中心。注册中心作为管理在整个项目群的配置文件及动态参数的重要载体服务。Spring Cloud体系的子项目中,Spring Cloud Config子项目就是该注册中心。在整个分布式框架系统中,充当重要角色。

官方解释

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

本系列博文目录

【微服务】从零开始,轻松搞定SpringCloud微服务目录

说明:本系列源码持续更新,开始本篇之前先了解前面几篇文章。

开始起飞

基本思路:本文采用Git仓库作为配置文件的存放地址,通过创建一个配置中心服务器启动服务,然后再通过创建一个配置中心的客户端进行测试是否正常运转。

创建配置中心仓库

在原有的父类项目下创建一个普通的子项目,可以删除无关的文件,只留下空白项目。然后再创建一个测试的配置文件。

配置文件中加入测试数据

#随意设置的一个参数
myblog:
name: 千万之路刚开始-author-hyh
url: http://www.hanyahong.com
location: BeiJing

创建配置中心服务端

创建子项目

POM文件配置

在pom.xml文件中做一下配置


<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

配置项目resource配置文件

:整个博客是对各个子项目整合,因此加入了服务注册中心的相关配置

在resources文件夹下创建application.yml文件。并加入以下配置:

#服务端口
server:
port: 8082 #服务注册中心配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/ #spring设置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/hanyahong/spring-cloud-microservice.git
searchPaths: cloud-hyh-config-repo

创建主方法类

在创建完包以后,创建主方法。


@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication { public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

至此,服务端配置完毕,启动服务器即可,等待客户端验证。

创建客户端

创建一个配置客户端,对刚刚的服务进行测试。
@EnableDiscoveryClient: 服务发现客户端注解,用于被发现。
@EnableConfigServer: 开启配置中心服务器配置。

创建客户端子项目

配置pom文件

在子项目pom.xml中加入一下依赖及插件。

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

创建配置文件

在子项目中resources文件夹下,创建bootstrap.yml文件。加入一下配置。

spring:
application:
#本项目名称
name: config-client
cloud:
config:
#配置中心服务器地址配置
uri: http://localhost:8082/
profile: default
label: master
retry:
# 配置重试次数,默认为6
max-attempts: 6
# 间隔乘数 默认1.1
multiplier: 1.1
# 初始重试间隔时间,默认1000ms
initial-interval: 1000
# 最大间隔时间,默认2000ms
max-interval: 2000 server:
port: 8091
#服务发现配置
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/

创建程序入口

创建默认包以后创建ConfgClientApplication.java 文件。

/**
* @Description :配置中心启动类
* @Author hanyahong
* @Date 2017/12/6- 14:06
*/ @SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}

创建测试API

创建一个测试API文件,TestApi.java。

/**
* @Description :配置中心-客户端展示API
* @Author hanyahong
* @Date 2017/12/6- 16:39
*/
@RefreshScope
@RestController public class TestApi { @Value("${myblog.name}")
private String name;
@Value("${myblog.url}")
private String url;
@Value("${myblog.location}")
private String location;
@RequestMapping("/blog-info")
public String getBlogInfo() {
return "从Github仓库中获取得到我博客信息:【"+location+","+","+url+","+name+"】";
}
}

@RefreshScope:开启刷新

至此,配置中心测试的客户端基本完毕。
对于子项目来说有三个子项目:
cloud-hyh-config 端口号:8082
cloud-hyh-config-client 端口号:8091
cloud-hyh-config-repo 纯存储使用,该文档下面的配置文件一定要上传到仓库后,才可以远程获取。

启动项目并测试

对服务注册中心(上篇有写)、服务配置中心、服务客户端分别进行启动。
可以通过注册中心查看是否都已经被纳入管理。

测试一: 注册中心测试
首先通过访问配置中心服务器的地址可以进行测试是否获取成功。
访问 http://localhost:8082/config-client.yml 对仓库中资源文件 测试是否返回结果。
另外也可以通过 http://localhost:8082/config-client/master 进行访问。浏览器显示返回结果:

"name":"config-client","profiles":["master"],"label":null,"version":"7169e90f628c85d582f3f9d5fceda36696ebd751","state":null,"propertySources":[{"name":"https://github.com/hanyahong/spring-cloud-microservice.git/cloud-hyh-config-repo/config-client.yml","source":{"myblog.name":"千万之路刚开始-author-hyh","myblog.url":"http://www.hanyahong.com","myblog.location":"BeiJing","config-client.name":"test"}}]}

测试二: 客户端访问API测试
通过客户端访问API http://localhost:8091/blog-info 显示结果:
从Github仓库中获取得到我博客信息:【BeiJing-Customs,,http://www.hanyahong.com,千万之路刚开始-author-hyh】
测试成功!

测试三: 动态更新参数测试
配置中心一个重要的功能就是你无须重启去生效一些参数配置,系统可以通过访问/refresh 进行动态刷新,将参数生效。

  1. 修改配置文件信息,上传git仓库。
  2. 使用PostMan 或其他工具进行一次POST请求 API:http://localhost:8091/refresh (一定要看清楚,POST请求,浏览器直接访问无效,会报Request method 'GET' not supported 错误)。
  3. 再一次访问 http://localhost:8091/blog-info ,可以看到已在未重启的情况下,配置动态更新。

后续说明

因配置中心涉及很多数据的更新,不可能每次通过这种方式去动态更新,后续会有专门消息总线模块的讲解,将通过消息总线的机制去进行配置的传输。

源码地址

GitHub:https://github.com/hanyahong/spring-cloud-microservice

【微服务】之三:从零开始,轻松搞定SpringCloud微服务-配置中心的更多相关文章

  1. 从零开始,轻松搞定SpringCloud微服务系列

    本系列博文目录 [微服务]之一:从零开始,轻松搞定SpringCloud微服务系列–开山篇(spring boot 小demo) [微服务]之二:从零开始,轻松搞定SpringCloud微服务系列–注 ...

  2. 【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)

    微服务体系,有效解决项目庞大.互相依赖的问题.目前SpringCloud体系有强大的一整套针对微服务的解决方案.本文中,重点对微服务体系中的服务发现注册中心进行详细说明.本篇中的注册中心,采用Netf ...

  3. 【微服务】之四:轻松搞定SpringCloud微服务-负载均衡Ribbon

    对于任何一个高可用高负载的系统来说,负载均衡是一个必不可少的名称.在大型分布式计算体系中,某个服务在单例的情况下,很难应对各种突发情况.因此,负载均衡是为了让系统在性能出现瓶颈或者其中一些出现状态下可 ...

  4. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

  5. 【微服务】之六:轻松搞定SpringCloud微服务-API网关zuul

    通过前面几篇文章的介绍,我们可以轻松搭建起来微服务体系中比较重要的几个基础构建服务.那么,在本篇博文中,我们重点讲解一下,如何将所有微服务的API同意对外暴露,这个就设计API网关的概念. 本系列教程 ...

  6. 【微服务】之七:轻松搞定SpringCloud微服务-API权限控制

    权限控制,是一个系统当中必须的重要功能.张三只能访问输入张三的特定功能,李四不能访问属于赵六的特定菜单.这就要求对整个体系做一个完善的权限控制体系.该体系应该具备针区分用户.权限.角色等各种必须的功能 ...

  7. 【微框架】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  8. 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  9. 带你十天轻松搞定 Go 微服务系列(一)

    本文开始,我们会出一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建(本文) 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Au ...

随机推荐

  1. QQ顶部的消息,联系人切换

    高仿ios SegmentView 以前做过的一个项目美工设计的页面包含这个控件,和QQ消息页面顶部的效果一样,其实,这个控件是ios7的分段控制,android没有这个控件,不过实现起来也比较简单, ...

  2. PHP获取文件扩展名的五种方式

    这是我应聘实习时遇到的一道笔试题: 使用五种以上方式获取一个文件的扩展名. 要求:dir/upload.image.jpg,找出 .jpg 或者 jpg , 必须使用PHP自带的处理函数进行处理,方法 ...

  3. 静默获取微信用户openid如此简单,只需要一句话

    页面A getopenid.php <?php /** * Created by PhpStorm. * User: sunjianyin * Date: 2017/10/7 * Time: 下 ...

  4. 详解 $().css('width')和$().width()的区别

    在本次项目开发中,经常用jquery获取高度和宽度并且动态加载,有时候用$().css('width')或$().width()这两个方法获取宽度并设置,但是有时候出现获取不到的情况,查阅资料后发现他 ...

  5. hibernate的操作Blob和Clob类型数据(笔记)

  6. (转)IDEA破解 2017 IDEA license server 激活(可用)

    进入ide主页面,help-register-license server,然后输入 http://idea.iteblog.com/key.PHP(注意:php要小写)即可~

  7. C++有符号和无符号数的转换

    本文转自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题 ...

  8. FATE

    FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  9. awk命令练习

    文件 file.txt的内容格式: 文件中包含名字,电话号码和过去三个月里的捐款 具体内容如下: Mike Harrington:[510] 548-1278:250:100:175 Christia ...

  10. css给div添加阴影效果

    直接上代码: <style type="text/css">.mydiv{   width:250px; height:auto; border:#909090 1px ...