• Dubbo 版 Helloworld


    使用工具:MAVEN、IDEA、Spring、Dubbo、Zookeeper

    直接上代码

    项目结构:
    structur

    步骤如下:

    搭建MAVEN项目,添加相关依赖

    pom.xml

    <!--Zookeeper-->
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
      <version>4.0.0</version>
    </dependency>
    
    <!--dubbo-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.1</version>
    </dependency>
    

    dubbo-demo-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://code.alibabatech.com/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <dubbo:application name="demo-consumer"/>
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
    </beans>
    

    dubbo-demo-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://code.alibabatech.com/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:application name="demo-provider"/>
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <dubbo:protocol name="dubbo" port="20880"/>
        <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
        <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
    </beans>
    

    Provider.java

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    public class Provider {
    
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-provider.xml");
            context.start();
            System.in.read();
        }
    }
    

    Consumer.java

    import com.alibaba.dubbo.demo.DemoService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Consumer {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-consumer.xml");
            context.start();
            // obtain proxy object for remote invocation
            DemoService demoService = (DemoService) context.getBean("demoService");
            // execute remote invocation
            String hello = demoService.sayHello("world");
            // show the result
            System.out.println(hello);
        }
    }
    

    DemoService.java

    package com.alibaba.dubbo.demo;
    
    public interface DemoService {
    
        String sayHello(String name);
    
    }
    
    

    DemoServiceImpl.java

    package com.alibaba.dubbo.demo.provider;
    
    import com.alibaba.dubbo.demo.DemoService;
    
    public class DemoServiceImpl implements DemoService{
    
        public String sayHello(String name) {
            return "Hello " + name;
        }
    
    }
    

    运行方式:打开本地Zookeeper服务,先运行Provider,再运行Consumer

    运行结果:控制台输出Hello world,Zookeeper 服务器会输出几行信息:
    image
    打开一个Zookeeper客户端检查是否有节点存在
    image

  • 相关阅读:
    python之面向对象之类变量和实例变量
    python之面向对象之封装
    python之shutil模块
    利用python实现冒泡排序
    利用python实现二分法和斐波那契序列
    thinkphp input
    从右向左
    全局修改composer源地址
    Git忽略规则及.gitignore规则不生效的解决办法
    mysql主从数据库不同步的2种解决方法 (转载)
  • 原文地址:https://www.cnblogs.com/yuanmiemie/p/8904142.html
Copyright © 2020-2023  润新知