欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dubbo-hello world

程序员文章站 2022-05-12 16:18:50
Dubbo-hello world一、Provider​定义服务接口和实现类public interface ProviderService { String sayHello(String word);}public class ProviderServiceImpl implements ProviderService { @Override public String sayHello(String word) { System.out.p...

Dubbo-hello world

无论是服务端Provider还是消费端Consumer都是基于maven-quickstart项目构建!

一、Provider

  1. ​ 定义服务接口和实现类

    
    public interface ProviderService {
         String sayHello(String word);
    }
    
    public class ProviderServiceImpl implements ProviderService {
        @Override
        public String sayHello(String word) {
            System.out.println("请求过来了...");
    
            return word + "【server】";
        }
    }
    
  2. 定义dubbo服务端配置文件provider.xml(resources/META-INF/spring/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="provider" owner="eran">
            <dubbo:parameter key="qos.enable" value="true" />
            <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
            <dubbo:parameter key="qos.port" value="55555" />
        </dubbo:application>
    
        <dubbo:monitor protocol="registry" />
    
        <!--<dubbo:registry address="N/A" />   &lt;!&ndash; N/A表示dubbo自动分配地址,直连的方式 &ndash;&gt;-->
        <dubbo:registry address="zookeeper://zk-registry1.com.cn:2181" check="false" />
    
        <dubbo:protocol name="dubbo" port="20880" />
    
        <dubbo:service interface="com.common.dubbo.service.ProviderService" ref="providerSrv" />
    
        <bean id="providerSrv" class="com.common.dubbo.service.impl.ProviderServiceImpl"/>
    </beans>
    
  3. 测试类

    public class Start {
        public static void main(String[] args) throws IOException {
            System.out.println("Dubbo starting ...");
    
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/provider.xml");
            context.start();
            System.in.read();  //按任意键退出
        }
    }
    
  4. pom依赖

           <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.6</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.10</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
             <version>4.1.42.Final</version>
         </dependency>
    
    
    

二、Consumer

  1. 接口定义

    package com.common.dubbo.service;   //注意这个报名要和服务端一致
    
    public interface ProviderService {
         String sayHello(String word);
    }
    
    //其实也不用再定义了,可以依赖服务端包,但一旦依赖了服务端,就相当于引入了实现类,也不是很好,这里就自己定义了接口
    
  2. consumer.xml配置文件(resources/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="consumer" owner="eran"/>
    
        <!--<dubbo:registry address="N/A" />-->
        <dubbo:registry address="zookeeper://registry1.com.cn:2181" />
    
        <dubbo:reference check="false" id="providerSrv" interface="com.common.dubbo.service.ProviderService" />
    </beans>
    
  3. 测试类

    public class Start {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
            context.start();
            ProviderService providerService = (ProviderService)context.getBean("providerSrv");
            String hello = providerService.sayHello("hello");
            System.out.println(hello);
            System.in.read();
        }
    }
    
  4. pom依赖

      <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.6</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.10</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.5</version>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.32.Final</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.8.0</version>
            </dependency>
    
  5. 执行结果
    Dubbo-hello world
    Dubbo-hello world

  6. 引发思考
    1.dubbo是如何通信的,基于什么协议?
    2.dubbo配置文件中的具体配置什么意思?

本文地址:https://blog.csdn.net/u011910653/article/details/110230910