SpringCloud-Zipkin

Zipkin

Zipkin 是 Twitter 的一個開源項目,它基於 Google Dapper 實現,它致力於收集服務的定時數據,以解決微服務架構中的延遲問題,包括數據的收集、存儲、查找和展現。

Zipkin和微服務鏈路跟蹤 實例

項目結構

一 新建eurekserver

  1 pom.xml

      

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

   2 application.properties

   

  • server.port=8187
    eureka.instance.hostname=127.0.0.1
    #不要向註冊中心註冊自己
    eureka.client.register-with-eureka=false
    #禁止檢索服務
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://127.0.0.1:8187/eureka
    spring.application.name=eurekserver
    
    eureka.server.enable-self-preservation=false

   3 啓動類

      

  • package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

二   新建服務提供-productservice

   1 pom.xml

     <dependency>

          <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

   </dependency>

  • <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- Add Log4j2 Dependency -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    

   2 application.properties

server.port=8102

#設置應用的名稱
spring.application.name=productservice

        
#服務註冊的Eureka Server地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8187/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#設置註冊ip
#表示是否將自己註冊在EurekaServer上,默認爲true。由於當前應用就是EurekaServer,所以置爲false
eureka.client.register-with-eureka=true
#表示表示是否從EurekaServer獲取註冊信息,默認爲true。單節點不需要同步其他的EurekaServer節點的數據
eureka.client.fetch-registry=true

   

   3 啓動類

   

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {

    private static Logger logger= LoggerFactory.getLogger(DemoApplication.class);

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


    @RequestMapping(value="hello",method = RequestMethod.GET)
    public ResponseEntity<String> hello(){
        logger.info("called by product-service");
        return new ResponseEntity<>("hello productservice!", HttpStatus.OK);
    }
}

     

三   服務調用-orderserverice

   1 pom.xml

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Add Log4j2 Dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

   2 application.properties

   

server.port=8111

#設置應用的名稱

spring.application.name=orderservice


eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8187/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#設置註冊ip
#表示是否將自己註冊在EurekaServer上,默認爲true。由於當前應用就是EurekaServer,所以置爲false
eureka.client.register-with-eureka=true
#表示表示是否從EurekaServer獲取註冊信息,默認爲true。單節點不需要同步其他的EurekaServer節點的數據
eureka.client.fetch-registry=true

   3 啓動類

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {


    private static Logger logger= LoggerFactory.getLogger(DemoApplication.class);

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){ return new RestTemplate();
    }


    @Autowired
    RestTemplate restTemplate;

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


   @RequestMapping(value = "/hello",method = RequestMethod.GET)
   public ResponseEntity<String> hello()
   {
       return new ResponseEntity<String>("hello order service!", HttpStatus.OK);

   }

   @RequestMapping(value = "/product/hello",method = RequestMethod.GET)
   public String productHello() throws InterruptedException {
       Thread.sleep(100);
        logger.info("order-service calling product-service!");
        return restTemplate.getForEntity("http://PRODUCTSERVICE/hello",String.class).getBody();
    }

}

四 zipkinservice 

    

  1 pom.xml

     

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-server</artifactId>
    <version>2.12.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-ui</artifactId>
    <version>2.12.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Add Log4j2 Dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

   2 application.properties

  

spring.application.name=zipkinservice

server.port=9411

management.metrics.web.server.auto-time-requests=false
#設定樣本收集的比率爲100%
spring.sleuth.sampler.percentage=1.0

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8187/eureka
##http://shumeigang:[email protected]:8081/eureka
eureka.instance.prefer-ip-address=true
#設置註冊ip
#表示是否將自己註冊在EurekaServer上,默認爲true。由於當前應用就是EurekaServer,所以置爲false
eureka.client.register-with-eureka=true
#表示表示是否從EurekaServer獲取註冊信息,默認爲true。單節點不需要同步其他的EurekaServer節點的數據
eureka.client.fetch-registry=true

   3日誌文件log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

    <Configuration status="warn">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%m%n" />
            </Console>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="Console" />
            </Root>
        </Loggers>
    </Configuration>

   4 啓動類

   

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import zipkin2.server.internal.EnableZipkinServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZipkinServer
public class DemoApplication {


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

}

運行結果

1運行 eurek   http://127.0.0.1:8187/

 

2 zipkinservice   url    http://127.0.0.1:9411/zipkin/

2 運行 新建服務提供-productservice      http://127.0.0.1:8102/hello

3 運行 服務調用-orderserverice   http://127.0.0.1:8111/product/hello

最後在刷新 zipkinservice 如果沒有出來數據 你可以重新 打開http://127.0.0.1:8111/product/hello  http://127.0.0.1:8111/product/hello

結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章