本文已参与腾源会发起的「开源摘星计划」
- 视频教程
skywalking8.7.0搭建之应用搭建
- 搭建目标
实现一个springboot dubbo的应用,来模拟生产应用,产生链路信息。dubbo是分布式服务的rpc调用框架。这里实现了一个服务调用者和一个服务提供者,因此需要建立两个工程。采用maven多模块方式,可以在一个界面同时开发上述两个工程,方便开发和编译打包。
- Idea多模块工程新建
启动idea,安装好maven。打开idea配置界面file->settings->搜索maven,设置:
新建maven工程
File->New->Project
创建主工程:psbc
编辑主工程的pom.xml
在主工程中增加依赖,以支持dubbo分布式调度框架,这样子工程也可以共享主工程中的依赖。
增加:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
增加:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>8.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
然后再新建子工程psbc-provider:
同样的方法创建psbc-consumer。完成后如图:
为了避免运行报错:没有主清单属性。在两个工程中pom.xml增加配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
psbc-consumer中写三个类
接口类:
package com.psbc.api;
public interface IHelloService {
public String hello();
}
controller类:
package com.psbc;
import com.alibaba.dubbo.config.annotation.Reference;
import com.psbc.api.IHelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Reference(url = "dubbo://node2:20880")
private IHelloService helloService;
@GetMapping("/hello")
public String hello() throws InterruptedException {
Thread.sleep(1000);
String result = helloService.hello();
Thread.sleep(2000);
return result;
}
@GetMapping("/sleep/{millisecond}")
public String sleep(@PathVariable("millisecond") Integer millisecond) throws InterruptedException {
Thread.sleep(millisecond);
String result = helloService.hello();
return result;
}
}
springboot主程序:
package com.psbc;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
application.properties
spring.application.name=skywalking_dubbo_consumer
server.port=8085
psbc-provider中写三个类
接口类:
package com.psbc.api;
public interface IHelloService {
public String hello();
}
服务类:
package com.psbc;
import com.alibaba.dubbo.config.annotation.Service;
import com.psbc.api.IHelloService;
import org.springframework.stereotype.Component;
@Service(interfaceClass = IHelloService.class)
@Component
public class HelloService implements IHelloService {
@Override
public String hello() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if((int) (Math.random() * 200) == 1) {
int i = 1 / 0;
}
return "This is server! server port is 8086";
}
}
springboot主程序:
package com.psbc;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
application.properties
spring.application.name=skywalking_dubbo_provider
spring.dubbo.server=true
spring.dubbo.registry=N/A
server.port=8086
程序解释:psbc-provider通过dubbo的注释在本地注册了一个服务,外部可以通过rpc调用该服务:
@Service(interfaceClass = IHelloService.class)
psbc-consumer通过注释发现服务
@Reference(url = "dubbo://node2:20880")
private IHelloService helloService;
这样就可以通过helloService去调用外部服务。
编译为jar文件:
在项目目录下会生成target目录,其中有生成的应用包例如psbc-provider-1.0-SNAPSHOT.jar。ftp到虚拟机中并启动应用:
nohup java -javaagent:/root/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar -jar psbc-provider-1.0-SNAPSHOT.jar &
nohup java -javaagent:/root/apache-skywalking-apm-bin-es7/agent/skywalking-agent.jar -jar psbc-consumer-1.0-SNAPSHOT.jar &
ab -n3000 -c30 http://node1:8085/hello