springboot dubbo应用的链路信息采集——应用搭建

本文已参与腾源会发起的「开源摘星计划」

  • 视频教程

    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

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot集成Dubbo需要引入以下依赖: 1. dubbo-spring-boot-starter:版本为0.2.1.RELEASE,可以在仓库中查看具体的依赖信息\[1\]。 2. dubbo:具体版本可以在dubbo-spring-boot-starter的依赖中查看\[1\]。 3. log4j和slf4j的依赖。 4. spring-boot-starter-web:因为是以一个web服务进行启动,所以需要引入该依赖\[1\]。 在application.properties文件中,需要进行以下配置: 1. 配置dubbo的端口号和协议\[2\]。 2. 配置dubbo的接口所在包位置\[2\]。 在pom.xml文件中,需要引入以下依赖: 1. dubbo-interface模块:用于定义Dubbo的接口\[3\]。 2. spring-boot-starter和spring-boot-starter-web:用于启动Spring Boot应用\[3\]。 3. spring-boot-starter-dubbo:用于集成Dubbo和Spring Boot\[3\]。 以上是Spring Boot集成Dubbo的基本配置和依赖引入。具体的配置和使用可以根据项目需求进行调整。 #### 引用[.reference_title] - *1* [SpringBoot集成Dubbo](https://blog.csdn.net/MX__LL/article/details/125537275)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot整合dubbo(一)](https://blog.csdn.net/chenguixu/article/details/127867833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hotwifi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值