SpringBoot+Dubbo+Zookeeper 实例

前言

    当下Java 生态环境里面,微服务占据了非常大的份额,现在大部分新开发的 Java选型的后台程序都很奇妙的会跟微服务发生一些关系。那目前市面上主流的微服务方向主要有 Spring 家族推出的Spring Boot Cloud 还有阿里巴巴推出的 Dubbo 服务。

    这两种服务我都大体上的使用过,说一下我的感受。我感觉Spring Boot Cloud 周边的服务功能框架比较健全,如果希望快速的搭建起一个大型的商业类型的项目可以选择Spring Boot Cloud,毕竟人家血统纯正的 Spring Boot体系,以及最近挺火的 Spring Cloud Alibaba 感觉也挺不错的。Dubbo 服务这边大体就是使用 nacos 或者 zookeeper 作为注册中心来实现微服务,可能周边的服务框架没有 Spring BootCloud 那样健全,但是个人感觉 Dubbo 的服务调用会比 feign 和 ribbon 舒服一点。所以我就想粗略的讲一下 Dubbo 和 Zookeeper 怎么使用。

 

    创建服务 

    创建一个 maven 项目,下面三个子项目,分别是 common,provider,consumer。

    curator-recipes 和 curator-client 这两个服务包是因为当前 dubbo 版本和 zk 版本的兼容性问题,后续版本更新后应该就不需要开发人员引入了。

 parent pom.xml:

<project xmlns="//maven.apache.org/POM/4.0.0"
  xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId>
  <artifactId>springboot-dubbo-zookeeper</artifactId>
  <version>1.0.0.LEARN</version>
  <packaging>pom</packaging><name>springboot-dubbo-nacos</name>
  <url>//maven.apache.org</url><properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties><modules>
    <module>common</module>
    <module>consumer</module>
    <module>provider</module>
  </modules><dependencyManagement>
    <dependencies>
      <!-- common -->
      <dependency>
        <groupId>com.learn</groupId>
        <artifactId>common</artifactId>
        <version>${version}</version>
      </dependency><!-- Spring Boot -->
      <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.3.5.RELEASE</version>
        <scope>test</scope>
      </dependency>
      <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.5.RELEASE</version>
      </dependency>
      <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.3.5.RELEASE</version>
      </dependency><!-- Dubbo + Zookeeper -->
      <!-- //mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
      </dependency>
      <!-- //mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
      <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>5.1.0</version>
      </dependency>
      <!-- //mvnrepository.com/artifact/org.apache.curator/curator-client -->
      <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-client</artifactId>
        <version>5.1.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Common 模块

    common 模块用作定义 provider 和 consumer 模块当中公共的类和接口,为了怎么代码的耦合度。如果不使用 common 模块,就必须在 provider 和 consumer 模块当中都定义调用接口和实体类。

 common pom.xml:

<?xml version="1.0"?>
<project
  xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="//maven.apache.org/POM/4.0.0"
  xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion>
  <artifactId>common</artifactId>
  <name>common</name>
  <url>//maven.apache.org</url>
  <packaging>jar</packaging><parent>
    <groupId>com.learn</groupId>
    <artifactId>springboot-dubbo-zookeeper</artifactId>
    <version>1.0.0.LEARN</version>
  </parent><properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties><dependencies>
  </dependencies>
</project>

定义服务调用接口:

package com.learn.common.handler.service;
​
/**
 * 调用接口
 * @author xiaohu
 *
 */
public interface IHelloService {
  
  public String hello();
  
}

provider 模块

    provider 作为服务的提供者,只需要实现 common 中定义的接口就行了,然后就是把服务通过 Dubbo 注册到 zookeeper 注册中心。

 provider pom.xml:

<?xml version="1.0"?>
<project
  xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="//maven.apache.org/POM/4.0.0"
  xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion>
  <artifactId>provider</artifactId>
  <name>provider</name>
  <url>//maven.apache.org</url>
  <packaging>jar</packaging><parent>
    <groupId>com.learn</groupId>
    <artifactId>springboot-dubbo-zookeeper</artifactId>
    <version>1.0.0.LEARN</version>
  </parent><properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties><dependencies>
    <!-- common -->
    <dependency>
      <groupId>com.learn</groupId>
      <artifactId>common</artifactId>
    </dependency><!-- Spring Boot -->
    <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency><!-- Dubbo + Zookeeper-->
    <!-- //mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.apache.curator/curator-client -->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-client</artifactId>
    </dependency>
  </dependencies>
</project>

启动类:

package com.learn.provider;
​
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
/**
 * 服务提供者启动类
 * 
 * @author xiaohu
 *
 */
// 激活 Dubbo 服务
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
  public static void main(String[] args) {
    System.out.println("Provider Application Starter ...");
    SpringApplication.run(ProviderApplication.class, args);
    System.out.println("Provider Application Starter Successful!");
  }
}

application.properties:

# 启动端口
server.port=8000# dubbo + zookeeper
dubbo.application.name=provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
# -1 表示自动获取一个未被占用的端口
dubbo.protocol.port=-1
dubbo.protocol.name=dubbo

接口实现:

package com.learn.provider.handler.service;
​
import org.apache.dubbo.config.annotation.DubboService;
​
import com.learn.common.handler.service.IHelloService;
​
/**
 * IHelloService 实现类
 * @author xiaohu
 *
 */
@DubboService
public class HelloService implements IHelloService {
​
  public String hello() {
    // TODO Auto-generated method stub
    return "Hello World!";
  }
  
}

consumer 模块

consumer 作服务的消费者,调用服务接口。

consumer pom.xml:

<?xml version="1.0"?>
<project
  xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="//maven.apache.org/POM/4.0.0"
  xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion>
  <artifactId>consumer</artifactId>
  <name>consumer</name>
  <url>//maven.apache.org</url>
  <packaging>jar</packaging><parent>
    <groupId>com.learn</groupId>
    <artifactId>springboot-dubbo-zookeeper</artifactId>
    <version>1.0.0.LEARN</version>
  </parent><properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties><dependencies>
    <!-- common -->
    <dependency>
      <groupId>com.learn</groupId>
      <artifactId>common</artifactId>
    </dependency><!-- Spring Boot -->
    <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Dubbo + Zookeeper-->
    <!-- //mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
    </dependency>
    <!-- //mvnrepository.com/artifact/org.apache.curator/curator-client -->
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-client</artifactId>
    </dependency>
  </dependencies>
</project>

启动类:

package com.learn.consumer;
​
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
/**
 * 服务消费者启动类
 * 
 * @author xiaohu
 *
 */
// 激活 Dubbo
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
  public static void main(String[] args) {
    System.out.println("Consumer Application Starter ...");
    SpringApplication.run(ConsumerApplication.class, args);
    System.out.println("Consumer Application Starter Successful!");
  }
}

application.properties:

# 启动端口
server.port=8001# dubbo + zookeeper
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
# -1 表示随机一个未被使用端口 dubbo.protocol.port=-1 dubbo.protocol.name=dubbo

服务调用:

package com.learn.consumer.endpoint.controller;
​
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
​
import com.learn.common.handler.service.IHelloService;
​
/**
 * 服务调用
 * @author xiaohu
 *
 */
@RestController
public class HelloController {
  
  @DubboReference
  private IHelloService helloService;
  
  @RequestMapping(value = "/hello", method = RequestMethod.GET)
  public String hello() {
    return helloService.hello();
  }
  
}

运行测试一下:

先启动 zookeeper 服务:

再依次启动 provider 和 consumer 服务:

两个服务都已经成功的起来了。可以访问 //localhost:8001/hello 测试一下。

成功返回了 Hello World!

 

总结

    到此,一个简单的通过 Dubbo 完成的服务调用就写完了。这里需要说明,common 模块并不是必须的,只是个人的编码习惯,为的实体类和接口可以复用而已。如果不喜欢的可以在调用和实现模块中定义就行了,但是必须保证接口内容能对上。

    服务启动顺序最好是按照,服务提供者优先启动,服务消费者后面启动的原则。

    现在来说 Dubbo 经过了一段时间的断维之后,又开始维护了。我觉得这个是一件好的事情,给我们提供了更多元化的一个选择。也希望这篇文章能够帮助到大家,谢谢。

 

 有兴趣的话可以扫码或搜索 “边缘技术” 关注我的微信公众号,非常感谢大家。

# -1 表示自动获取一个未被占用的端口