Dubbo+Zookeeper

Posted 西北野狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo+Zookeeper相关的知识,希望对你有一定的参考价值。

搭建ZooKeeper服务注册中心

上传文件:

zookeeper-3.4.9.tar.gz

解压zookeeper-3.4.9.tar.gz

/opt/zookeeper-3.4.9/conf/zoo_sample.cfg复制为/opt/zookeeper-3.4.9/conf/zoo.cfg

新建目录:/opt/zookeeper-3.4.9/data

修改/opt/zookeeper-3.4.9/conf/zoo.cfg文件

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/opt/zookeeper-3.4.9/data

启动ZooKeeper:

[[email protected] bin]# pwd
/opt/zookeeper-3.4.9/bin
[[email protected] bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[[email protected] bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone

永久关闭防火墙:

chkconfig iptables off

举例子说明:

创建服务端工程:

引入依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.soyoungboy.distribute</groupId>
    <artifactId>DubboConsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>com.soyoungboy.distribute</groupId>
            <artifactId>DubboInterfaces</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.10.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

创建服务接口的实现类

import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello " + name;
    }

}

Spring配置文件:

<?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="demo-provider"/>
    
    <!-- 配置注册中心的位置 -->
    <!-- Multicast 注册中心:multicast://224.5.6.7:1234 -->
    <!-- 配置ZooKeeper注册中心位置方式如下 -->
    <dubbo:registry address="192.168.159.200:2181" protocol="zookeeper"/>
    
    <!-- 配置Dubbo客户端的访问端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 配置接口实现类对应的bean -->    
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- 配置提供的服务的接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

 log4j.properties

log4j.rootLogger=DEBUG,myConsole
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.ImmediateFlush=true
log4j.appender.myConsole.Target=System.out
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n

创建客户端工程:DubboConsumer

引入依赖:

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.8</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.5</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.10.RELEASE</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.soyoungboy.distributed</groupId>
    <artifactId>ServiceInterface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

配置文件:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 配置当前应用模块名称 -->
    <dubbo:application name="demo-consumer"/>
    
    <!-- 配置注册中心位置 -->
    <dubbo:registry address="192.168.159.246:2181" protocol="zookeeper"/>
    
    <!-- 配置提供服务的接口类型 -->
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
    
    <context:component-scan base-package="com.soyoungboy.distribute.handler"/>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

</beans>

在需要调用服务的bean中注入接口类型的bean

import java.util.Map;

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

import com.alibaba.dubbo.demo.DemoService;

@Controller
public class SayHelloHandler {
    
    @Autowired
    private DemoService demoService;
    
    @RequestMapping("/testSayHello")
    public String sayHello(Map<String, Object> map) {
        
        //像调用本地方法一样调用远程服务
        String sayHello = demoService.sayHello("Tom");
        
        map.put("result", sayHello);
        
        return "result";
    }

}

 

以上是关于Dubbo+Zookeeper的主要内容,如果未能解决你的问题,请参考以下文章

dubbo与zookeeper的关系

9.2 SpringBoot使用Zookeeper和Dubbo

Dubbo官方的监控后台管理

Dubbo的RPC远程过程调用+Dubbo的负载均衡+Zookeeper注册中心

zookeeper和dubbo的关系[转]

zookeeper在dubbo到底起了什么作用