搜索
您的当前位置:首页正文

DUBBO的简单搭建

来源:易榕旅网

首先添加zk和dubbo的依赖到 pom文件中,生产者消费者都要添加

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>

**

生产者:

//该接口为暴露的服务接口
public interface DubboService01 {
    String sayHello(String name);
}
//服务的具体实现
public class DubboService01Impl implements DubboService01 {
    @Override
    public String sayHello(String name) {
        return name + "调用dubbo成功!";
    }
}

生产者配置文件如下:

<?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">

    <bean id="DubboService01" class="com.example.demo.service.impl.DubboService01Impl"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:application name="hello-provider"/>

    <dubbo:service retries="0" interface="com.example.demo.service.DubboService01" ref="DubboService01"/>
</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

//生产者注册消息
public class Test {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubboprovider.xml");
        context.start();
        System.in.read(); //保持服务一直开启
    }
}

**

消费者

**

//生产者所暴露的服务接口体现
public interface DubboService01 {
    String sayHello(String name);
}

消费者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="hello-consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183"/>
    <!-- 此处的路径要和生产者路径完全一致 id和类名完全一致-->
    <dubbo:reference id="DubboService01" check="false" interface="com.example.demo.service.DubboService01"/>

</beans>
import com.example.demo.service.DubboService01;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//消费者订阅消息
public class TestDubbo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubboconsumer.xml");

        context.start();

        DubboService01 dubboService01 = (DubboService01) context.getBean("DubboService01");

        String sayHello = dubboService01.sayHello("周瑜");

        System.out.println(sayHello);
    }
}

测试结果 先启动生产者服务 在启动消费者即可 出现如下则调用成功(此处没有讲解如何配置zk 比较简单 百度一下吧 )

因篇幅问题不能全部显示,请点此查看更多更全内容

Top