码迷,mamicode.com
首页 > 编程语言 > 详细

dubbo+zookeeper下生产者和消费者配置(基于springboot开发)

时间:2018-08-17 11:22:46      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:hub   pack   end   author   aik   接口   imp   home   www.   

一.总共分为三个目录:

  • dubbo-api      服务的接口用于对接客户端和服务端 
  • dubbo-client     客户端配置文件为:consumer.xml
  • dubbo-service  服务端配置文件为:provide.xml  

二.配置文件

  1.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" />
    <!-- zookeeper作为注册中心 -->
    <dubbo:registry protocol="zookeeper"
        address="zookeeper://10.92.1.17:2181" />
    <dubbo:protocol name="dubbo" port="20895" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference
        interface="com.dubbo.api.rpc.TestService" id="testService"></dubbo:reference>
</beans>

 

  2.provide.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="provider" />
    <!-- 使用zookeeper作为注册中心 -->
    <dubbo:registry protocol="zookeeper"
        address="zookeeper://10.92.1.17:2181" />
    <dubbo:protocol name="dubbo" port="20895" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:service interface="com.dubbo.api.rpc.TestService"
        ref="testService"></dubbo:service>
</beans>

  3pom文件:   

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.dubbo.api</groupId>
            <artifactId>dubbo-api</artifactId>
             <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

  4. 服务的接口

/**  
* @Title: TestService.java  
* @Description:   
* @author lsc  
* @date 2018年8月17日  
* @version 1.0  
* @Copyright:Copyright Taikanglife.All Rights Reserved  
*/  
package com.dubbo.api.rpc;

/**   
 * @ClassName:  TestService   
 * @Description:
 * @author: lsc
 * @date: 2018年8月17日 上午10:03:28     
 * @Copyright:Copyright Taikanglife.All Rights Reserved
 */
public interface TestService {
    
    public String sayHello();
}

  5.服务的实现

package com.dubbo.dubbo;

import org.springframework.stereotype.Service;

import com.dubbo.api.rpc.TestService;

/**
 * @ClassName:  TestServiceImpl   
 * @Description:
 * @author: lsc
 * @date: 2018年8月17日 上午10:24:11     
 * @Copyright:Copyright Taikanglife.All Rights Reserved
 */
@Service("testService")
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello() {
        return "hello dubbo";
    }
}

  6.服务的调用

package com.dubbo.dubboclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dubbo.api.rpc.TestService;

/**
 * @ClassName:  MyController   
 * @Description:
 * @author: lsc
 * @date: 2018年8月17日 上午10:24:56     
 * @Copyright:Copyright Taikanglife.All Rights Reserved
 */
@RestController
public class MyController {
    
    @Autowired
    private TestService testService;
 
    @RequestMapping("/home")
    public String home(){
        return testService.sayHello();
    }
}

  

dubbo+zookeeper下生产者和消费者配置(基于springboot开发)

标签:hub   pack   end   author   aik   接口   imp   home   www.   

原文地址:https://www.cnblogs.com/liushuchen/p/9491929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!