都dubbo3了,别再用xml了配置dubbo服务了

这里是weihubeats ,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党

背景

最近项目再准备升级dubbo3,所以打算简单实现一个dubbo3的demo。

来学习一下dubbo

dubbo3

dubbo3主要是为了融入云原生打造的

Dubbo 3 提供的核心特性列表,主要包括四部分。

  1. 全新服务发现模型。应用粒度服务发现,面向云原生设计,适配基础设施与异构系统;性能与集群伸缩性大幅提升。
  2. 下一代 RPC 协议 Triple。基于 HTTP/2 的 Triple 协议,兼容 gRPC;网关穿透性强、多语言友好、支持 Reactive Stream。
  3. 统一流量治理模型。面向云原生流量治理,SDK、Mesh、VM、Container 等统一治理规则;能够支持更丰富的流量治理场景。
  4. Service Mesh。在最新的3.1.0的版本中支持Sidecar Mesh 与 Proxyless Mesh,提供更多架构选择,降低迁移、落地成本。

dubbo3实践

目前官方已经给我们总结很多大公司的一些dubbo3使用案例,如果需要升级的,可以参考这些案例

dubbo3实践

本次打算基于spring boot + dubbo3做一个简单的demo

项目结构

dubbo版本

  • 3.3.0-beta.1

interface

就是公共接口,给双方公用的

  • DemoService
java 复制代码
public interface DemoService {

    String sayHello(String name);
}

provider

  1. 添加依赖
xml 复制代码
       <dependency>
            <groupId>io.github.weihubeats</groupId>
            <artifactId>spring-boot-dubbo-3x-interface</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId>
        </dependency>
  1. 启动类添加@EnableDubbo
java 复制代码
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  1. rpc服务类添加@DubboService注解
java 复制代码
@DubboService
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
  1. application.yml
application 复制代码
dubbo:
  application:
    name: dubbo-springboot-demo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181

这里我们使用dubbo协议,不使用Triple协议,启动端口使用-1,随机。zookeeper作为注册中心,默认使用本地的zookeeper

consumer

  1. 添加依赖

同上面provider

  1. 启动类添加@EnableDubbo

同上面provider

  1. 添加一个定时任务定时调用dubboRPC服务进行测试
java 复制代码
@Component
public class Task implements CommandLineRunner {
    @DubboReference
    private DemoService demoService;

    @Override
    public void run(String... args) throws Exception {
        String result = demoService.sayHello("world");
        System.out.println("Receive result ======> " + result);

        new Thread(()-> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
}
  1. application.yml
application 复制代码
dubbo:
  application:
    name: dubbo-springboot-demo-consumer
    qos-port: 33333
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181

这里我们需要制定一个qos-port端口,避免端口冲突。其他的配置和provider类似

测试

  1. 先启动provider
  2. 再启动consumer

查看控制台:

看看zookeeper的元数据目录

总结

我们用基于Spring BootZookeeper注册中心快速入门使用了dubbo3。可以看到dubbo3并不需要像以前那样配置xml

比如这样

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder/>

    <!-- 定义应用名 -->
    <dubbo:application name="demo-provider"/>

    <!-- 定义注册中心地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 定义订阅信息,Dubbo 会在 Spring Context 中创建对应的 bean -->
    <dubbo:reference id="greetingsService" interface="org.apache.dubbo.samples.api.GreetingsService"/>

</beans>

相对xml来说注解的使用更为简单。从spirngspring boot就可以说明xml文件令人讨厌。

所以我们使用高版本的duboo还是能尽量用注解就用注解吧,毕竟注解还是方便很多的.

想了解更多duubo3相关的内容可以参考dubbo官方文档,非常完善的中文文档

源码

参考

相关推荐
master-dragon5 小时前
wireshark抓取dubbo调用实践
wireshark·dubbo
web151173602237 小时前
Spring Boot项目中解决跨域问题(四种方式)
spring boot·后端·dubbo
IT闫4 天前
【Dubbo+Zookeeper】——SpringBoot+Dubbo+Zookeeper知识整合
分布式·zookeeper·云原生·dubbo
T0uken4 天前
【Docker】百度网盘:基于VNC的Web访问及后台下载
前端·docker·dubbo
阿猿收手吧!10 天前
【Docker】容器被停止/删除的方式及命令:全面解析与实践指南
运维·docker·容器·面试·eureka·dubbo
三天不学习10 天前
Uniapp 获取定位详解:从申请Key到实现定位功能
uni-app·dubbo
m0_7482466111 天前
Java进阶:Dubbo
java·开发语言·dubbo
阿里云云原生12 天前
使用 Apifox、Postman 测试 Dubbo 服务,Apache Dubbo OpenAPI 即将发布
apache·dubbo·postman
俏布斯14 天前
服务发现(Dubbo-zookeeper)
zookeeper·服务发现·dubbo
CopyLower14 天前
ZooKeeper 和 Dubbo 的关系:技术体系与实际应用
分布式·zookeeper·dubbo