声明:链路追踪属于微服务的一部分,微服务系列博文陆续会出,可以帮助小伙伴们学习微服务一站式从入门到精通。
文章目录
- 一、zipkin 服务端
- 1. 下载
- 2. 前台运行
- 3. 服务端访问
- 二、网关服务
- 2.1. 父项目依赖
- 2.2. gateway依赖
- 2.3. gateway配置
- 三、订单服务
- 3.1. order依赖
- 3.2. order配置
- 3.3. 控制层
- 四、产品服务
- 4.1. product依赖
- 4.2. 配置
- 4.3. 控制层
- 五、用户服务
- 5.1.user依赖
- 5.1.user配置
- 5.3.控制层
- 六、测试验证
- 6.1. 访问
- 6.2. 控制出台日志
- 6.3. 服务端监控
一、zipkin 服务端
1. 下载
https://repo1.maven.org/maven2/io/zipkin/zipkin-server
2. 前台运行
java -jar zipkin-server-2.23.4-exec.jar
3. 服务端访问
http://zipkin.server.com:9411/zipkin/
二、网关服务
2.1. 父项目依赖
<properties><!--全局版本统一管理--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><common.version>1.0-SNAPSHOT</common.version><api.version>1.0-SNAPSHOT</api.version><spring-cloud.version>Hoxton.SR9</spring-cloud.version><spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version></properties><dependencies><!--服务注册发现--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- spring cloud zipkin 依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency></dependencies><dependencyManagement><dependencies><!--公共模块--><dependency><groupId>com.gblfy</groupId><artifactId>api-serv</artifactId><version>${api.version}</version></dependency><!--公共模块--><dependency><groupId>com.gblfy</groupId><artifactId>common</artifactId><version>${common.version}</version></dependency><!--https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!--spring-cloud-alibaba 版本控制--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
2.2. gateway依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
2.3. gateway配置
server:port: 80
spring:cloud:nacos:discovery:service: gateway-servserver-addr: localhost:8848gateway:discovery:locator: # 是否与服务发现组件进行结合,通过 serviceId 转发到具体服务实例。lowerCaseServiceId: true # 是否开启基于服务发现的路由规则enabled: true # 是否将服务名称转小写routes:# 订单服务- id: order-servuri: lb://order-servpredicates:- Path=/order/**filters:- StripPrefix=1# 商品服务- id: product-servuri: lb://product-servpredicates:- Path=/product/**filters:- StripPrefix=1zipkin:base-url: http://zipkin.server.com:9411/ # 服务端地址discovery-client-enabled: falsesender:type: web # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据sleuth:sampler:probability: 1.0 # 收集数据百分比,默认 0.1(10%)
三、订单服务
3.1. order依赖
<!-- SpringCloud Openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--springmvc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
3.2. order配置
server:port: 8000
spring:application:name: order-servcloud:nacos:discovery:service: order-servserver-addr: localhost:8848zipkin:base-url: http://zipkin.server.com:9411/ # 服务端地址discovery-client-enabled: falsesender:type: web # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据sleuth:sampler:probability: 1.0 # 收集数据百分比,默认 0.1(10%)#请求处理的超时时间
ribbon:ReadTimeout: 10000ConnectTimeout: 10000# feign 配置
feign:sentinel:enabled: trueokhttp:enabled: truehttpclient:enabled: falseclient:config:default:connectTimeout: 10000readTimeout: 10000compression:request:enabled: trueresponse:enabled: true# 暴露监控端点
management:endpoints:web:exposure:include: '*'
3.3. 控制层
package com.gblfy.order.controller;import com.alibaba.fastjson.JSON;
import com.gblfy.api.RemoteProductService;
import com.gblfy.api.RemoteUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OrderController {private final RemoteProductService productService;private final RemoteUserService userService;//http://localhost:8000/order/create?productId=11&userId=11222@GetMapping("/order/create")public String createOrder(@RequestParam Integer productId, @RequestParam Integer userId) {// 调用商品服务,通过商品ID获取商品名称String productName = productService.getProductName(productId);// 调用用户服务,通过用户ID获取用户名称String userName = userService.geUserName(userId);HashMap<String, Object> map = new HashMap<>();map.put("商品名称", productName);map.put("用户名称", userName);return JSON.toJSONString(map);}
}
四、产品服务
4.1. product依赖
<!-- SpringCloud Openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--springmvc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
4.2. 配置
server:port: 8000
spring:application:name: order-servcloud:nacos:discovery:service: order-servserver-addr: localhost:8848zipkin:base-url: http://zipkin.server.com:9411/ # 服务端地址discovery-client-enabled: falsesender:type: web # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据sleuth:sampler:probability: 1.0 # 收集数据百分比,默认 0.1(10%)#请求处理的超时时间
ribbon:ReadTimeout: 10000ConnectTimeout: 10000# feign 配置
feign:sentinel:enabled: trueokhttp:enabled: truehttpclient:enabled: falseclient:config:default:connectTimeout: 10000readTimeout: 10000compression:request:enabled: trueresponse:enabled: true# 暴露监控端点
management:endpoints:web:exposure:include: '*'
4.3. 控制层
package com.gblfy.product.controller;import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ProductController {//http://localhost:9000/product/" + productId@GetMapping("/product/{productId}")public String getProductName(@PathVariable Integer productId) {// 调用用户服务,通过用户ID获取用户名称return "IPHOME13 的商品ID:" + productId;}
}
五、用户服务
5.1.user依赖
<!-- SpringCloud Openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--springmvc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
5.1.user配置
server:port: 15000
spring:cloud:nacos:discovery:service: user-servserver-addr: localhost:8848zipkin:base-url: http://zipkin.server.com:9411/ # 服务端地址discovery-client-enabled: falsesender:type: web # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据sleuth:sampler:probability: 1.0 # 收集数据百分比,默认 0.1(10%)application:name: user-serv
5.3.控制层
package com.gblfy.user.controller;import com.gblfy.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UserController {private final UserService userService;@GetMapping("/user/{userId}")public String getUserName(@PathVariable Integer userId) {return userService.getUserName(userId);}
}
六、测试验证
6.1. 访问
http://localhost/order/order/create?productId=22&userId=11
6.2. 控制出台日志
6.3. 服务端监控
开源地址:
https://gitee.com/gb_90/micro-service-parent