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

dubbo官方文档,写的很详细

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.jar

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;
        }
    }

}

接口实现类
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" />
服务提供者和服务消费者做相同的修改

dubbo还有很多功能,自己也在学习中,更多的信息请查看dubbo官方文档。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值