前言

当下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="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.learn</groupId>
<artifactId>springboot-dubbo-zookeeper</artifactId>
<version>1.0.0.LEARN</version>
<packaging>pom</packaging>

<name>springboot-dubbo-nacos</name>
<url>http://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 -->
<!-- https://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>
<!-- https://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>
<!-- https://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 -->
<!-- https://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>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://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="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<name>common</name>
<url>http://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="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<name>provider</name>
<url>http://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 -->
<!-- https://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>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Dubbo + Zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!-- https://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="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<name>consumer</name>
<url>http://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 -->
<!-- https://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>
<!-- https://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-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!-- https://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 服务:

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

成功返回了 Hello World!

总结

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

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

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

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

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

SpringBoot+Dubbo+Zookeeper 实例的更多相关文章

  1. 搭建SpringBoot+dubbo+zookeeper+maven框架(二)

    上一篇文章是关于搭建SpringBoot+dubbo+zookeeper+maven框架的,但是里面的功能还不够完善,今天就日志管理方面做一些改善. 下了demo的网友可能会发现项目在启动时会有警告: ...

  2. 搭建SpringBoot+dubbo+zookeeper+maven框架(一)

    这几天项目还没来,所以就自己试着参考网上的一些资料,搭建了一个SpringBoot+dubbo+zookeeper+maven框架,网上参考的很多资料照着他们一步一步搭建,最后很多都运行不通,很是郁闷 ...

  3. SpringBoot + Dubbo + zookeeper 搭建简单分布式服务

    SpringBoot + Dubbo + zookeeper 搭建简单分布式服务 详细操作及源码见: https://github.com/BillyYangOne/dubbo-springboot

  4. springboot+dubbo+zookeeper+mybatis

    参考地址:https://www.cnblogs.com/gaopengfirst/p/9555240.html 首先创建一个maven项目: 再在该父项目中创建3个module,分别是:provid ...

  5. 分布式应用开发 | SpringBoot+dubbo+zookeeper实现服务注册发现 | 远程服务调用

    前言 通过新建两个独立服务--提供者.消费者,模拟两个独立分布的应用,通过使用dubbo+zookeeper来实现远程服务调用. 目录 项目搭建 provider-server consumer-se ...

  6. Springboot+dubbo+zookeeper整合

    本想自己搭建一个Spring+dubbo+zookeeper整合好的框架,想寻找个最佳实现但是遇到各种各样的问题,只好自己看看dubbo starter的源码 整理如下: 通过打上断点来看配置的对不对 ...

  7. SpringBoot+Dubbo+ZooKeeper+Maven入门实践

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11798626.html 注*** 本实例为仅适合初学者,关于dubbo和springboot以 ...

  8. 带着新人学springboot的应用11(springboot+Dubbo+Zookeeper 上)

    这次说个在大型项目比较常见的东西,就是分布式,分布式到底是个什么东西呢?概念太大,不好说,就像刚学javaee的人问你,什么是web啊,什么是spring啊等等,你可能觉得,这个东西我好像知道,但是用 ...

  9. SpringBoot&Dubbo&Zookeeper远程调用项目搭建

    序言 Dubbo一款分布式服务框架,作为阿里巴巴SOA服务化治理方案的核心框架,通过高性能和透明化的RPC实现服务的远程调用,对服务的负载均衡以及项目的耦合性提供很强的解决方式;具体Dubbo的介绍和 ...

随机推荐

  1. Scrapy 项目:腾讯招聘

    目的: 通过爬取腾讯招聘网站(https://careers.tencent.com/search.html)练习Scrapy框架的使用 步骤: 1.通过抓包确认要抓取的内容是否在当前url地址中,测 ...

  2. java安全管理器SecurityManager

    本文转载自java安全管理器SecurityManager 导语 这是一篇对Java安全管理器入门的文章,目的是简单了解什么是SecurityManager,对管理器进行简单配置,解决简单问题. 比如 ...

  3. 微信小程序:标签字符串直接变成标签来显示要通过富文本技术

    rich-text标签存在nodes属性直接接受标签字符串

  4. Docker 一键安装及Docker管理面板Portainer中文汉化

       前言 Docker接触了一段时间了,批量操作过程中感觉太繁琐,所以找到了好评率比较高的Portainer面板,使用后感觉的确不错所以准备拿出来精力来做个汉化版,过程中发现词条非常多,所以暂时先汉 ...

  5. Blackduck的Hub安装教程

    1 产品介绍 Black Duck 是最早进行开源代码检测工具开发的公司,其产品包括Protex 和HUB,Protex 强调检测的精度和准确性,而HUB 强调检测的速度和易用性. 1.1 Prote ...

  6. brew安装Nginx

    目录 安装流程 常用命令记录 典型配置方式 查看启动状态是否有报错 php 启动 参考 安装流程 这里使用 brew 来安装软件. 安装 brew install nginx 查看安装信息(经常用到, ...

  7. 通过序列号Sequence零代码实现订单流水号

    序列号管理 本文通过产品编码和订单流水号介绍一下序列号(Sequence)在crudapi中的应用. 概要 序列号 MySQL数据库没有单独的Sequence,只支持自增长(increment)主键, ...

  8. PHP配置 4. 虚拟主机配置open_basedir

    将/usr/local/php/etc/php.ini中open_basedir注释掉,编辑虚拟主机配置open_basedir #vim /usr/local/apache2 .4/conf/ext ...

  9. wireshark如何抓取分析https的加密报文

    [问题概述] https流量基于ssl/tls加密,无法直接对报文进行分析. [解决方案] 方案1 -- 利用"中间人攻击"的代理方式抓包分析.整个方案过程比较简单,这里不赘述,大 ...

  10. 数据库遇到的问题——mysql在线修改表结构大数据表的风险与解决办法归纳

    互联网应用会频繁加功能,修改需求.那么表结构也会经常修改,加字段,加索引.在线直接在生产环境的表中修改表结构,对用户使用网站是有影响. 以前我一直为这个问题头痛.当然那个时候不需要我来考虑,虽然我们没 ...