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

猜你喜欢

转载自blog.csdn.net/qq_22596931/article/details/85095004