java ee / rpc / google Protocol Buffers / protobuf / xml / json / dubbo / Hessian / Thrift / JSONRPC / Avro

s

1、protobuff 资料

http://code.google.com/p/protobuf/downloads/list

http://protobuf.googlecode.com/files/protobuf-2.0.0beta.zip

http://protobuf.googlecode.com/files/protoc-2.0.0beta-win32.zip

https://juejin.cn/post/7175805388619513911

2、protobuff对比

特性 \ 类型 xml json protobuf
数据结构支持 简单结构 简单结构 复杂结构
数据保存方式 文本 文本 二进制
数据保存大小
编解码效率
语言支持程度 覆盖主流语言 覆盖主流语言 覆盖主流语言

3、编译环境

操作系统:CentOS Linux release 7.9.2009 (Core)

编译器版本:gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

protobuf 版本:3.17.3

4、protibuff 使用

  • 为什么要使用 protobuf ,而不使用 xml、json 等其他数据结构化标准;
  • 在 centos7 下怎么编译安装 protobuf 以及可能遇到哪些安装问题;
  • 如何将 proto IDL 文件生成 C++ 源代码;
  • protobuf 普通数据接口如何使用;
  • protobuf 的反射是什么?以及反射接口如何使用;
  • protobuf 的 RPC 接口有什么用处?以及如何使用。
 

- Dubbo 分布式服务框架

Dubbo 是一个分布式服务框架,支持多种协议进行服务之间的通信。Dubbo 协议详解如下,这里用表格展示:

协议 描述
Dubbo - Dubbo 协议是 Dubbo 框架自定义的二进制协议。<br>- 使用Dubbo协议时,服务提供者和消费者之间的通信采用二进制方式,性能更高。<br>- Dubbo 协议的 Header 部分包含序列化器、压缩器、请求类型等信息。
RMI - 基于 Java RMI(远程方法调用)的协议。<br>- 使用 Java 序列化进行对象的传输。<br>- 通常需要 Java 类实现 java.io.Serializable 接口。<br>- 不推荐在不同语言之间使用,因为它是 Java 特有的。
Hessian - 基于 Hessian 二进制协议的协议。<br>- 使用 Hessian 序列化进行对象的传输。<br>- 支持多种语言,是一种跨语言的协议。<br>- 性能相对较好,序列化和反序列化速度快。
HTTP - 基于 HTTP 协议的协议。<br>- 使用 HTTP 协议进行通信,适用于 Web 环境。<br>- 通过 URL 传递参数,数据以 JSON 或 XML 格式进行编码。<br>- 开放、通用,但性能相对较差。
WebService - 基于 SOAP 协议的 WebService 协议。<br>- 使用 XML 进行数据传输。<br>- 兼容性好,支持跨语言调用。<br>- 适用于需要 WebService 标准的场景,但性能相对较差。
Thrift - 基于 Apache Thrift 协议的协议。<br>- 使用 Thrift 二进制协议进行通信,提供了一套接口定义语言(IDL)进行接口定义。<br>- 跨语言支持,性能较好,适用于高性能、跨语言的分布式系统。
Memcached - 基于 Memcached 协议的协议。<br>- 使用 Memcached 协议进行通信,适用于一些缓存场景。<br>- 通过 TCP 连接,使用简单的文本协议进行通信,性能较好。<br>- 不支持异步调用,适用于简单的场景。
Redis - 基于 Redis 协议的协议。<br>- 使用 Redis 协议进行通信,适用于一些缓存场景。<br>- 通过 TCP 连接,使用 RESP(Redis Serialization Protocol)进行通信,性能较好。<br>- 不支持异步调用,适用于简单的场景。
JSONRPC - 基于 JSON-RPC 协议的协议。<br>- 使用 JSON 进行数据传输。<br>- 通信通过 HTTP 或者 Socket 进行,适用于 Web 环境。<br>- 简单、通用,但性能相对较差。
Avro - 基于 Apache Avro 协议的协议。<br>- 使用 Avro 二进制协议进行通信,提供了一种丰富的数据定义语言。<br>- 跨语言支持,性能较好,适用于高性能、跨语言的分布式系统。

请注意,Dubbo 支持多种协议,可以根据实际需求选择合适的协议。表格中的描述仅是对每个协议的简要概述,具体的使用和配置可以参考 Dubbo 官方文档和相关协议的官方文档。

- Dubbo 范例

以下是一个简单的Dubbo示例,演示如何创建Dubbo服务和消费者。在这个例子中,我们将使用Spring作为Dubbo的容器,并通过ZooKeeper作为注册中心。

 

以下是一个简单的Dubbo示例,演示如何创建Dubbo服务和消费者。在这个例子中,我们将使用Spring作为Dubbo的容器,并通过ZooKeeper作为注册中心。

### 1. 定义接口:

首先,定义一个服务接口 `HelloService`,该接口包含一个简单的方法:

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

### 2. 实现接口:

创建一个实现上述接口的服务提供者 `HelloServiceImpl`:

public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

### 3. 配置服务提供者:

使用Spring配置文件 `provider.xml` 配置Dubbo服务提供者:

<!-- provider.xml -->

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务提供者应用名称 -->
    <dubbo:application name="hello-provider"/>

    <!-- 注册中心地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 服务提供者协议和端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 引用服务接口 -->
    <bean id="helloService" class="com.example.service.HelloServiceImpl"/>

    <!-- 发布服务 -->
    <dubbo:service interface="com.example.service.HelloService" ref="helloService"/>

</beans>

### 4. 配置服务消费者:

使用Spring配置文件 `consumer.xml` 配置Dubbo服务消费者:

<!-- consumer.xml -->

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务消费者应用名称 -->
    <dubbo:application name="hello-consumer"/>

    <!-- 注册中心地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 引用服务接口 -->
    <dubbo:reference id="helloService" interface="com.example.service.HelloService"/>

</beans>

### 5. 编写消费者代码:

编写一个简单的Java类来测试Dubbo服务消费者:

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.service.HelloService;

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();

        HelloService helloService = (HelloService) context.getBean("helloService");
        String result = helloService.sayHello("Dubbo");
        System.out.println(result);
    }
}

### 6. 编写提供者代码:

编写一个简单的Java类来测试Dubbo服务提供者:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();

        System.out.println("Dubbo provider started");
        // 保持程序运行
        System.in.read();
    }
}

### 7. 运行示例:

1. 启动 ZooKeeper 服务。

2. 运行 `Provider` 类启动服务提供者。

3. 运行 `Consumer` 类启动服务消费者。

你将看到消费者在控制台输出:`Hello, Dubbo!`,表示Dubbo服务调用成功。

请确保在实际项目中将相关的Dubbo和ZooKeeper的依赖库引入到你的项目中,并进行合适的配置。此示例仅用于演示基本的Dubbo使用方式,实际项目中可能需要更详细的配置和处理。

 

 

 

 

end

posted @ 2008-08-04 16:16  siemens800  阅读(13)  评论(0)    收藏  举报