SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪

这里存储方式笔者选择使用ElasticSearch,具体版本是6.5.0,ElasticSearch的构建方式选择使用Docker

docker pull elasticsearch:6.5.0

docker run -d --restart=always --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.5.0

ElasticSearch6.5.0单节点版已经构建完成,为了方便后续操作,需要修改一个ElasticSearch的命名,输入命令docker exec -it es /bin/bash进入容器文件目录,输入vi config/elasticsearch.yml进入ElasticSearch配置文件,修改cluster.name的值,笔者这里修改为CollectorDBCluster,修改完成后,保存当前修改,输入exit退出容器文件目录,输入docker restart es重启当前容器,在浏览器输入http://192.168.44.128:9200/,看到如下信息可以证明ElasticSearch6.5.0单节点版已经在正常的运行了。

SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第1张图片

Skywalking构建,进入Skywalking官网,进入下载页面(http://skywalking.apache.org/downloads/ ),如图: 

SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第2张图片

我下载的6.5,放到usr/src下

tar -zxvf apache-skywalking-apm-6.5.0.tar.gz 

  • agent:探针相关,后面会做更加详细的介绍。
  • bin:这里放的是oapService和webappService的启动脚本,当然也有执行两个脚本的合并脚本startup.sh
  • config:这里主要存放的是collector的配置信息,我们需要修改这里的application.yml中的有关ElasticSearch的配置,如下图:
  • SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第3张图片SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第4张图片

 

修改storage.elasticsearch.nameSpace为我们前面构建ElasticSearch设置的cluster.name,笔者这里的值为CollectorDBCluster,同时修改storage.elasticsearch.clusterNodes为我们当前构建的ElasticSearch的地址。

  • logs:存放collector和webapp-ui生成的日志。
  • webapp:这里存放的是Skywalking展示UI的jar和配置文件。

Skywalking中默认使用的端口有8080、11800、12800,请保证这些端口未被占用,如需修改,可以修改config目录中的application.ymlwebapp目录中的webapp.yml

接下来启动collector和webapp-ui,进入bin目录中,执行命令./startup.sh,如:

 

打开浏览器访问http://192.168.44.128:8080/,可以看到webapp-ui的仪表盘,如图:

SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第5张图片 

我们将创建4个工程,分别为Zuul-Service、Eureka-Service、Consumer-Service和Provider-Service

Zuul,pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    Zuul-Service

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application

server:
  port: 8080
spring:
  application:
    name: spring-cloud-zuul-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ribbon:
    eager-load:
      enabled: true
  routes:
    spring-cloud-consumer-server:
      path: /client/**
      serviceId: spring-cloud-consumer-server

启动类 

package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication {

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

}

eureka,pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    Eureka-Service

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

application

server:
  port: 8761
spring:
  application:
    name: spring-cloud-eureka-server
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    lease-expiration-duration-in-seconds: 90
    lease-renewal-interval-in-seconds: 30
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 60000
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

package com.atguigu;

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

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

Provider,pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    Provider-Service

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

application

server:
  port: 7000
spring:
  application:
    name: spring-cloud-provider-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true

启动类

package com.atguigu;

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

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {

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

}

控制器

package com.atguigu.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(String name) {
        return "Hello, name is " + name;
    }
}

Consumer,pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    Consumer-Service

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

applicationy.yml

server:
  port: 8000
spring:
  application:
    name: spring-cloud-consumer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  instance:
    prefer-ip-address: true

控制器

package com.atguigu.controller;

import com.atguigu.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@RestController
public class HelloController {

    @Autowired
    HelloRemote helloRemote;

    @GetMapping("/hello")
    public String hello(String name){
        return helloRemote.hello(name);
    }
}

service层

package com.atguigu.remote;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@FeignClient(name = "spring-cloud-provider-server")
@Component
public interface HelloRemote {

    @GetMapping("/hello")
    String hello(@RequestParam("name") String name);
}

启动类

package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/22
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerApplication {

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

}

这里我们需要使用到Skywalking的探针agent,我们在工程根目录中新建一个文件夹,命名为skywalking,讲刚才解压的Skywalking中的agent整个文件夹copy到skywalking,这里我们启动时只需要配置javaagen命令加载agent探针即可,在idea中使用需要修改启动配置,点击右上角的Edit Configurations...,在打开的窗口中选择Environment->VM Options,配置如下脚本:

-javaagent:D:\project\cloud2020\skywalking\agent\skywalking-agent.jar
-Dskywalking.agent.service_name=zuul-service
-Dskywalking.collector.backend_service=192.168.184.128:11800

SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第6张图片

顺次启动四个工程后,使用浏览器访问:http://localhost:8080/client/hello?name=spring,多刷新几次后,我们再使用浏览器访问http://192.168.44.128:8080/,如:

SpringCloud第十八节内容精简,SpringCloud 整合Skywalking链路跟踪_第7张图片

你可能感兴趣的:(SpringCloud)