3.Skywalking高级
3.1 Rpc调用监控
Skywalking(6.5.0)支持的Rpc框架有以下几种:
- Dubbo 2.5.4 -> 2.6.0 (原生的Dubbo)
- Dubbox 2.8.4 (当当网改编后的Dubbo)
- Apache Dubbo 2.7.0 (捐献至Apache后的Dubbo)
- Motan 0.2.x -> 1.1.0
- gRPC 1.x
- Apache ServiceComb Java Chassis 0.1 -> 0.5,1.0.x
- SOFARPC 5.4.0
接下来为大家提供一些简单使用Dubbo服务调用的项目代码演示, 大家可以参考下面简单版 或者详细请参照Demo中的脚手架示例scaffold-dubbo-demo
3.1.1 服务提供方
pom文件:
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sf.saas</groupId>
<artifactId>skywalking_dubbo_provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>skywalking_dubbo_provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--添加springboot和dubbo集成配置-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
代码语言:javascript复制spring.application.name=skywalking_dubbo_provider spring.dubbo.server=true
spring.dubbo.registry=N/A
server.port=8086
为了简化环境搭建,采用了本地直接调用的方式,所以将注册中心写成N/A表示不注册到注册中 心。
IHelloService接口:.
代码语言:javascript复制public interface IHelloService {
String hello();
}
IHelloServiceImpl实现类
代码语言:javascript复制import com.alibaba.dubbo.config.annotation.Service; import com.itcast.api.IHelloService;
import org.springframework.stereotype.Component;
@Service(interfaceClass = IHelloService.class)
@Component
public class HelloServiceImpl implements IHelloService {
@Override
public String hello() {
return "hello skywalking";
}
}
SkywalkingDubboProviderApplication启动类
代码语言:javascript复制import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//添加dubbo生效注解
@EnableDubboConfiguration
public class SkywalkingDubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SkywalkingDubboProviderApplication.class, args);
}
}
需要添加@EnableDubboConfiguration注解
3.1.2 服务消费方
pom文件:
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sf.saas</groupId>
<artifactId>skywalking_dubbo_consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>skywalking_dubbo_consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
代码语言:javascript复制spring.application.name=skywalking_dubbo_consumer
server.port=8085
IHelloService接口
代码语言:javascript复制public interface IHelloService {
String hello();
}
TestController
代码语言:javascript复制import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.api.IHelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Reference(url = "dubbo://127.0.0.1:20880")
private IHelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.hello();
}
}
采用直连而非从注册中心获取服务地址的方式,在@Reference注解中声明
url = “dubbo://127.0.0.1:20880”
SkywalkingDubboConsumerApplication
代码语言:javascript复制import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//添加dubbo生效注解
@EnableDubboConfiguration
public class SkywalkingDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SkywalkingDubboConsumerApplication.class, args);
}
}
需要添加@EnableDubboConfiguration注解
3.1.3 部署方式
1、将上述两个项目使用package打包后上传至/usr/local/skywalking目录下。 2、首先我们复制两份agent,防止使用的冲突。
代码语言:javascript复制cd /usr/local/skywalking/apache-skywalking-apm-bin/ cp -r agent agent_dubbo_provider
cp -r agent agent_dubbo_consumer
vi agent_dubbo_provider/config/agent.config
修改agent_dubbo_provider配置中的应用名为:
代码语言:javascript复制# The service name in UI
agent.service_name=${SW_AGENT_NAME:dubbo_provider}
接着修改agent_dubbo_consumer
代码语言:javascript复制vi agent_dubbo_consumer/config/agent.config
修改应用名
代码语言:javascript复制# The service name in UI
agent.service_name=${SW_AGENT_NAME:dubbo_consumer}
3、先启动provider,等待启动成功
代码语言:javascript复制#切换到目录下
cd /usr/local/skywalking
#启动provider
java -javaagent:/usr/local/skywalking/apache-skywalking-apm- bin/agent_dubbo_provider/skywalking-agent.jar -jar skywalking_dubbo_provider.jar &
出现如图所示内容,应用就已经启动成功了
4、启动consumer,等待启动成功。
代码语言:javascript复制#启动consumer
java -javaagent:/usr/local/skywalking/apache-skywalking-apm- bin/agent_dubbo_consumer/skywalking-agent.jar -jar skywalking_dubbo_consumer.jar &
5、调用接口,接口地址为:http://虚拟机IP地址:8085/hello
6、此时如果页面显示
那么dubbo的调用就成功了。
7、打开skywalking查看dubbo调用的监控情况。
仪表盘
目前 dubbo_provider和 dubbo_consumer的服务已经出现,同时出现了两个接口分别是:
- /hello接口,是浏览器调用dubb_consumer的http接口
- IHelloService.hello()是dubbo_consumer调用dubbo_provider的dubbo接口
拓扑图
该图中已经表示出了一个调用的链路关系:
代码语言:javascript复制User(浏览器) ----> dubber_consumer ----> dubbo_provider
并且在服务的上方标识出了每个服务代表的内容,dubbo_consumer是SpringMvc的服务,而 dubbo_provider是Dubbo的服务。
追踪
追踪图中显示本次调用耗时49ms,其中dubbo接口耗时29ms,那么另外的20ms其实是SpringMVC接 口的开销,这样就能很好的评估出每个环节的耗时时间。