欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot+Dubbo+zookeeper 搭建一个简单的单机服务实现RPC调用

程序员文章站 2022-04-28 08:58:57
...

目录

一、准备工作

二、创建Maven项目

三、启动工程

四、使用dubbo-admin实现服务监控

五、为什么要使用dubbo

六、其他可以实现RPC服务


一、准备工作

安装zookeeper,我们安装一个单一的zookeeper

这里使用docker方便安装

docker pull zookeeper

docker run --privileged=true -d --name zookeeper --publish 2181:2181  -d zookeeper:latest

测试一下是否安装成功

docker exec -it zookeeper /bin/bash

cd bin

./zkCli.sh

ls /

stat /

安装正常以上是可以出结果的。

二、创建Maven项目

SpringBoot+Dubbo+zookeeper 搭建一个简单的单机服务实现RPC调用

创建一个LearnDubbo的项目,新增两个子模块

1.模块provider-dubbbo 的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.xxx</groupId>
    <artifactId>provider-dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learnDubboProvider</name>
    <description>dubbo生产</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!--引入zookeeper的客户端工具-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

application.yml 生产者的配置

spring:
  dubbo:
    application:
      name: provider1-ticket
    registry:
      address: zookeeper://127.0.0.1:2181
    scan:
      base-packages: service
    annotation:
      package: service
    protocol:
      name: dubbo
      port: 20880

server:
 port: 8081

启动类 service.LearnDubboProviderApp

package service;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
@EnableDubbo //一个包含3个注解
public class LearnDubboProviderApp{

    @Autowired
    TicketService ticketService;

    @RequestMapping("/")
    @ResponseBody
    public String home2(){return "Hello World!"+ticketService.getTicket();}

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

}

service.TicketService

package service;

public interface TicketService {
    public String getTicket();
}

service.TicketServiceImpl,注意这里的Service是dubbo的,目的是暴漏服务。(这个地方因为springbooot和dubbo的版本问题报错比较折腾,后来发现使用较低版本就好了)

package service;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Component
@Service
public class TicketServiceImpl implements TicketService{
    @Override
    public String getTicket() {
        return "《我和我的家乡》";
    }
}

2模块consumer-dubbbo 的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.xxx</groupId>
    <artifactId>consumer-dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learnDubboConsumer</name>
    <description>dubbo消费</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version><exclusions>
        </exclusions>

        </dependency>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>

        <!--引入zookeeper的客户端工具-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

application.yml 生产者的配置 注意端口是8082

spring:
  dubbo:
    application:
      name: consumer1-ticket
    registry:
      address: zookeeper://127.0.0.1:2181
    scan:
      base-packages: service
    annotation:
      package: service
    protocol:
      name: dubbo
      port: 20880
server:
  port: 8082

启动类 service.LearnDubboConsumerApp 这里 @EnableDubbo 注解可以直接从yml加载,比较契合springboot的特点,强烈推荐,网上说的xml配置dubbo大家感兴趣可以试一试

package service;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
@EnableDubbo //消费方不可使用生产方的配置
//@ImportResource(locations = {"classpath:dubbo/dubbo-consumer.xml"})
public class LearnDubboConsumerApp{

    @Autowired
    private UserService userService;

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

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

}

service.TicketService 注意这个interface只是为了访问zookeeper暴漏的服务,不去实现它

package service;

public interface TicketService {
    public String getTicket();
}

service.UserService  调用服务的消费方(注意这里@Reference如果报空指针,可检查zookeeper中服务是否成功注册)

package service;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService{

    @Reference
    TicketService ticketService;

    public String hello(){
        String ticket = ticketService.getTicket();
        System.out.println("买到票了:"+ticket);
        return "买到票了:"+ticket;
    }
}

三、启动工程

1、先启动生产者服务 provider-dubbo;再启动消费者服务consumer-dubbo

2、在浏览器输入 http://localhost:8082

可见 显示 买到票了 《我和我的家乡》,这样我们就成功演示了SpringBoot+Dubbo+zookeeper 搭建一个简单的单机服务实现RPC调用

如果为能成功显示,检查依赖版本号和是否配置问题。

3、如何查看zookeeper是否注册了服务

可回到一、准备阶段,进入zkCli.sh中,输入命令 

ls /dubbo/service.TicketService/providers

结果如下

[dubbo%3A%2F%2F192.168.113.189%3A20880%2Fservice.TicketService%3Fanyhost%3Dtrue%26application%3Dprovider1-ticket%26dubbo%3D2.6.0%26generic%3Dfalse%26interface%3Dservice.TicketService%26methods%3DgetTicket%26pid%3D1773%26side%3Dprovider%26timestamp%3D1605245796825]

同理可检查消费者服务是否订阅

ls /dubbo/service.TicketService/consumers

结果如下

[consumer%3A%2F%2F192.168.113.189%2Fservice.TicketService%3Fapplication%3Dconsumer1-ticket%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.6.0%26interface%3Dservice.TicketService%26methods%3DgetTicket%26pid%3D1700%26side%3Dconsumer%26timestamp%3D1605244783536]

四、使用dubbo-admin实现服务监控

首先docker拉取,可以直接run等几秒就自动下载,mac下非常方便,对docker不熟悉的童鞋可自习docker容器技术。

docker run --name dubbo-admin -p 8888:8080 --link zookeeper -d qq986945193/david_dubbo_admin

这里运行后使用浏览器 localhost:8888 进入用户名密码都是root,进入后可见如下画面,实现了服务治理的可视化

SpringBoot+Dubbo+zookeeper 搭建一个简单的单机服务实现RPC调用

 

五、为什么要使用dubbo

dubbo是一个比较简单的微服务架构,轻量级,相比springcloud而言虽功能不及其齐全,但也有可圈可点地方。

我们在不同的服务器之间调用接口当然可以使用http请求,但是这样有很大的问题,每次都需要写发送Http的代码,这样不太好,很不优雅。我们考虑的是能否像本地调用一样呢,所以我们考虑使用RPC,而使用RPC呢dubbo则是一个不错的选择

 

六、其他可以实现RPC服务

Eureka、Nacos、Consul、CoreDNS、Zookeeper(dubbo)