Dubbo入门学习--Dubbo简单示例

ゝ一纸荒年。 2022-06-16 02:59 328阅读 0赞

1、Zookeeper安装

从Zookeeper官网下载,进入到bin目录下,在cms中执行zkServer就可以运行Zookeeper了。

![Image 1][]

2、API接口

创建一个接口jar,此接口在服务提供者和服务消费者中使用

  1. public interface IDemoServer {
  2. String sayHello(String str);
  3. }

3、服务提供者

applicationContext.xml中添加如下配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!--应用名称-->
  11. <dubbo:application name="hello-world-app" />
  12. <!--配置Zookeeper连接地址-->
  13. <dubbo:registry protocol="zookeeper" address="localhost:2181" />
  14. <!--服务协议-->
  15. <dubbo:protocol name="dubbo"/>
  16. <!--接口-->
  17. <dubbo:service interface="com.tianjunwei.server.dubbo.service.IDemoServer"
  18. ref="demoService" /> <!-- 和本地bean一样实现服务 -->
  19. <!--实现类-->
  20. <bean id="demoService" class="com.tianjunwei.server.dubbo.service.DemoServerImpl" />
  21. </beans>

接口IDemoServer的实现类

  1. public class DemoServerImpl implements IDemoServer {
  2. public String sayHello(String str) {
  3. return str;
  4. }
  5. }

Main函数启动提供服务:

  1. public class Main {
  2. public static void main(String[] args) {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
  4. context.start();
  5. while(true){
  6. }
  7. }
  8. }

3、服务消费者

applicationContext.xml中如下配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名-->
  11. <dubbo:application name="consumer-of-helloworld-app" />
  12. <!-- Zookeeper连接信息 -->
  13. <dubbo:registry protocol="zookeeper" address="localhost:2181" />
  14. <!-- 服务接口 -->
  15. <dubbo:reference id="demoService" interface="com.tianjunwei.server.dubbo.service.IDemoServer"/>
  16. </beans>

服务调用:

  1. public class ChatAction {
  2. @SuppressWarnings("resource")
  3. public static void main(String[] args) throws InterruptedException{
  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  5. context.start();
  6. IDemoServer demoServer = (IDemoServer) context.getBean("demoService");
  7. System.out.println("client:"+demoServer.sayBye("dubbo"));
  8. }
  9. }

运行结果:

client:dubbo

可以去官网https://github.com/alibaba/dubbo中参考示例程序。

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 dubbo学习入门了解)

    概述 Apache Dubbo 是一款高性能、轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发

    相关 Dubbo简单入门【1】

    一、Dubbo是什么 以下笔记学习自:[Dubbo 官方文档][Dubbo] 1. 本质:一个Jar包,一个可以进行远程服务调用的分布式框架 分布式: 不