什么是dubbo?

Apache Dubbo™ (incubating)是一款由阿里团队开发的一款优秀的RPC框架高性能Java RPC框架。官网:http://dubbo.apache.org/zh-cn/index.html

rpc 浏览器 rpc官网_zookeeper

什么是RPC?

RPC是远程过程调用(Remote Procedure Call)的缩写形式。它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。 RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。


关于dubbo的背景,需求和架构这里就不过多介绍了,官方介绍非常全面。http://dubbo.apache.org/zh-cn/docs/user/preface/background.html

rpc 浏览器 rpc官网_分布式_02

注册中心

dubbo支持多种注册中心,推荐使用ZooKeeper。http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html

rpc 浏览器 rpc官网_分布式_03

使用docker部署ZooKeeper

安装docker

yum -y install docker-io

启动docker

systemctl start docker

设置docker开机自启

systemctl enable docker

拉取zk镜像

docker pull zookeeper:3.5.7

创建容器

docker create --name zk -p 2181:2181 zookeeper:3.5.7

启动容器

docker start zk

查看是否启动成功

docker ps

已经成功启动

rpc 浏览器 rpc官网_java_04

springboot集成dubbo

项目结构

rpc 浏览器 rpc官网_zookeeper_05

父工程下有两个子模块,一个dubbo-service模块(服务提供者)一个dubbo-consumer模块(服务消费者)

父工程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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>

    <groupId>com.sunyuqi.dubbo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-service</module>
        <module>dubbo-consumer</module>
    </modules>

    <dependencies>
        <!--添加SpringBoot测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--添加dubbo的springboot依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--添加zookeeper依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--添加springboot的maven插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

dubbo-service模块

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>
        <groupId>com.sunyuqi.dubbo</groupId>
        <artifactId>dubbo-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-service</artifactId>

    <dependencies>
        <!--添加springboot依赖,非web项目-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
</project>

配置文件application.properties

# Spring boot application
spring.application.name = dubbo-service
server.port = 9090

# Service version
dubbo.service.version = 1.0.0

# 服务的扫描包
dubbo.scan.basePackages  = com.sunyuqi.dubbo.service

# 应用名称
dubbo.application.name = dubbo-provider-demo

# 协议以及端口
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20882

# zk注册中心
dubbo.registry.address = zookeeper://192.168.130.128:2181
# 超时时间
dubbo.registry.timeout=10000
# 元数据中心
dubbo.metadata-report.address=zookeeper://192.168.130.128:2181

实体类User

package com.sunyuqi.dubbo.dubbo.pojo;

public class User implements java.io.Serializable {

    private static final long serialVersionUID = -7341603933521593227L;

    private Long id;

    private String username;

    private String password;

    private Integer age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

service接口

package com.sunyuqi.dubbo.dubbo.service;

import com.sunyuqi.dubbo.dubbo.pojo.User;

import java.util.List;

public interface UserService {

    /**
     * 查询所有的用户数据
     *
     * @return
     */
    List<User> queryAll();

}

service实现类 为了测试方便,我们就不查询数据库了,进行模拟查询

package com.sunyuqi.dubbo.dubbo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sunyuqi.dubbo.dubbo.pojo.User;
import com.sunyuqi.dubbo.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;


@DubboService(version = "${dubbo.service.version}") //声明这是一个dubbo服务,声明版本号
public class UserServiceImpl implements UserService {

	/**
	 * 实现查询,这里做模拟实现,不做具体的数据库查询
	 */
	public List<User> queryAll() {
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 10; i++) {
			User user = new User();
			user.setAge(10 + i);
			user.setId(Long.valueOf(i + 1));
			user.setPassword("123");
			user.setUsername("username_" + i);
			list.add(user);
		}
		return list;
	}
}

引导类 运行引导类,即可将服务信息注册到zookeeper

package com.sunyuqi.dubbo.dubbo;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;


@SpringBootApplication
public class DubboProvider {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DubboProvider.class)
                .web(WebApplicationType.NONE) // 非 Web 应用
                .run(args);
    }
}

dubbo-consumer模块

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>
        <groupId>com.sunyuqi.dubbo</groupId>
        <artifactId>dubbo-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
</project>

配置文件application.properties

# Spring boot application
spring.application.name = dubbo-consumer
server.port = 9091

# 应用名称
dubbo.application.name = dubbo-consumer-demo

# zk注册中心
dubbo.registry.address = zookeeper://192.168.130.128:2181
dubbo.registry.timeout=10000
# 元数据中心
dubbo.metadata-report.address=zookeeper://192.168.130.128:2181

service接口 无需编写实现类

package com.sunyuqi.dubbo.service;

import com.sunyuqi.dubbo.pojo.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface UserService {

    /**
     * 查询所有的用户数据
     *
     * @return
     */
    List<User> queryAll();
}

User实体类

package com.sunyuqi.dubbo.pojo;

public class User implements java.io.Serializable {

    private static final long serialVersionUID = -7341603933521593227L;

    private Long id;

    private String username;

    private String password;

    private Integer age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

引导类

package com.sunyuqi.dubbo;

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

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

测试类

package com.sunyuqi;

import com.sunyuqi.dubbo.DubboConsumer;
import com.sunyuqi.dubbo.pojo.User;
import com.sunyuqi.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DubboConsumer.class)
public class UserServiceTest {

    @DubboReference(version = "1.0.0", loadbalance = "roundrobin")
    private UserService userService;

    @Test
    public void testQueryAll() {
        List<User> users = (List<User>) this.userService.queryAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

运行代码测试,成功从注册中心拉取到服务

rpc 浏览器 rpc官网_java_06