zipkin及sleuth的使用

父工程


<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.0modelVersion>

    <groupId>com.xysd.cloudgroupId>
    <artifactId>cloudexecise2artifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>cloudproviderproductmodule>
        <module>cloudconsumermodule>
        <module>clouderurekamodule>
    modules>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-loggingartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.4version>
            <scope>providedscope>
        dependency>
    dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <finalName>cloudexecise2finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                configuration>
            plugin>
        plugins>
    build>
project>

注册中心 cloudeureka

依赖


<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">
    <parent>
        <artifactId>cloudexecise2artifactId>
        <groupId>com.xysd.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-erurekaartifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>

    <build>
        <finalName>eureka-serverfinalName>
        <plugins>
            <plugin> 
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration> 
                    <mainClass>com.xysd.cloud.EurekaAppmainClass>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

配置application.yml


server:
  port: 8761
eureka:
  instance:
    hostname: localhost   # 定义 Eureka 实例所在的主机名称
  client:
    fetch-registry: false #是否从eureka中获取注册信息
    register-with-eureka: false #是否将自己注册到注册中心
    service-url:
      defaultZone: http://localhost:8761/eureka/

代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApp.class,args);
    }
}

生产者cloudproviderproduct


<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">
    <parent>
        <artifactId>cloudexecise2artifactId>
        <groupId>com.xysd.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-provider-productartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>com.xysd.cloudgroupId>
            <artifactId>cloud-apiartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.31version>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
    
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
 
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-sleuthartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zipkinartifactId>
        dependency>

    dependencies>

    <build>
        <finalName>cloud-eurekafinalName>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <configuration>
                    <delimiters>
                        <delimiter>$delimiter>
                    delimiters>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                configuration>
            plugin>
        plugins>
    build>
project>

配置

server:
 port: 8080
mybatis:  #mybatyis的配置
  mapper-locations: classpath:mapper/*.xml
spring:
 datasource:
   type: com.alibaba.druid.pool.DruidDataSource # 配置当前要使用的数据源的操作类型
   driver-class-name: com.mysql.cj.jdbc.Driver # 配置MySQL的驱动程序类
   url: jdbc:mysql://localhost:3306/cloud?serverTimezone=GMT%2B8 # 数据库连接地址
   username: root # 数据库用户名
   password: root # 数据库连接密码
 application:
   name: cloudproviderproduct
 sleuth:
   web:
     client:
       enabled: true #web开启sleuth功能
   sampler:
     probability: 1.0 #可以设置为小数,最大值为1.0,当设置为1.0时就是链路数据100%收集到zipkin-server,当设置为0.1时,即10%概率收集链路数据
 zipkin:
   base-url: http://localhost:9411 # 指定了 Zipkin 服务器的地址


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    instance-id: cloudproviderproduct
    prefer-ip-address: true #显示ip
info:
  app.name: cloudproviderproduct
  company.name: xysd
  build.artifactId: $project.artifactId$
  build.modelVersion: $project.modelVersion$

代码

import com.xysd.cloud.entity.Product;

import java.util.List;

public interface ProductMapper {
    boolean create(Product product);
    public Product findById(Long id);
    public List<Product> findAll();
}


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xysd.cloud.mapper.ProductMapper">

    <resultMap id="productResult" type="com.xysd.cloud.entity.Product">
        <id column="productId" property="productId"/>
        <result column="productName" property="productName"/>
        <result column="productDesc" property="productDesc"/>
    resultMap>
    <select id="findById" resultMap="productResult" parameterType="long">
        select productId,productName,productDesc from product WHERE productId=#{id} ;
    select>
    <select id="findAll" resultMap="productResult">
        SELECT productId,productName,productDesc from product;
    select>
    <insert id="create" parameterType="com.xysd.cloud.entity.Product">
        INSERT INTO product(productName,productDesc) VALUES (#{productName},database()) ;
    insert>
mapper>
package com.xysd.cloud.service;

import com.xysd.cloud.entity.Product;

import java.util.List;

public interface ProductService {
    Product get(long id);
    boolean add(Product product);
    List<Product> list();
}

package com.xysd.cloud.service.impl;

import com.xysd.cloud.entity.Product;
import com.xysd.cloud.mapper.ProductMapper;
import com.xysd.cloud.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product get(long id) {
        return productMapper.findById(id);
    }

    @Override
    public boolean add(Product product) {
        System.out.println(product);
        return productMapper.create(product);
    }

    @Override
    public List<Product> list() {
        return productMapper.findAll();
    }
}

package com.xysd.cloud.controller;

import com.xysd.cloud.entity.Product;
import com.xysd.cloud.service.ProductService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public Object get(@PathVariable("id") long id) {
        return this.productService.get(id);
    }

    @RequestMapping(value = "/add")
    public Object add(@RequestBody Product product) {
        return this.productService.add(product);
    }

    @RequestMapping(value = "/list")
    public Object list() {
        return this.productService.list();
    }
}
package com.xysd.cloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
@MapperScan("com.xysd.cloud.mapper")
public class ProductApp {
    public static void main(String[] args) {
        SpringApplication.run(ProductApp.class,args);
    }
}

消费者 cloudconsumer

依赖


<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">
    <parent>
        <artifactId>cloudexecise2artifactId>
        <groupId>com.xysd.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-consumerartifactId>
    <dependencies>
        <dependency>
            <groupId>com.xysd.cloudgroupId>
            <artifactId>cloud-apiartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
    
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-sleuthartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zipkinartifactId>
        dependency>


    dependencies>

    <build>
        <finalName>cloud-eurekafinalName>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <configuration>
                    <delimiters>
                        <delimiter>$delimiter>
                    delimiters>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                configuration>
            plugin>
        plugins>
    build>
project>

配置

server:
  port: 8083

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    #fetch-registry: true #是否从eureka中获取注册信息
    #register-with-eureka: false #是否将自己注册到注册中心
  instance:
    instance-id: cloudconsumerproduct
    prefer-ip-address: true #显示ip
    #lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    #lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)

spring:
  application:
    name: cloudconsumerproduct
  sleuth:
    web:
      client:
        enabled: true  #web开启sleuth功能
    sampler:
      probability: 1.0 #可以设置为小数,最大值为1.0,当设置为1.0时就是链路数据100%收集到zipkin-server,当设置为0.1时,即10%概率收集链路数据
  zipkin:
    base-url: http://localhost:9411 # 指定了 Zipkin 服务器的地址



info:
  app.name: cloudproviderproduct
  company.name: xysd
  build.artifactId: $project.artifactId$
  build.modelVersion: $project.modelVersion$

feign:
  client:
    config:
      default:
        connectTimeout: 2000
        readTimeout: 2000

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 2000


代码

package com.xysd.cloud.service;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "cloudproviderproduct",fallback = ProductFeignClientCallBack.class)
public interface ProductFeignClient {

    @RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET)
    public Object get(@PathVariable("id") long id);
}

package com.xysd.cloud.service;

import com.xysd.cloud.entity.Product;
import org.springframework.stereotype.Component;

@Component
public class ProductFeignClientCallBack implements ProductFeignClient {

    /**
     * 熔断降级的方法
     */
    @Override
    public Object get(long id) {
        Product p = new Product();
        p.setProductName("服务降级学习");
        p.setProductDesc("这是基于feign的服务降级测试");
        return p;
    }
}

package com.xysd.cloud.controller;

import com.xysd.cloud.entity.Product;
import com.xysd.cloud.service.ProductFeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerProductController {

    @Resource
    private ProductFeignClient productFeignClient;

    @RequestMapping("/product/getFeign")
    public Object getProduct2(@RequestParam(value = "id") long id) throws InterruptedException {
       return productFeignClient.get(id);
    }
}
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.xysd.cloud.service")
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }
}

测试

http://localhost:8083/consumer/product/getFeign?id=11010
zipkin及sleuth的使用_第1张图片
当前springcloud版本,zipkin server 需要去官网下载jar。
windows下如果有gitbash,可以使用它下载

curl -sSL https://zipkin.io/quickstart.sh | bash -s

进入zipkin.jar的目录,启动命令

java -jar zipkin.jar

通过java -jar zipkin.jar的方式启动之后,在浏览器上访问lcoalhost:9411,测试zipkin服务是否启动

你可能感兴趣的:(springcloud,spring,cloud)