dubbo + zookeeper 实例

来源:互联网 发布:天堂伞淘宝 编辑:程序博客网 时间:2024/05/18 13:28

1、服务提供端

dubbox.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 owner="foresee" name="ops" />    <!--zookeeper注册中心 -->    <dubbo:registry id="local" address="zookeeper://127.0.0.1:2181"/>    <!--使用multicast广播注册中心暴露服务地址 -->    <!--<dubbo:registry address="multicast://10.57.41.19:1234" /> -->    <!--<dubbo:protocol name ="dubbo1" port="20883" />-->    <dubbo:protocol name ="dubbo" port="20880" />    <!-- 配置监控的服务地址和IP    <dubbo:monitor address="127.0.0.1:7070"  />-->    <!-- 发布这个服务给其他外围业务调用-->    <dubbo:service protocol="dubbo" timeout="500000" retries="0" connections="100" interface ="com.anson.IHelloWorld" registry="local" ref="helloWorld" />    <!-- 获取这个服务 --></beans>


spring-mvc

<?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:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="http://www.springframework.org/schema/beans                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.1.xsd                          http://www.springframework.org/schema/mvc                          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 开启自动注释注入 主要保护拦截器、视图的默认注入 --><mvc:annotation-driven /><!-- 自动扫描该包,扫描包含 @Component, @Repository, @Service, and @Controller,使SpringMVC认为包下用了@controller注解的类是控制器 --><context:component-scan base-package="com.anson" /><import resource="dubbox.xml"/></beans>

服务具体代码:

package com.anson;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Service;@Service("helloWorld")public class HelloWorldImpl implements IHelloWorld {    public String sayHello(String name) {        return name + ",你是动物吗?";    }    public static void main(String[] args) throws InterruptedException {        String[] fileUrl = new String[]{"spring-mvc.xml"};        new ClassPathXmlApplicationContext(fileUrl);        while (true) {            Thread.sleep(10000);        }    }}


2、服务消费方

dubbox.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 owner="foresee" name="szdsywtest" />    <!--zookeeper注册中心 -->    <dubbo:registry id="local" address="zookeeper://127.0.0.1:2181"/>    <!--使用multicast广播注册中心暴露服务地址 -->    <!--<dubbo:registry address="multicast://10.57.41.19:1234" /> -->    <!--<dubbo:protocol name ="dubbo1" port="20883" />-->    <!-- 配置监控的服务地址和IP    <dubbo:monitor address="127.0.0.1:7070"  />-->    <!-- 发布这个服务给其他外围业务调用-->    <!-- 获取这个服务 -->    <dubbo:reference id="helloWorld" registry="local" protocol="dubbo" interface="com.anson.IHelloWorld" check="false" /></beans>


消费方具体代码
package com.anson;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Servicepublic class DubboClient {    @Resource    private IHelloWorld helloWorld;    private static ClassPathXmlApplicationContext context;    static {        String[] fileUrl = new String[]{"dubbox.xml","spring-mvc.xml"};        context = new ClassPathXmlApplicationContext(fileUrl);    }    public void sayHello(String name){        if (helloWorld == null){            helloWorld = (IHelloWorld)context.getBean("helloWorld");        }        System.out.println(helloWorld.sayHello(name));    }    public static void main(String[] args) {        DubboClient test = new DubboClient();        test.sayHello("毛毛虫");    }}


接口代码

package com.anson;public interface IHelloWorld {    public String sayHello(String name);}


消费方依赖包

        <dependency>            <groupId>org.javassist</groupId>            <artifactId>javassist</artifactId>    <version>3.15.0-GA</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>    <version>2.8.4</version>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.6</version>        </dependency>     <dependency>        <groupId>com.github.sgroschupf</groupId>        <artifactId>zkclient</artifactId>        <version>0.1</version>    </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>    <version>4.3.4.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.3.4.RELEASE</version>        </dependency>             <dependency>                 <groupId>javax</groupId>                 <artifactId>javaee-api</artifactId><version>7.0</version>

提供方依赖包

        <dependency>            <groupId>org.javassist</groupId>            <artifactId>javassist</artifactId>    <version>3.15.0-GA</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>    <version>2.8.4</version>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.6</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>    <version>4.3.4.RELEASE</version>        </dependency>

说明:zookeeper端口设置为2181 。