Dubbo+Zookeeper简单使用

一、安装

1.安装zookeeper注册中心

1.1上传zookeeper安装包zookeeper-3.4.6.tar.gz到Linux服务器:

1.2 解压缩:

[root@localhost ~]# tar xvf zookeeper-3.4.6.tar.gz -C /usr/local/

1.3 修改解压缩目录:

先到/usr/local/,再mv zookeeper-3.4.6/ zookeeper

1.4 启动ZooKeeper:

先hostname: 查看本地机器名

然后编辑主机名称文件vi /etc/hosts 增加主机IP地址 127.0.0.1 主机名称

如:

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

然后到zookeeper的conf目录下复制ZooKeeper的配置文件 cp zoo_sample.cfg zoo.cfg

再到zookeeper的bin目录下执行./zkServer.sh start启动服务,需要停止服务时./zkServer.sh stop(停止)

1.5查看ZooKeeper的监听端口: netstat -antp|grep 2181结果为LISTEN

1.6开放防火墙2181端口:

 firewall-cmd --add-port=2181/tcp --permanent
  firewall-cmd --reload

2.安装Dubbo的监控中心:

2.1 上传安装包到linux的Tomcat发布目录(webapps):

dubbo-admin-2.6.1.war

2.2 修改安装包的名字:

mv dubbo-admin-2.6.1.war ROOT.war

2.3 删除原先tomcat发布目录下的 ROOT

rm -rf ROOT

2.4 启动tomcat到tomcat的bin目录下:./startup.sh

2.5 开放8080端口防火墙:

firewall-cmd --add-port=8080/tcp --permanent
 firewall-cmd --reload

2.6 访问dubbo监控中心:http://虚拟机Linux的ip:8080

账号、密码默认是root

zookeeper服务器注册端口2181,对外服务端口20881,消费端20880

二、spring整合Dubbo、Zookeeper

1.创建服务的提供者(maven工程)

1.1 导入Dubbo、ZooKeeper的依赖(pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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.xxx</groupId>
    <artifactId>Spring_Dubbo_ServiceProvider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 需要将服务打为war包 -->
    <packaging>war</packaging>

    <!-- 定义版本变量 -->
    <properties>
        <spring.version>4.3.10.RELEASE</spring.version>
        <webVersion>3.0</webVersion>
    </properties>


    <dependencies>
        <!-- 导入spring基本依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- 导入webmvc依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 导入Dubbo、Zookeeper依赖包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

    <!--导入tomcat编译和运行插件,后边直接用插件启动项目-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8081</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2创建Spring的配置文件:applicationContext-service.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置Dubbo的应用名称 -->
    <dubbo:application name="myDubboService-provider"></dubbo:application>

    <!-- 配置服务的注册地址,协议://Linux服务器地址:访问端口 -->
    <dubbo:registry address="zookeeper://192.168.152.129:2181"></dubbo:registry>

    <!-- 扫描服务所在的包 -->
    <dubbo:annotation package="com.xxx.service"></dubbo:annotation>
</beans>

1.3在main(idea)下创建webapp/WEN-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

1.4创建服务的业务接口及实现类:

package com.xxx.service;

public interface UserService {
    String sayHello();
}
package com.xxx.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xxx.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String sayHello() {
        return "This is UserServiceProvider";
    }
}

1.5启动ZooKeeper、Dubbo的监控中心:

查看服务提供者是否注册成功:访问http://Linux地址:8080/,看服务数,应用数是否改变,如果之前没服务应该是从0变为1

2.创建服务的调用者(maven)

调用者和消费者里创建的接口要一样。

2.1导入依赖:
与服务提供者相同,只需修改tomcat端口号为8082,注意别忘了打包方式为war

<packaging>war</packaging>

2.2创建服务消费者spring的配置文件applicationContext-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描控制器包 -->
    <context:component-scan base-package="com.xxx.controller"></context:component-scan>

    <!-- 防止乱码 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"></constructor-arg>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 配置Dubbo的应用名称 -->
    <dubbo:application name="myDubboService-consumer"></dubbo:application>

    <!-- 配置服务的注册地址,协议://Linux服务器地址:访问端口 -->
    <dubbo:registry address="zookeeper://192.168.152.129:2181"></dubbo:registry>

    <!-- 扫描服务所在的包 -->
    <dubbo:annotation package="com.xxx.controller"></dubbo:annotation>
</beans>

2.3 创建web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext-consumer.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

2.4创建与服务提供者相同的service接口,但不提供service实现类

package com.xxx.service;

public interface UserService {
    String sayHello();
}

2.5创建控制器类:

package com.xxx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xxx.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference//注意这个注解来自com.alibaba.dubbo.config.annotation.Reference
    private UserService userService;

    @ResponseBody
    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }
}

2.6创建webapp目录:
在webapp/WEB-INF/下创建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <!-- 加载spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext-consumer.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

2.7启动调用者和提供者服务:通过刚才配置的maven插件下的tomcat7中的tomcat7:run启动

在浏览器访问 http://127.0.0.1:8082/user/sysHello.do,访问的是消费者的地址端口加项目路径,实际调用的是服务提供者的方法。

三、SpringBoot整合Dubbo、ZooKeeper

3.1 创建springboot父工程什么都不用导,可以删掉src目录:

将pom文件中的依赖和插件删除,增加打包方式pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxx</groupId>
    <artifactId>springboot_dubbo_zookeeper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_dubbo_zookeeper</name>
    <description>Demo project for Spring Boot</description>
    <!-- 增加父工程的打包方式,父工程不需要启动 -->
    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>

</project>

3.2 创建接口模块(module maven):我创建的叫UserService模块

模块里需要做的是增加一个接口

package com.xxx.service;

public interface UserService {
    String sayHello(String name);
}

然后在maven中使用一下install命令,将这个模块安装到本地仓库,方便后边引用。

3.3 创建服务提供者模块也是maven模块:

3.3.1 在pom文件中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>springboot_dubbo_zookeeper</artifactId>
        <groupId>com.xxx</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>MyProvider</artifactId>

    <dependencies>
        <!-- 导入springboot的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <!-- 导入springboot整合dubbo的依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!-- 导入刚才创建被安装到本地仓库的的UserService模块,用于提供接口 -->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>UserService</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

3.3.2 创建配置文件application.yml用于springboot对dubbo的一些配置

dubbo:
  registry:
    #配置dubbo服务器地址和端口
    address: 192.168.152.129:2181
    #配置使用的协议
    protocol: zookeeper
  #对协议的其他的配置
  protocol:
    port: 20881
    name: dubbo
  #对应用的配置E
  application:
    name: mySpringBootDubboSesrvice
    #关闭质量保证
    qosEnable: false
  #配置扫面服务包
  scan:
    basePackages: com.xxx.service

3.3.3 创建接口的实现类,实现导入的UserService本地依赖中的方法:

package com.xxx.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.xxx.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    public String sayHello(String name) {
        return "欢迎:"+name;
    }
}

3.3.4 编写springboot启动类:(注册服务)

package com.xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyProviderApplicationService {
    public static void main(String[] args) {
        SpringApplication.run(MyProviderApplicationService.class,args);
    }
}

3.3.5 启动服务提供者,查看监控中心服务提供者是否注册成功…

如能看到,说明注册成功,看不到,分析问题,找原因(注册地址、@Service、端口、扫描包名)

3.4 创建服务的消费者maven:

3.4.1 导入依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>springboot_dubbo_zookeeper</artifactId>
        <groupId>com.xxx</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>MyConsumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <!-- 导入springboot整合dubbo的依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!-- 导入刚才创建被安装到本地仓库的的UserService模块,用于提供接口 -->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>UserService</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.4.2 创建配置文件application.yml:

dubbo:
  registry:
    address: 192.168.152.129:2181
    protocol: zookeeper
  protocol:
    name: dubbo
  application:
    name: springbootConsumer
    qosEnable: false
server: 8081

3.4.3 创建控制器:

package com.xxx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xxx.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Reference
    UserService userService;
    @RequestMapping("/sayHello")
    public String sayHello(String name){
        return userService.sayHello(name);
    }
}

3.4.4 创建启动类:

package com.xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyapplicationConsumer {
    public static void main(String[] args) {
        SpringApplication.run(MyapplicationConsumer.class,args);
    }
}

3.4.5 查看监控中心,看服务消费者是否注册成功:

3.5.6 进行访问测试:

http://127.0.0.1:8081/sayHello?name=aaa

如果返回:欢迎aaa

即 成功