微信公众号:[0error] 关注可了解更多的知识干货,也可看看生活杂谈。如有问题或建议,欢迎在公众号留言。
今天继续学习SpringCloud。
上篇我们讲了Feign和Hystrix
这一篇针对配置中心和服务跟踪详细说说
以下代码皆用最简单的代码示例,并非真正的业务代码
学习中用到的学习资料如下:
文章:SpringCloud极简入门
https://gitbook.cn/gitchat/column/5e38e68dec8d9033cf91a047
视频:Spring Cloud从入门到实战
https://www.bilibili.com/video/BV1p4411K7pz?spm_id_from=333.999.0.0
配置中心
在基于微服务的分布式系统中,存在一个问题,多个微服务所对应的配置项也会非常多,一旦某个微服务进行了修改,则其他服务也需要作出调整,直接在每个微服务中修改对应的配置项是非常麻烦的,改完之后还需要重新部署项目。
Spring Cloud 提供了对应的解决方案,即 SpringCloud Config,通过服务端可以为多个客户端提供配置服务。
Spring Cloud Config 可以将配置文件存放在本地,也可以存放在远程 Git 仓库中。拿远程 Git 仓库来说,具体的操作思路是将所有的外部配置文件集中放置在 Git 仓库中,然后创建 Config Server,通过它来管理所有的配置文件,需要更改某个微服务的配置信息时,只需要在本地进行修改,然后推送到远程 Git 仓库即可,所有的微服务实例都可以通过 Config Server 来读取对应的配置信息。
本地文件系统实现
-
创建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
-
创建 application.yml
server:
port: 8762
spring:
application:
name: nativeconfigserver
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/shared
注解说明
profiles.active
:配置文件的获取方式
cloud.config.server.native.search-locations
:本地配置文件存放的路径
-
resources 路径下创建 shared 文件夹,并在此路径下创建 configclient-dev.yml。
server:
port: 8070
foo: foo version 1
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class NativeConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(NativeConfigServerApplication.class,args);
}
}
注解说明
@EnableConfigServer
:声明配置中心。
创建客户端读取本地配置中心的配置文件
-
创建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
-
创建 bootstrap.yml,配置读取本地配置中心的相关信息。
spring:
application:
name: configclient
profiles:
active: dev
cloud:
config:
uri: http://localhost:8762
fail-fast: true
注解说明
cloud.config.uri
:本地 Config Server 的访问路径
cloud.config.fail-fase
:设置客户端优先判断 Config Server 获取是否正常。
通过 spring.application.name
结合 spring.profiles.active
拼接目标配置文件名,configclient-dev.yml,去 Config Server 中查找该文件。
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NativeConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(NativeConfigClientApplication.class,args);
}
}
-
Handler
package com.janeroad.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/native")
public class NativeConfigHandler {
@Value("${server.port}")
private String port;
@Value("${foo}")
private String foo;
@GetMapping("/index")
public String index(){
return this.port+"-"+this.foo;
}
}
依次启动 NativeConfigServer、ConfigClient,访问 http://localhost:8070/native/index

读取本地配置成功。
远程配置实现

首先将配置文件上传到GitHub仓库。
-
创建配置文件,上传至 GitHub
server:
port: 8070
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: configclient
-
创建 Config Server,新建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
-
创建配置文件 application.yml
server:
port: 8888
spring:
application:
name: configserver
cloud:
bus:
trace:
enable: true
config:
server:
git:
uri: https://github.com/JaneRoad/springcloud.git
searchPaths: config
username: 马赛克#自己的账号
password: 马赛克#自己的密码
label: master
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
**创建 Config Client **
-
创建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
-
创建 bootstrap.yml
spring:
cloud:
config:
name: configclient
label: master
discovery:
enabled: true
service-id: configserver
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
注解说明
spring.cloud.config.name
:当前服务注册在 Eureka Server 上的名称,与远程仓库的配置文件名对应。
spring.cloud.config.label
:Git Repository 的分支。
spring.cloud.config.discovery.enabled
:是否开启 Config 服务发现支持。
spring.cloud.config.discovery.service-id
:配置中心在 Eureka Server 上注册的名称。
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
-
Handler
package com.janeroad.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloHandler {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
依次启动注册中心,configserver,configclient,如下图所示

通过控制台输出信息可以看到,configclient已经读取到了Git仓库中的配置信息。
通过Postman工具访问http://localhost:8070/config/index,如下图所示

服务跟踪
一个分布式系统中往往会部署很多个微服务,这些服务彼此之间会相互调用,整个过程就会较为复杂,在进行问题排查或者优化的时候工作量就会比较大,需要能准确跟踪每一个网络请求的整个运行流程,获取它在每个微服务上的访问情况、是否有延迟、耗费时间等,这样的话分析系统性能,排查解决问题就会容易很多, 使用Zipkin 组件可以来实现服务跟踪。
什么是 Zipkin
Zipkin 是一个可以采集并且跟踪分布式系统中请求数据的组件,可以为开发者采集某个请求在多个微服务之间的追踪数据,并以可视化的形式呈现出来,让开发者可以更加直观地了解到请求在各个微服务中所耗费的时间等信息。
ZipKin 组件包括两部分:Zipkin Server 和 Zipkin Client,服务端用来采集微服务之间的追踪数据,再通过客户端完成数据的生成和展示,Spring Cloud 为服务跟踪提供了解决方案,Spring Cloud Sleuth 集成了 Zipkin 组件。
实现 Zipkin Server
-
创建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
-
创建配置文件 application.yml
server:
port: 9090
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class,args);
}
}
注解说明
@EnableZipkinServer
:声明启动 Zipkin Server
实现 Zipkin Client
-
创建 Maven 工程,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
-
创建配置文件 application.yml
server:
port: 8090
spring:
application:
name: zipkinclient
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9090/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
属性说明
spring.sleuth.web.client.enabled
:设置开启请求跟踪
spring.sleuth.sampler.probability
:设置采样比例,默认是 1.0
srping.zipkin.base-url
:Zipkin Server 地址
-
创建启动类
package com.janeroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZipkinClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinClientApplication.class,args);
}
}
-
Handler
package com.janeroad.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
依次启动注册中心、Zipkin、ZipkinClient, 打开浏览器访问 http://localhost:9090/zipkin/,Zipkin 首页,如下图所示

点击 Find Traces 按钮可看到监控数据情况,当前没有监控到任何数据,如下图

访问 http://localhost:8090/zipkin/index

再次刷新 http://localhost:9090/zipkin/可看到监控数据

点击可查看详情,如下图所示。
原文始发于微信公众号(0error):SpringCloud-配置中心和服务跟踪
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/22022.html