栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

nacos 集成 zipkin sleuth实现链路追踪(入门篇)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nacos 集成 zipkin sleuth实现链路追踪(入门篇)


声明:链路追踪属于微服务的一部分,微服务系列博文陆续会出,可以帮助小伙伴们学习微服务一站式从入门到精通。

文章目录
          • 一、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. 父项目依赖
  
        
        UTF-8
        UTF-8
        1.8

        1.0-SNAPSHOT
        1.0-SNAPSHOT
        Hoxton.SR9
        2.2.6.RELEASE
    

    

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
    

    

        
            
            
                com.gblfy
                api-serv
                ${api.version}
            
            
            
                com.gblfy
                common
                ${common.version}
            
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    
2.2. gateway依赖
  	   
            org.springframework.cloud
            spring-cloud-starter-gateway
        
2.3. gateway配置
server:
  port: 80
spring:
  cloud:
    nacos:
      discovery:
        service: gateway-serv
      server-addr: localhost:8848
    gateway:
      discovery:
        locator: # 是否与服务发现组件进行结合,通过 serviceId 转发到具体服务实例。
          lowerCaseServiceId: true # 是否开启基于服务发现的路由规则
          enabled: true # 是否将服务名称转小写
      routes:
        # 订单服务
        - id: order-serv
          uri: lb://order-serv
          predicates:
            - Path=/order/**
          filters:
            - StripPrefix=1
        # 商品服务
        - id: product-serv
          uri: lb://product-serv
          predicates:
            - Path=/product/**
          filters:
            - StripPrefix=1
  zipkin:
    base-url: http://zipkin.server.com:9411/ # 服务端地址
    discovery-client-enabled: false
    sender:
      type: web                      # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据
  sleuth:
    sampler:
      probability: 1.0            # 收集数据百分比,默认 0.1(10%)

三、订单服务 3.1. order依赖
 
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
3.2. order配置
server:
  port: 8000
spring:
  application:
    name: order-serv
  cloud:
    nacos:
      discovery:
        service: order-serv
      server-addr: localhost:8848
  zipkin:
    base-url: http://zipkin.server.com:9411/ # 服务端地址
    discovery-client-enabled: false
    sender:
      type: web                      # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据
  sleuth:
    sampler:
      probability: 1.0            # 收集数据百分比,默认 0.1(10%)

#请求处理的超时时间
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

# feign 配置
feign:
  sentinel:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  compression:
    request:
      enabled: true
    response:
      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 map = new HashMap<>();

        map.put("商品名称", productName);
        map.put("用户名称", userName);

        return JSON.toJSONString(map);
    }
}
四、产品服务 4.1. product依赖
 
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
4.2. 配置
server:
  port: 8000
spring:
  application:
    name: order-serv
  cloud:
    nacos:
      discovery:
        service: order-serv
      server-addr: localhost:8848
  zipkin:
    base-url: http://zipkin.server.com:9411/ # 服务端地址
    discovery-client-enabled: false
    sender:
      type: web                      # 数据传输方式,web 表示以 HTTP 报文的形式向服务端发送数据
  sleuth:
    sampler:
      probability: 1.0            # 收集数据百分比,默认 0.1(10%)

#请求处理的超时时间
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

# feign 配置
feign:
  sentinel:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  compression:
    request:
      enabled: true
    response:
      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依赖
 
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
5.1.user配置
server:
  port: 15000
spring:
  cloud:
    nacos:
      discovery:
        service: user-serv
      server-addr: localhost:8848
  zipkin:
    base-url: http://zipkin.server.com:9411/ # 服务端地址
    discovery-client-enabled: false
    sender:
      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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号