dubbo + zookeeper 实例

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;

@Service
public 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 。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值