Dubbox和Zookeeper入门

Dubbox和Zookeeper

一。准备dubbox和zookeeper文件

1.dubbox下载路径

https://github.com/dangdangdotcom/dubbox

2.zookeeper下载“单机”路径

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

二。执行命令

1.大开

输入:

执行命令:  mvn clean install -Dmaven.test.skip=true

  在命令提示符中执行下列的的命令,将刚才编译出来的jar文件安装到Maven仓库中,命令中红色的部分更改为自己Dubbox所在的目录

mvn install:install-file -Dfile=D:Softdubbox-masterdubbo	argetdubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar -DgeneratePom=true

在开启Zookeeper服务

在idea的pom.xml输入

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <!-- 添加zk客户端依赖 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>

        <!-- 如果要使用json序列化 -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.7.Final</version>
        </dependency>

        <!-- 如果要使用xml序列化 -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.7.Final</version>
        </dependency>

        <!-- 如果要使用netty server -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-netty</artifactId>
            <version>3.0.7.Final</version>
        </dependency>

        <!-- 如果要使用Sun HTTP server -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jdk-http</artifactId>
            <version>3.0.7.Final</version>
        </dependency>

        <!-- 如果要使用tomcat server -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>com.esotericsoftware.kryo</groupId>
            <artifactId>kryo</artifactId>
            <version>2.24.0</version>
        </dependency>
        <dependency>
            <groupId>de.javakaffee</groupId>
            <artifactId>kryo-serializers</artifactId>
            <version>0.26</version>
        </dependency>
        <dependency>
            <groupId>de.ruedigermoeller</groupId>
            <artifactId>fst</artifactId>
            <version>1.55</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>7.0.0.pre5</version>
        </dependency>

生产者和消费者都需要使用的接口

package com.yjc.service;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

@Path("/user")
public interface UserService {
    @GET
    @Path("/getUser/{id}")
    @Consumes({ MediaType.APPLICATION_JSON })
    public String getUser(@PathParam("id") Integer id);
}

生产者中的接口实现类

package com.yjc.service.impl;

import com.yjc.service.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public String getUser(Integer id) {
        System.out.println("dubbox发布----UserService类中的getUser方法,参数----"+id);
        return "dubbox";
    }
}

生产者中的配置文件applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="app-provider"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
<!--    <dubbo:registry address="192.168.118.3:2181,192.168.118.4:2181,192.168.118.5:2181"   protocol="zookeeper" />-->
    <dubbo:registry address="127.0.0.1:2181"   protocol="zookeeper" />

    <dubbo:protocol name="rest" port="8081" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.yjc.service.UserService" ref="iUserService" />
    <bean id="iUserService" class="com.yjc.service.impl.UserServiceImpl"/>
</beans>

生产者测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class AppTest 
{
    public static void main(String[] args) throws IOException {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("duboox 注册成功!");
        System.in.read();
    }
}

消费者中的配置文件applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="app-provider"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="127.0.0.1:2181"   protocol="zookeeper" />



    <!-- 声明需要暴露的服务接口 -->
    <dubbo:reference interface="com.yjc.service.UserService" id="iUserService" />
</beans>

消费者中的测试类

package com.yjc;

import static org.junit.Assert.assertTrue;

import com.yjc.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    public static void main(String[] args) throws IOException {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService  = (UserService)ctx.getBean("iUserService");
        String user = userService.getUser(100);
        System.out.println("消费者消费的参数-----"+user);
        System.in.read();
    }
}
原文地址:https://www.cnblogs.com/rzbwyj/p/12012667.html