spring整合dubbo-2.5.3(使用TCP广播或者zookeeper 暴露和发现服务)

原文:https://blog.csdn.net/liang_love_java/article/details/50763869 

dubbo需要的jar

dubbo-2.5.3.jar 
javassist-3.15.0-GA.jar 
netty-3.2.5.Final.jar 
zkclient-0.1.jar 
zookeeper-3.5.1-alpha.jardubbo消费提供者
接口

package com.lp.dubbo.demo;

import java.io.Serializable;

public interface ProviderDemoService {

    public String sayHello(String name);

    public User getUser();

    static class User implements Serializable{
        private static final long serialVersionUID = 1L;
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "name=" + name + ",age=" + age;
        }
    }

}

接口实现类
package com.lp.dubbo.demo;

public class ProviderDemoServiceImpl implements ProviderDemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }

    @Override
    public User getUser() {
        User user = new User();
        user.setName("lp");
        user.setAge(26);
        return user;
    }

}

配置文件

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

    <!-- 具体的实现bean -->
    <bean id="demoService" class="com.lp.dubbo.demo.ProviderDemoServiceImpl" />
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="xixi_provider" />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry  address="multicast://224.1.1.1:12345" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20881" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.lp.dubbo.demo.ProviderDemoService" ref="demoService" />

</beans>

测试代码

package com.lp.dubbo.demo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
        //保证主线不会死掉
        System.in.read(); // 按任意键退出

    }

}

dubbo 服务消费者
接口(与服务提供者的接口是一模一样的)
package com.lp.dubbo.demo;

import java.io.Serializable;

public interface ProviderDemoService {

    public String sayHello(String name);

    public User getUser();

    static class User implements Serializable{
        private static final long serialVersionUID = 1L;
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "name=" + name + ",age=" + age;
        }
    }

}

配置文件 dubbo-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-of-helloworld-app"  />

    <!-- 使用multicast广播注册中心发现服务地址 -->
     <dubbo:registry address="multicast://224.1.1.1:12345" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.lp.dubbo.demo.ProviderDemoService" />

</beans>

测试代码

package com.lp.dubbo.demo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lp.dubbo.demo.ProviderDemoService.User;

public class ConsumerTest {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        ProviderDemoService demoService = (ProviderDemoService)context.getBean("demoService"); // 获取远程服务代理
        User user = demoService.getUser();
        System.out.println(user);
    }

}


进行测试
先运行 ProviderTest, 再 运行 ConsumerTest。 
输出结果 
name=lp,age=26

上面的xml配置是使用 TCP广播进行 服务暴露和服务发现的,其实可以使用zookeeper实现暴露和服务发现,但是需要安装zookeeper,zookeeper安装教程。 
如果你的zookeeper是集群,将

<dubbo:registry  address="multicast://224.1.1.1:12345" />
替换为
<dubbo:registry protocol="zookeeper" address="192.168.17.129:2181,192.168.17.129:2182,192.168.17.129:2183" /> 或者
<dubbo:registry address="zookeeper://192.168.17.129:2181?backup=192.168.17.129:2182,192.168.17.129:2183" />
服务提供者和服务消费者做相同的修改

如果你的zookeeper是单机的,将

<dubbo:registry  address="multicast://224.1.1.1:12345" />
替换为
<dubbo:registry  address="zookeeper://224.1.1.1:12345" />
服务提供者和服务消费者做相同的修改
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dubbo 2.5.3 全部jar包下载 [INFO] dubbo-parent ...................................... SUCCESS [1.042s] [INFO] Hessian Lite(Alibaba embed version) ............... SUCCESS [4.438s] [INFO] dubbo-common ...................................... SUCCESS [9.153s] [INFO] dubbo-container ................................... SUCCESS [0.019s] [INFO] dubbo-container-api ............................... SUCCESS [1.557s] [INFO] dubbo-container-spring ............................ SUCCESS [1.378s] [INFO] dubbo-container-jetty ............................. SUCCESS [1.448s] [INFO] dubbo-container-log4j ............................. SUCCESS [1.566s] [INFO] dubbo-container-logback ........................... SUCCESS [1.775s] [INFO] dubbo-remoting .................................... SUCCESS [0.151s] [INFO] dubbo-remoting-api ................................ SUCCESS [6.705s] [INFO] dubbo-remoting-netty .............................. SUCCESS [5.750s] [INFO] dubbo-remoting-mina ............................... SUCCESS [2.109s] [INFO] dubbo-remoting-grizzly ............................ SUCCESS [0.989s] [INFO] dubbo-remoting-p2p ................................ SUCCESS [2.322s] [INFO] dubbo-remoting-http ............................... SUCCESS [3.506s] [INFO] dubbo-remoting-zookeeper .......................... SUCCESS [1.279s] [INFO] dubbo-rpc ......................................... SUCCESS [0.015s] [INFO] dubbo-rpc-api ..................................... SUCCESS [3.413s] [INFO] dubbo-rpc-default ................................. SUCCESS [4.105s] [INFO] dubbo-rpc-injvm ................................... SUCCESS [3.112s] [INFO] dubbo-rpc-rmi ..................................... SUCCESS [1.349s] [INFO] dubbo-rpc-hessian ................................. SUCCESS [1.721s] [INFO] dubbo-rpc-http .................................... SUCCESS [1.199s] [INFO] dubbo-rpc-webservice .............................. SUCCESS [0.997s] [INFO] dubbo-cluster ..................................... SUCCESS [6.085s] [IN
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值