首页 技术 正文
技术 2022年11月19日
0 收藏 961 点赞 2,629 浏览 3531 个字

Dubbo是一款高性能、轻量级的开源Java RPC框架。

很多朋友想学习测试dubbo接口,但是苦于没有可练习的dubbo接口而放弃,下面我就带大家开发一个简单的dubbo接口。

接口需求

客户端输入uncleyong(当然,也可以输入其它字符串),服务端返回hello uncleyong

开发环境

jdk + idea + maven + zookeeper

jdk安装:https://www.cnblogs.com/uncleyong/p/10732951.html

idea安装:https://www.cnblogs.com/uncleyong/p/10971923.html

maven安装:https://www.cnblogs.com/uncleyong/p/10743181.html

zookeeper安装:https://www.cnblogs.com/uncleyong/p/10737119.html

common开发

idea中创建模块dubbo-common

存放公共的实体类、接口

package com.uncleyong.dubbotest.service;public interface SayHelloToClient {
public String sayHello(String name);
}

然后mvn install打包,供provider及consumer在pom文件中引包 

dubbo接口demo开发

provider开发

idea中创建模块dubbo_provider

创建实现类

package com.uncleyong.dubbotest.service.impl;import com.uncleyong.dubbotest.service.SayHelloToClient;public class SayHelloToClientImpl implements SayHelloToClient {    public String sayHello(String name){
System.out.println("from client :" + name);
return "hello, " + name;
}
}

配置文件,provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
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"> <dubbo:application name="dubbo-provider"/> <!-- 使用zookeeper广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20888"/> <!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.uncleyong.dubbotest.service.SayHelloToClient" ref="sayhellotoclient"/> <!-- 和本地bean一样实现服务 -->
<bean id="sayhellotoclient" class="com.uncleyong.dubbotest.service.impl.SayHelloToClientImpl"/></beans>

创建主运行文件,ProviderMain

package com.uncleyong.dubbotest.main;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"provider.xml"});
context.start();
System.out.println("注册成功,如想退出,按任意键退出");
System.in.read(); // 按任意键退出
}
}

consumer开发

idea中创建模块dubbo_consumer

主运行文件

package com.uncleyong.dubbotest.main;import com.uncleyong.dubbotest.service.SayHelloToClient;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Scanner;public class ConsumerMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"consumer.xml"});
context.start();
// 获取远程服务代理
SayHelloToClient say = (SayHelloToClient) context.getBean("sayhellotoclient");
// 执行远程方法
String res = say.sayHello("UncleYong");
// 显示调用结果
System.out.println(res);
new Scanner(System.in).next();
}
}

配置文件,consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
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"> <dubbo:application name="dubbo-consumer"/> <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="sayhellotoclient" interface="com.uncleyong.dubbotest.service.SayHelloToClient"/></beans>

运行结果

先启动zookeeper,进入zookeeper的bin目录,点击【zkServer.cmd】

dubbo接口demo开发

启动provider

dubbo接口demo开发

服务注册成功

dubbo接口demo开发

dubbo接口demo开发

启动consumer,可以看到输出了结果

dubbo接口demo开发

dubbo接口demo开发

在provider端,也可以看到客户端发过来的消息

dubbo接口demo开发

至此,开发完成。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857