首页 > 其他分享 >Dubbo使用实例

Dubbo使用实例

时间:2023-05-30 17:24:14浏览次数:35  
标签:Dubbo String 示例 使用 接口 实例 服务提供者 public

下面是一个使用Java实现的Dubbo示例代码,包括服务提供者和服务消费者:

首先,您需要确保已安装ZooKeeper,并且ZooKeeper服务器正在运行。

接下来,您可以使用以下示例代码创建Dubbo的服务提供者和服务消费者:

服务提供者示例代码

接口定义:

public interface GreetingService {
    String sayHello(String name);
}

实现类:

public class GreetingServiceImpl implements GreetingService {
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

服务提供者启动类:

import org.apache.dubbo.config.*;

public class Provider {
    public static void main(String[] args) throws Exception {
        ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(GreetingService.class);
        serviceConfig.setRef(new GreetingServiceImpl());
        serviceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        serviceConfig.export();

        System.out.println("Service provider started.");

        // 阻塞当前线程,使服务一直保持运行状态
        System.in.read();
    }
}

服务消费者示例代码

消费者启动类:

import org.apache.dubbo.config.*;
import org.apache.dubbo.config.utils.ReferenceConfigCache;

public class Consumer {
    public static void main(String[] args) {
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        GreetingService greetingService = ReferenceConfigCache.getCache().get(referenceConfig);

        String result = greetingService.sayHello("John");

        System.out.println(result);
    }
}

在上述示例代码中,我们定义了一个GreetingService接口,其中包含一个sayHello方法用于打招呼。

服务提供者实现了GreetingService接口,并提供了一个GreetingServiceImpl类来实现接口方法。

服务提供者启动类通过Dubbo的ServiceConfig来配置服务接口、实现类和ZooKeeper注册中心。然后,调用export方法将服务发布到Dubbo上下文中,并开始监听客户端的请求。

服务消费者通过Dubbo的ReferenceConfig来配置服务接口和ZooKeeper注册中心。然后,使用ReferenceConfigCache来获取服务接口的实例。最后,调用服务接口的方法来消费服务提供者的功能。

请确保您已正确配置Dubbo和相关依赖项,并根据需要更改ZooKeeper服务器地址和接口实现逻辑。运行Provider和Consumer代码后,您将看到消费者成功调用服务提供者的方法,并获得打招呼的结果。



当然!这是另一个使用Java实现的Dubbo示例代码,用于演示Dubbo服务的远程调用和参数传递:

首先,确保您已安装并启动了ZooKeeper服务器。

接下来,您可以使用以下示例代码来创建Dubbo的服务提供者和服务消费者:

服务提供者示例代码

接口定义:

public interface CalculatorService {
    int add(int a, int b);
}

实现类:

public class CalculatorServiceImpl implements CalculatorService {
    public int add(int a, int b) {
        return a + b;
    }
}

服务提供者启动类:

import org.apache.dubbo.config.*;

public class Provider {
    public static void main(String[] args) throws Exception {
        ServiceConfig<CalculatorService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(CalculatorService.class);
        serviceConfig.setRef(new CalculatorServiceImpl());
        serviceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        serviceConfig.export();

        System.out.println("Service provider started.");

        // 阻塞当前线程,使服务一直保持运行状态
        System.in.read();
    }
}

服务消费者示例代码

消费者启动类:

import org.apache.dubbo.config.*;
import org.apache.dubbo.config.utils.ReferenceConfigCache;

public class Consumer {
    public static void main(String[] args) {
        ReferenceConfig<CalculatorService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(CalculatorService.class);
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        CalculatorService calculatorService = ReferenceConfigCache.getCache().get(referenceConfig);

        int result = calculatorService.add(5, 3);

        System.out.println("Result: " + result);
    }
}

在上述示例代码中,我们定义了一个CalculatorService接口,其中包含一个add方法用于相加两个整数。

服务提供者实现了CalculatorService接口,并提供了一个CalculatorServiceImpl类来实现接口方法。

服务提供者启动类通过Dubbo的ServiceConfig来配置服务接口、实现类和ZooKeeper注册中心。然后,调用export方法将服务发布到Dubbo上下文中,并开始监听客户端的请求。

服务消费者通过Dubbo的ReferenceConfig来配置服务接口和ZooKeeper注册中心。然后,使用ReferenceConfigCache来获取服务接口的实例。最后,调用服务接口的方法来远程调用服务提供者的功能,并获取返回结果。

请确保您已正确配置Dubbo和相关依赖项,并根据需要更改ZooKeeper服务器地址和接口实现逻辑。运行Provider和Consumer代码后,您将看到消费者成功远程调用服务提供者的方法,并获得相加的结果。



当然!这是另一个使用Java实现的Dubbo示例代码,用于演示Dubbo的配置和注解使用:

首先,确保您已安装并启动了ZooKeeper服务器。

接下来,您可以使用以下示例代码来创建Dubbo的服务提供者和服务消费者:

服务提供者示例代码

import org.apache.dubbo.config.*;

public class Provider {
    public static void main(String[] args) throws Exception {
        ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(GreetingService.class);
        serviceConfig.setRef(new GreetingServiceImpl());
        serviceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        serviceConfig.export();

        System.out.println("Service provider started.");

        // 阻塞当前线程,使服务一直保持运行状态
        System.in.read();
    }
}

服务消费者示例代码

import org.apache.dubbo.config.*;

public class Consumer {
    public static void main(String[] args) {
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://localhost:2181"));

        GreetingService greetingService = referenceConfig.get();

        String result = greetingService.sayHello("John");

        System.out.println(result);
    }
}

服务接口和实现类示例代码

public interface GreetingService {
    String sayHello(String name);
}

public class GreetingServiceImpl implements GreetingService {
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

在上述示例代码中,我们定义了一个GreetingService接口,其中包含一个sayHello方法用于打招呼。

服务提供者实现了GreetingService接口,并提供了一个GreetingServiceImpl类来实现接口方法。

服务提供者启动类使用Dubbo的ServiceConfig来配置服务接口、实现类和ZooKeeper注册中心。然后,调用export方法将服务发布到Dubbo上下文中,并开始监听客户端的请求。

服务消费者使用Dubbo的ReferenceConfig来配置服务接口和ZooKeeper注册中心。然后,通过调用get方法获取服务接口的实例。最后,调用服务接口的方法来消费服务提供者的功能。

请确保您已正确配置Dubbo和相关依赖项,并根据需要更改ZooKeeper服务器地址和接口实现逻辑。运行Provider和Consumer代码后,您将看到消费者成功调用服务提供者的方法,并获得打招呼的结果。

标签:Dubbo,String,示例,使用,接口,实例,服务提供者,public
From: https://www.cnblogs.com/lukairui/p/17443795.html

相关文章

  • .net调用动态库NationECCode.dll使用电子凭证二维码解码接口
    .net调用动态库NationECCode.dll使用电子凭证二维码解码接口 C#.net调用示例代码:[DllImport("NationECCode.dll",CallingConvention=CallingConvention.StdCall)]publicstaticexternvoidNationEcTrans(stringurl,stringinput,IntPtroutput);......
  • 如何使用ComboBox以获取用户的单选并可编辑
    如何使用ComboBox以获取用户的单选并可编辑ComboBox是一种单选下拉框,和ChoiceBox很类似。ComboBox不仅可以接收用户的一个选项,还可以提供可编辑框,让用户输入内容。效果展示示例代码importjavafx.application.Application;importjavafx.geometry.Insets;importjava......
  • python中如何使用正则表达式查询字符串
    '''Createdon2019年12月2日@author:hp''''''上一篇文章介绍了那么多关于正则表达式的用法,现在终于到了python中如何使用正则表达式了,不急,请诸君慢慢来''''''之前在讲字符串时,已经说过了字符串的格式化输出,大家没看的可以看我的上一篇文章格式化输出时,是含有模式串......
  • #yyds干货盘点#Linux编辑器vim使用
    vim:是一款编辑器,只负责写代码;相当于windows的记事本;gcc:是一款编译器,只负责程序的翻译;gdb:是一款调试器,只负责程序的调试;vim是一个功能强大的多模式编辑器,是Linux/UNIX上最常用的文本编辑器,它的作用是建立、编辑、显示文本文件。Vim没有菜单,只有命令。命令模式(默认):输入的任何......
  • vues全局使用WebSocket
    //import{showInfoMsg,showErrorMsg}from'@/utils/popInfo'//importElementUIfrom'element-ui';import{Toast}from'vant';functioninitWebSocket(baseObj){console.log(baseObj)//constwsUri=WS_API+&quo......
  • SwiftUI 使用contentShape()控制点击区域
    当我们向一个view添加TapGesture时,就会发现“有内容”的区域是可以点击的。“有内容”指的是有图片、文字、背景颜色的区域。而空白区域,是不能触发点击回调的。比如:structContentView:View{varbody:someView{VStack(spacing:124){Image(sys......
  • 在树莓派上使用numpy实现简单的神经网络推理,pytorch在服务器或PC上训练好模型保存成nu
    这几天又在玩树莓派,先是搞了个物联网,又在尝试在树莓派上搞一些简单的神经网络,这次搞得是mlp识别mnist手写数字识别训练代码在电脑上,cpu就能训练,很快的:1importtorch2importtorch.nnasnn3importtorch.optimasoptim4fromtorchvisionimportdatasets,transfor......
  • 关于数据库-SQL-between-运算符语句的使用及说明
    关于数据库SQL语句between运算符说明如下1、多用于选取介于两个值之间的数据范围内的值2、运算符选择给定范围内的值。值可以是数字,文本或日期3、是包含性的:包括开始和结束值,且开始值需小于结束值(否则返回空,即0条记录)关于SQL语句between的使用格式如下:selectcolumn_nam......
  • MegEngine 使用小技巧:如何解读 MegCC 编译模型几个阶段 Pass 的作用
    MegCC 是一个真真实实的深度学习模型编译器,具备极其轻量的Runtime二进制体积,高性能,方便移植,极低内存使用以及快启动等核心特点。用户可在MLIR上进行计算图优化,内存规划,最后通过预先写好的code模版进行代码生成。MegCC中主要的PassMGBToKernelPass:这个Pass主要将MGB......
  • django-simpleui使用
    django-simpleui使用安装pipinstalldjango-simpleui豆瓣:http://pypi.douban.com/simple/中科大:https://pypi.mirrors.ustc.edu.cn/simple/清华:https://pypi.tuna.tsinghua.edu.cn/simple/settings配置INSTALLED_APPS=['simpleui',]升级simpleuipipinstal......