Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1de02fcce8 | |||
| 66c3b97650 | |||
| 186401fe22 | |||
| d5ee93f952 | |||
| 3aaa98b247 | |||
| e93531c52c | |||
| 45534e0b17 | |||
| d417c42553 | |||
| 0985a88859 | |||
| 617692cf6b | |||
| 0792e3448f | |||
| bd5bd59e5b | |||
| 8298582ef3 | |||
| 228d426364 | |||
| 93d2ebbbe1 | |||
| a6c5ba2594 | |||
| e92ddde554 | |||
| 6671163b02 | |||
| 22a9c9e329 | |||
| 97750b76b1 | |||
| a0549cc87f | |||
| 0cd1993c85 | |||
| 879b754cef | |||
| a95a124a46 | |||
| b193099ebc | |||
| 7ef0580560 | |||
| f7f0c049f2 | |||
| d9739146f3 | |||
| 3e83e6d7f8 | |||
| 2a794ea441 | |||
| 7602bdef62 | |||
| 72c4c91e5d | |||
| aa5a0db7f6 | |||
| 05629f46e0 | |||
| 94189f572c | |||
| 61b3f40793 | |||
| 10e01554cd | |||
| 9aa572b63e | |||
| fbe4824ea5 | |||
| 02dfa8fc58 | |||
| 13067122c7 | |||
| 25f039d237 | |||
| 34c6c5ba85 | |||
| e976c34ac9 | |||
| 1543c67894 | |||
| b84cd2d94b | |||
| 0a895131cd | |||
| a869f1dded | |||
| cd27777ee3 | |||
| 5feb925873 | |||
| be9c5d5294 | |||
| 5bb7b86a3d | |||
| eeb5532bff | |||
| 9e29cf14bb | |||
| c3819a69e3 | |||
| 10c4b99e54 | |||
| f9f812d1e3 | |||
| 7c920fa887 | |||
| f191fd47ae | |||
| 4ebc3891d9 | |||
| f1e652bcce |
Generated
-8
@@ -1,8 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/../../../../../../:\WorkDir\GitHub\cwiki-us-docs\cwikius-docs\.idea/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
-25
@@ -1,25 +0,0 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||
<value>
|
||||
<package name="java.util" alias="false" withSubpackages="false" />
|
||||
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
|
||||
<package name="io.ktor" alias="false" withSubpackages="true" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="PACKAGES_IMPORT_LAYOUT">
|
||||
<value>
|
||||
<package name="" alias="false" withSubpackages="true" />
|
||||
<package name="java" alias="false" withSubpackages="true" />
|
||||
<package name="javax" alias="false" withSubpackages="true" />
|
||||
<package name="kotlin" alias="false" withSubpackages="true" />
|
||||
<package name="" alias="true" withSubpackages="true" />
|
||||
</value>
|
||||
</option>
|
||||
</JetCodeStyleSettings>
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
||||
Generated
-5
@@ -1,5 +0,0 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
||||
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+9
-2
@@ -2,11 +2,18 @@
|
||||
- [文档介绍和快速链接](README.md)
|
||||
- [公众平台](CONTACT.md)
|
||||
|
||||
- Gradle
|
||||
- [Deploy](deploy.md)
|
||||
- Java
|
||||
- [语言基础](java/fundamentals/index.md)
|
||||
- [Gradle](gradle.md)
|
||||
|
||||
- 其他小工具
|
||||
- [JWT](jwt/README.md)
|
||||
- [MessagePack](message-pack/index.md)
|
||||
- [Protocol Buffers](protocol-buffers/index.md)
|
||||
|
||||
- 面试和算法
|
||||
- [面试问题和经验](interview/index.md)
|
||||
- [算法题](algorithm/index.md)
|
||||
|
||||
- [Awesome docsify](awesome.md)
|
||||
- [Changelog](changelog.md)
|
||||
@@ -0,0 +1,19 @@
|
||||
# 算法
|
||||
在应聘技术岗位的时候,可能都会有一个让绝大部分人都比较讨厌的算法环节。
|
||||
|
||||
也不知道是谁搞出这么无聊的东西,但没有办法现实总是残酷的,在抱怨的同时也只能在找工作之前复习下这些在实际工作中基本上不用的算法题了。
|
||||
|
||||
因为 MD 文档格式的限制,所以我们在这里只放一个索引并且维护这个索引,有关的具体内容,请通过索引访问我们的社区进行查看,我们也非常欢迎您参与讨论,
|
||||
您可以在社区注册后进行讨论。
|
||||
|
||||
## 算法问题
|
||||
在这里我将按照我遇到算法问题的先后顺序进行排列。很多算法题目都是题库上面的原题,你可以到题库上面去找找。
|
||||
|
||||
我这里只是结合我在面试的时候遇到的题目来进行解答和进行一些点评,就当时总结和消遣吧,如果你希望有什么问题一起讨论的话,欢迎 Fork 这个项目并且登录社区进行讨论。
|
||||
|
||||
最新的内容在最前面。
|
||||
|
||||
### 内容索引
|
||||
* [A “word-wrap” functionality(一个字符串包裹函数)](https://www.ossez.com/t/a-word-wrap-functionality/13452)
|
||||
* [Prime numbers from 1 to 100 (打印 100 以内的素数)](https://www.ossez.com/t/prime-numbers-from-1-to-100-100/13450)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# CWIKIUS 文档和手册
|
||||
|
||||
欢迎来到 CWIKIUS 文档和手册的空间
|
||||
|
||||
GitHub 上有关 CWIKIUS 的项目:[https://github.com/cwiki-us-docs/cwikius-docs](https://github.com/cwiki-us-docs/cwikius-docs)
|
||||
|
||||
|
||||
如果您有兴趣参与我们的小组和项目,请使用下面的联系方式和我们联系:
|
||||
|
||||
| 联系方式名称 | 联系方式 |
|
||||
|---|---|
|
||||
| 电子邮件 | [service@ossez.com](mailto:service@ossez.com) |
|
||||
| QQ 或微信 | 103899765 |
|
||||
| QQ 交流群 Spring | 15186112 |
|
||||
| 社区论坛 | [https://www.ossez.com/](https://www.ossez.com/) |
|
||||
|
||||
## 公众平台
|
||||
我们建议您通过社区论坛来和我们进行沟通,请关注我们公众平台上的账号
|
||||
|
||||
### 微信公众号
|
||||

|
||||
|
||||
### 头条号
|
||||
我们也在头条号上创建了我们的公众号,请扫描下面的 QR 关注我们的头条号。
|
||||
|
||||

|
||||
|
||||
|
||||
## CWIKIUS 文档和手册快速导航
|
||||
|
||||
在下面的表格中,我们列出了一些比较有用的 CWIKIUS 相关软件开发使用教程的导航,在我们文档的整理中,也参考了一些这些文档。
|
||||
|
||||
在这里对原作者表示感谢。
|
||||
|
||||
| 网站名称 | URL | NOTE |
|
||||
|---|---|---|
|
||||
| [github.io](https://cwiki-us-docs.github.io/cwikius-docs/#/) | [https://cwiki-us-docs.github.io/cwikius-docs/#/](https://cwiki-us-docs.github.io/cwikius-docs/#/) | 本手册的编译版本将会部署在这个链接上 |
|
||||
| WWW.CWIKIUS.CN | [http://www.cwikius.cn/](http://www.cwikius.cn/) | CWIKIUS.CN 一个有独立思考和温度的清新站 |
|
||||
|
||||
### 容器 docker
|
||||
| 网站名称 | URL | NOTE |
|
||||
|---|---|---|
|
||||
| Docker 手册中译在线版 | https://docker.ossez.com/ | 基于 Docker 容器英文手册的翻译版本|
|
||||
| Docker 手册中译 MD 在线版 | https://docker-docs.ossez.com/ | 因为 Docker 的官方手册进行了渲染,我们推荐你使用上面的链接,这个版本为 docsify 渲染版本,可能会丢格式 |
|
||||
Vendored
BIN
Binary file not shown.
-5
@@ -1,5 +0,0 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 9.9 KiB |
@@ -0,0 +1,28 @@
|
||||
# 面试问题和经验文档
|
||||
在应聘技术岗位的时候,通常会要求你进行一些算法和基础知识的考核。因计算机的知识比较复杂概念也非常多,这就给应聘者带来很大的困惑,应该怎么去做?
|
||||
|
||||
同时面试官很多时候也是突击某个问题,然后拿着这个问题来问应聘者。结果就导致了很多问题非常刁钻古怪,甚至有时候你根本就不可能在实际开发中遇到。
|
||||
|
||||
不管怎么样,这种面试方式至少目前来说是一种趋势,所以我们尽量收集一些技术岗的面试经验供大家参考。
|
||||
|
||||

|
||||
|
||||
|
||||
因为 MD 文档格式的限制,所以我们在这里只放一个索引并且维护这个索引,有关的具体内容,请通过索引访问我们的社区进行查看,我们也非常欢迎您参与讨论,
|
||||
您可以在社区注册后进行讨论。
|
||||
|
||||
## 面试问题和经验索引
|
||||
在这里我们希望按照年份进行一些分开,这样能够帮助大家尽量区分时间,同时我们的排序是按照倒序排列的。
|
||||
|
||||
最新的内容在最前面(按照时间倒序排序)。
|
||||
|
||||
### 2021
|
||||
* [2021 疫情期间美国公司技术岗的面试流程](https://www.ossez.com/t/topic/13463)
|
||||
* [从拒绝到拒绝](https://www.ossez.com/t/topic/13462)
|
||||
* [从一个工作到一个工作](https://www.ossez.com/t/topic/13461)
|
||||
* [北美亚特兰大一金融服务公司面试总结](https://www.ossez.com/t/topic/13453)
|
||||
* [北美一工作搜索引擎公司技术岗面经](https://www.ossez.com/t/topic/13451)
|
||||
* [一房地产数据服务初创公司的面经](https://www.ossez.com/t/topic/13441)
|
||||
|
||||
### 2019
|
||||
* [IT 技术岗位 2019 年北美求职流水账](https://www.ossez.com/t/it-2019/13433)
|
||||
@@ -0,0 +1,2 @@
|
||||
# 集合
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 基本概念
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 数据类型
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 异常处理
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 语言基础
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# I/O 操作
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 语言结构
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 方法和函数
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# 反射
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# JVM
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# 多线程
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# 新特性
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
@@ -0,0 +1,549 @@
|
||||
# MessagePack 文档
|
||||
|
||||
中文标题【MessagePack Java 0.6.X 快速开始指南】
|
||||
|
||||
MessagePack 的相关文档,本文是根据官方的内容和我们项目中的具体实践进行编辑的。
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
> :warning: 0.6.x 版本的 MessagePack 已经过期被淘汰了。如果你现在开始使用 MessagePack 话,请不要使用这个版本。
|
||||
|
||||
我们在这里保留 0.6.x 版本的内容主要用于参考用途。
|
||||
|
||||
最新的 MessagePack 版本请参考: https://github.com/msgpack/msgpack-java 中的项目源代码。
|
||||
|
||||
:information_source: MessagePack 中文文档请参考: http://docs.ossez.com/messagepack-docs/index.html
|
||||
|
||||
:information_source: MessagePack 测试和示例源代码: https://github.com/cwiki-us-demo/serialize-deserialize-demo-java
|
||||
|
||||
|
||||
这个指南提供了使用 msgpack-java 的快速指南。在开始的时候,我们将会介绍如何安装 msgpack-java,然后将会运行如何使用 msgpack 来对对象序列化/反序列化(serialize/deserizalize)对象。
|
||||
|
||||
## 安装
|
||||
你可以使用下面 2 种方法来安装 msgpack-java —— 从 maven 下载或者直接构建 jar 包。
|
||||
|
||||
### 从 Maven2 仓库中进行安装
|
||||
MessagePack 针对 Java 的使用已经发布到 Maven 的中央仓库中(Maven Central Repository)。你可以使用下面的参数来配置你项目的 pom.xml 文件。
|
||||
|
||||
````xml
|
||||
<dependency>
|
||||
<groupId>org.msgpack</groupId>
|
||||
<artifactId>msgpack</artifactId>
|
||||
<version>${msgpack.version}</version>
|
||||
</dependency>
|
||||
````
|
||||
|
||||
你需要将 ${msgpack.version} 替换为当前的 MessagePack 版本,有关可以使用的具体版本你可以访问 http://repo1.maven.org/maven2/org/msgpack/msgpack/ 中的版本。
|
||||
|
||||
请注意,在 0.6.x 版本中最新的版本只更新到 0.6.12。
|
||||
|
||||
### 从 git 仓库中进行安装
|
||||
你可以从代码仓库中获得最新的代码。
|
||||
|
||||
----
|
||||
```bash
|
||||
$ git clone git@github.com:msgpack/msgpack-java.git
|
||||
$ cd msgpack-java
|
||||
$ mvn package
|
||||
```
|
||||
|
||||
使用上面的代码进行编译后u,你将会在 msgpack-java/target 目录中得到 msgpack.jar 包。
|
||||
|
||||
同时你也需要 https://code.google.com/p/json-simple/ 和 https://github.com/jboss-javassist/javassist 来让 msgpack.jar 可以在项目中使用。否则你将会收到 NoClassDefFoundError 错误。
|
||||
|
||||
## 如何使用
|
||||
下面是如何使用的示例代码。
|
||||
|
||||
### 使用一个消息打包(message-packable)类
|
||||
使用注解 '_@Message_' 来让你可以序列化你自己类中对象的 public 字段。
|
||||
|
||||
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Object.java 中下载到本地后进行编译测试。
|
||||
|
||||
```java
|
||||
package com.insight.demo.msgpack;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.annotation.Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* MessagePack6Objects
|
||||
*
|
||||
* @author yhu
|
||||
*/
|
||||
public class MessagePack6Object {
|
||||
final Logger logger = LoggerFactory.getLogger(MessagePack6Object.class);
|
||||
|
||||
/**
|
||||
* MessageData Message Object
|
||||
*/
|
||||
@Message // Annotation
|
||||
public static class MessageData {
|
||||
// public fields are serialized.
|
||||
public String uuid;
|
||||
public String name;
|
||||
public double version;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test MessagePack6Objects
|
||||
*/
|
||||
@Test
|
||||
public void testMessagePack6Objects() {
|
||||
logger.debug("MessagePack6Objects for Objects");
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
|
||||
// INIT OBJ
|
||||
MessageData src = new MessageData();
|
||||
src.uuid = uuid;
|
||||
src.name = "MessagePack6";
|
||||
src.version = 0.6;
|
||||
|
||||
try {
|
||||
MessagePack msgPack = new MessagePack();
|
||||
|
||||
// Serialization
|
||||
logger.debug("------ Serialization ------");
|
||||
byte[] bytes = msgPack.write(src);
|
||||
logger.debug("Bytes Array Length: [{}]", bytes.length);
|
||||
|
||||
// Deserialization
|
||||
logger.debug("------ Deserialization ------");
|
||||
MessageData dst = msgPack.read(bytes, MessageData.class);
|
||||
logger.debug("Check Object for UUID: [{}]", dst.uuid);
|
||||
|
||||
assertEquals(uuid, dst.uuid);
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("MessagePack Serialization And Deserialization error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
如果你希望按照顺序序列化多个对象的话,你可以使用 '_Packer_' 和 '_Unpacker_' 多个对象。
|
||||
|
||||
这是因为 '_MessagePack.write(Object)_' 和 '_read(byte[])_' 实际上每次都调用创建了 '_Packer_' 和 '_Unpacker_' 对象。
|
||||
|
||||
为了使用 '_Packer_' 和 '_Unpacker_' 对象,请调用 '_createPacker(OutputStream)_' 和 '_createUnpacker(InputStream)_'。
|
||||
|
||||
本代码可以 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Objects.java 中查看。
|
||||
|
||||
|
||||
```java
|
||||
package com.insight.demo.msgpack;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.annotation.Message;
|
||||
import org.msgpack.packer.Packer;
|
||||
import org.msgpack.unpacker.Unpacker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* MessagePack6Objects
|
||||
*
|
||||
* @author yhu
|
||||
*/
|
||||
public class MessagePack6Objects {
|
||||
final Logger logger = LoggerFactory.getLogger(MessagePack6Objects.class);
|
||||
|
||||
/**
|
||||
* MessageData Message Objects
|
||||
*/
|
||||
@Message // Annotation
|
||||
public static class MessageData {
|
||||
// public fields are serialized.
|
||||
public String uuid;
|
||||
public String name;
|
||||
public double version;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test MessagePack6Objects
|
||||
*/
|
||||
@Test
|
||||
public void testMessagePack6Objects() {
|
||||
logger.debug("MessagePack6Objects for Objects");
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
|
||||
// INIT OBJ
|
||||
MessageData src1 = new MessageData();
|
||||
src1.uuid = uuid;
|
||||
src1.name = "MessagePack6-src1";
|
||||
src1.version = 0.6;
|
||||
|
||||
MessageData src2 = new MessageData();
|
||||
src2.uuid = uuid;
|
||||
src2.name = "MessagePack6-src2";
|
||||
src2.version = 10.6;
|
||||
|
||||
MessageData src3 = new MessageData();
|
||||
src3.uuid = uuid;
|
||||
src3.name = "MessagePack6-src3";
|
||||
src3.version = 1.6;
|
||||
|
||||
try {
|
||||
MessagePack msgPack = new MessagePack();
|
||||
|
||||
// Serialization
|
||||
logger.debug("------ Serialization ------");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Packer packer = msgPack.createPacker(out);
|
||||
packer.write(src1);
|
||||
packer.write(src2);
|
||||
packer.write(src3);
|
||||
|
||||
byte[] bytes = out.toByteArray();
|
||||
logger.debug("Bytes Array Length: [{}]", bytes.length);
|
||||
|
||||
// Deserialization
|
||||
logger.debug("------ Deserialization ------");
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
Unpacker unpacker = msgPack.createUnpacker(in);
|
||||
|
||||
MessageData dst1 = unpacker.read(MessageData.class);
|
||||
MessageData dst2 = unpacker.read(MessageData.class);
|
||||
MessageData dst3 = unpacker.read(MessageData.class);
|
||||
|
||||
logger.debug("Check Object for UUID: [{}]", dst1.uuid);
|
||||
|
||||
assertEquals(uuid, dst1.uuid);
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("MessagePack Serialization And Deserialization error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 多种类型变量的序列化和反序列化(serialization/deserialization)
|
||||
|
||||
类 '_Packer/Unpacker_' 允许序列化和反序列化多种类型的变量,如后续程序所示。这个类启用序列化和反序列化多种类型的变量和序列化主要类型变量以及包装类,'_String_' 对象,
|
||||
'_byte[]_' 对象,'_ByteBuffer 对象等的方法相似。
|
||||
|
||||
如上面提示的,你可以序列化和反序列化你自己的对象,前提是你自己的对象需要使用 '_@Message_' 注解。
|
||||
|
||||
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Types.java 中查看。
|
||||
|
||||
```java
|
||||
package com.insight.demo.msgpack;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.packer.Packer;
|
||||
import org.msgpack.unpacker.Unpacker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* MessagePack6Types
|
||||
*
|
||||
* @author yhu
|
||||
*/
|
||||
public class MessagePack6Types {
|
||||
final Logger logger = LoggerFactory.getLogger(MessagePack6Types.class);
|
||||
|
||||
|
||||
/**
|
||||
* Test MessagePack6Types
|
||||
*/
|
||||
@Test
|
||||
public void testMessagePack6Types() {
|
||||
logger.debug("testMessagePack6Types for Types");
|
||||
|
||||
MessagePack msgpack = new MessagePack();
|
||||
try {
|
||||
|
||||
//
|
||||
// Serialization
|
||||
//
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Packer packer = msgpack.createPacker(out);
|
||||
|
||||
// Serialize values of primitive types
|
||||
packer.write(true); // boolean value
|
||||
packer.write(10); // int value
|
||||
packer.write(10.5); // double value
|
||||
|
||||
// Serialize objects of primitive wrapper types
|
||||
packer.write(Boolean.TRUE);
|
||||
packer.write(new Integer(10));
|
||||
packer.write(new Double(10.5));
|
||||
|
||||
// Serialize various types of arrays
|
||||
packer.write(new int[]{1, 2, 3, 4});
|
||||
packer.write(new Double[]{10.5, 20.5});
|
||||
packer.write(new String[]{"msg", "pack", "for", "java"});
|
||||
packer.write(new byte[]{0x30, 0x31, 0x32}); // byte array
|
||||
|
||||
// Serialize various types of other reference values
|
||||
packer.write("MessagePack"); // String object
|
||||
packer.write(ByteBuffer.wrap(new byte[]{0x30, 0x31, 0x32})); // ByteBuffer object
|
||||
packer.write(BigInteger.ONE); // BigInteger object
|
||||
|
||||
//
|
||||
// Deserialization
|
||||
//
|
||||
byte[] bytes = out.toByteArray();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
Unpacker unpacker = msgpack.createUnpacker(in);
|
||||
|
||||
// to primitive values
|
||||
boolean b = unpacker.readBoolean(); // boolean value
|
||||
int i = unpacker.readInt(); // int value
|
||||
double d = unpacker.readDouble(); // double value
|
||||
|
||||
// to primitive wrapper value
|
||||
Boolean wb = unpacker.read(Boolean.class);
|
||||
Integer wi = unpacker.read(Integer.class);
|
||||
Double wd = unpacker.read(Double.class);
|
||||
|
||||
// to arrays
|
||||
int[] ia = unpacker.read(int[].class);
|
||||
Double[] da = unpacker.read(Double[].class);
|
||||
String[] sa = unpacker.read(String[].class);
|
||||
byte[] ba = unpacker.read(byte[].class);
|
||||
|
||||
// to String object, ByteBuffer object, BigInteger object, List object and Map object
|
||||
String ws = unpacker.read(String.class);
|
||||
ByteBuffer buf = unpacker.read(ByteBuffer.class);
|
||||
BigInteger bi = unpacker.read(BigInteger.class);
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("MessagePack Serialization And Deserialization error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
方法 '_Packer#write()_' 允许序列化多种类型的数据。
|
||||
|
||||
类 '_Unpacker_' 针对反序列化二进制数据为主要变量,提供了一个反序列化方法。例如,你希望将二进制数据反序列化为 '_boolean_' (或者 '_int_') 数据类型,你可以使用 '_Unpacker_' 中的 '_readBoolean_' (或者 '_readInt_') 方法。
|
||||
|
||||
'_Unpacker_' 同时也为参考变量提供了一个读取的方法。这个方法允许为一个参考变量从二进制数据中进行反序列化。参考变量的定义为你将类型指定为一个参数。
|
||||
例如,你希望反序列化二进制数据到 '_String_' (或者 '_byte[]_') 对象,你必须在调用 '_read(String.class)_' (或者 '_read(byte[].class)_') 方法的时候定义描述。
|
||||
|
||||
## List, Map 对象的序列化和反序列化(serialization/deserialization)
|
||||
为了序列化原生的容器对象例如 '_List_' 和 '_Map_' 对象,你必须使用 '_Template_'。
|
||||
|
||||
'_Template_' 对象是 serializer 和 deserializer 的配对。例如,为了序列化一个 '_List_' 对象,在 '_List_' 对象中 '_Integer_' 对象为元素,你可以使用下面的方法来创建一个模板对象(Template object)。
|
||||
|
||||
```java
|
||||
Template listTmpl = Templates.tList(Templates.TInteger);
|
||||
```
|
||||
|
||||
类 '_tList_', '_TInteger_' 是静态方法,字段为 '_Templates_'。
|
||||
|
||||
一个 '_List 和 '_Map_' 对象的用例如下显示:
|
||||
|
||||
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Template.java 中查看。
|
||||
|
||||
```java
|
||||
package com.insight.demo.msgpack;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.packer.Packer;
|
||||
import org.msgpack.template.Template;
|
||||
import org.msgpack.unpacker.Unpacker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.msgpack.template.Templates.*;
|
||||
|
||||
/**
|
||||
* MessagePack6Template
|
||||
*
|
||||
* @author yhu
|
||||
*/
|
||||
public class MessagePack6Template {
|
||||
final Logger logger = LoggerFactory.getLogger(MessagePack6Template.class);
|
||||
|
||||
|
||||
/**
|
||||
* Test MessagePack6Template
|
||||
*/
|
||||
@Test
|
||||
public void testMessagePack6Template() {
|
||||
logger.debug("MessagePack6Template for Template");
|
||||
|
||||
MessagePack msgpack = new MessagePack();
|
||||
try {
|
||||
|
||||
// Create templates for serializing/deserializing List and Map objects
|
||||
Template<List<String>> listTmpl = tList(TString);
|
||||
Template<Map<String, String>> mapTmpl = tMap(TString, TString);
|
||||
|
||||
//
|
||||
// Serialization
|
||||
//
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Packer packer = msgpack.createPacker(out);
|
||||
|
||||
// Serialize List object
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("msgpack");
|
||||
list.add("for");
|
||||
list.add("java");
|
||||
packer.write(list); // List object
|
||||
|
||||
// Serialize Map object
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("sadayuki", "furuhashi");
|
||||
map.put("muga", "nishizawa");
|
||||
packer.write(map); // Map object
|
||||
|
||||
//
|
||||
// Deserialization
|
||||
//
|
||||
|
||||
byte[] bytes = out.toByteArray();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
Unpacker unpacker = msgpack.createUnpacker(in);
|
||||
|
||||
// to List object
|
||||
List<String> dstList = unpacker.read(listTmpl);
|
||||
|
||||
// to Map object
|
||||
Map<String, String> dstMap = unpacker.read(mapTmpl);
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("MessagePack Serialization And Deserialization error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 不使用注解(annotations)来序列化
|
||||
如果你不能添加 @Message 到你的定义对象中但是你还是希望进行序列化。你可以使用 register 方法来在类中启用序列化对象。
|
||||
|
||||
如下的代码所示:
|
||||
|
||||
```java
|
||||
MessagePack msgpack = new MessagePack();
|
||||
msgpack.register(MyMessage2.class);
|
||||
```
|
||||
|
||||
例如,如果 MyMessage2 类被包含到了外部的库中了。你没有办法比较容易的编辑源代码,添加 '_@Message_' 到源代码中。
|
||||
|
||||
register 方法能够允许为 MyMessage2 自动创建一个 serializer 和 deserializer 对。
|
||||
|
||||
你可以在执行方面后序列化对象 MyMessage2。
|
||||
|
||||
### 可选字段
|
||||
你可添加一个新的字段来保持可用性。在新字段中使用 '_@Optional_' 注解。
|
||||
|
||||
```java
|
||||
@Message
|
||||
public static class MyMessage {
|
||||
public String name;
|
||||
public double version;
|
||||
|
||||
// new field
|
||||
@Optional
|
||||
public int flag = 0;
|
||||
}
|
||||
```
|
||||
|
||||
如果你尝试反序列化老版本数据的话,可选字段将会被忽略。
|
||||
|
||||
### 动态类型
|
||||
我们知道 Java 是一个静态类型的语言。通过输入 '_Value_' MessagePack能够实现动态的特性。
|
||||
|
||||
'_Value_' 有方法来检查自己的类型('_isIntegerType()_', '_isArrayType()_', 等...),同时也转换为自己的类型 ('_asStringValue()_', '_convert(Template)_')。
|
||||
|
||||
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java 中查看。
|
||||
|
||||
```java
|
||||
package com.insight.demo.msgpack;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePack;
|
||||
import org.msgpack.type.Value;
|
||||
import org.msgpack.unpacker.Converter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.msgpack.template.Templates.TString;
|
||||
import static org.msgpack.template.Templates.tList;
|
||||
|
||||
/**
|
||||
* MessagePack6Objects
|
||||
*
|
||||
* @author yhu
|
||||
*/
|
||||
public class MessagePack6DynamicTyping {
|
||||
final Logger logger = LoggerFactory.getLogger(MessagePack6DynamicTyping.class);
|
||||
|
||||
|
||||
/**
|
||||
* Test MessagePack6Objects
|
||||
*/
|
||||
@Test
|
||||
public void MessagePack6DynamicTyping() {
|
||||
logger.debug("MessagePack6Objects for Objects");
|
||||
|
||||
// Create serialize objects.
|
||||
List<String> src = new ArrayList<String>();
|
||||
src.add("msgpack");
|
||||
src.add("kumofs");
|
||||
src.add("viver");
|
||||
|
||||
MessagePack msgpack = new MessagePack();
|
||||
|
||||
try {
|
||||
|
||||
// Serialize
|
||||
byte[] raw = msgpack.write(src);
|
||||
|
||||
// Deserialize directly using a template
|
||||
List<String> dst1 = msgpack.read(raw, tList(TString));
|
||||
|
||||
// Or, Deserialze to Value then convert type.
|
||||
Value dynamic = msgpack.read(raw);
|
||||
List<String> dst2 = new Converter(dynamic).read(tList(TString));
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("MessagePack Serialization And Deserialization error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
@@ -0,0 +1,221 @@
|
||||
# Protocol Buffers 文档
|
||||
|
||||
Protocol Buffers 相关文档的内容。
|
||||
|
||||

|
||||
|
||||
## Protocol Buffers 简介
|
||||
如果你在互联网上搜索的话,你可能会看到下面的说明文字。
|
||||
|
||||
Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。
|
||||
|
||||
他们用于 RPC 系统和持续数据存储系统。
|
||||
|
||||
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。
|
||||
|
||||
它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。
|
||||
|
||||
目前提供了 C++、Java、Python 三种语言的 API。
|
||||
|
||||
|
||||
我们是在实际的数据处理的时候,需要使用 JSON 来序列化数据,但是又考虑到 JSON 在传输的时候数据过大,同时还有很多并不需要的垃圾文件。
|
||||
我们才考虑到了 Protocol Buffer 提供的功能。
|
||||
|
||||

|
||||
|
||||
|
||||
# Protocol Buffers 开发者指南
|
||||
|
||||
欢迎来到 protocol buffers 的开发者指南。protocol buffers 是一个语言中立,平台中立针对通讯协议,数据存储和其他领域中对结构化数据进行序列化的扩展方法。
|
||||
|
||||
本文档主要针对的是 Java,C++ 或 Python 的开发人员希望在开发的应用程序中使用 Protocol Buffers。这个有关 Protocol Buffers 摘要性的介绍将会告诉你如何开始使用 Protocol Buffers。
|
||||
如果你希望更加深入的了解有关 Protocol Buffers 的内容,你可以进入 tutorials 或者 protocol buffer encoding 页面来详细了解。
|
||||
|
||||
有关 API 的参考文档,请参考页面:reference documentation 这里提供了所有这 3 种语言的参考,同时也针对 .proto language 和 style 提供相关的指南。
|
||||
|
||||
## 什么是 Protocol Buffers
|
||||
Protocol buffers 是对结构化数据序列化的一个灵活,高效,自动化工具 —— 你可以将 Protocol buffers 想象成 XML,但是体积更小,更快也更加简单。
|
||||
|
||||
你可以自己定义你的结构化数据,然后你可以使用特定的代码生成工具来非常容易对你的结构化数据进行读取和写入。这些数据的读取和写入可以是一系列的数据流和使用不同的计算机程序语言。
|
||||
你甚至可以在不对已经部署的程序进行破坏的情况下更新你的数据结构。
|
||||
|
||||
## Protocol Buffers 是如何进行工作的
|
||||
你需要制定你希望如何将你的数据进行序列化。你是通过 proto 文件来定义你的消息结构化数据的。
|
||||
|
||||
每一 protocol buffer message 是一个小的信息记录逻辑,这个消息中包含有一系列的名字,变量对照序列。
|
||||
|
||||
下面是一些基本的.proto 文件,这些文件中定义了一个消息,这个消息包含有一个 person 信息:
|
||||
|
||||
```
|
||||
message Person {
|
||||
required string name = 1;
|
||||
required int32 id = 2;
|
||||
optional string email = 3;
|
||||
|
||||
enum PhoneType {
|
||||
MOBILE = 0;
|
||||
HOME = 1;
|
||||
WORK = 2;
|
||||
}
|
||||
|
||||
message PhoneNumber {
|
||||
required string number = 1;
|
||||
optional PhoneType type = 2 [default = HOME];
|
||||
}
|
||||
|
||||
repeated PhoneNumber phone = 4;
|
||||
}
|
||||
```
|
||||
|
||||
通过上面你可以看到这个消息的格式非常简单—— 每一个消息类型都有一个或者多个唯一进行编号的字段,每一个字段包含有一个名字和变量类型。
|
||||
|
||||
变量可以为数字(整形或者浮点型)(numbers),布尔类型(booleans),字符串(strings),原生二进制(raw bytes)甚至其他的 protocol buffer 消息类型,能够允许你分级的结构化你的数据。
|
||||
|
||||
你可以将字段指定为可选字段(optional fields),必须字段(required fields)和重复字段(repeated fields)。你可以从下面的 Protocol Buffer Language Guide 页面中找到更多有关 .proto 的定义。
|
||||
|
||||
一旦你成功定义了你的消息,你可以针对你使用的语言使用你定义的 .proto 来运行 protocol buffer 编译器(protocol buffer compiler)来生成数据访问类。
|
||||
|
||||
针对每一个字段,在数据访问类中提供了简单的访问方法(例如 name() 和 set_name())和序列化到原生 2 进制数据和从原生 2 进制数据反序列化的方法。
|
||||
|
||||
针对上面的定义,如果你现在使用的是 C++ 语言的话,当你把消息定义进行编译后,你将会得到一个称为 Person 的类。对数据进行序列化和从序列化的数据中(protocol buffer 消息)重新获得 Person 数据。
|
||||
|
||||
然后你可以写一些类似 Person person; 的代码。
|
||||
|
||||
```cpp
|
||||
Person person;
|
||||
person.set_name("John Doe");
|
||||
person.set_id(1234);
|
||||
person.set_email("jdoe@example.com");
|
||||
fstream output("myfile", ios::out | ios::binary);
|
||||
person.SerializeToOstream(&output);
|
||||
```
|
||||
|
||||
随后,你可以对消息进行读取:
|
||||
|
||||
```cpp
|
||||
fstream input("myfile", ios::in | ios::binary);
|
||||
Person person;
|
||||
person.ParseFromIstream(&input);
|
||||
cout << "Name: " << person.name() << endl;
|
||||
cout << "E-mail: " << person.email() << endl;
|
||||
```
|
||||
|
||||
你可以向你的消息中添加新的字段而不会损坏老的消息。这是因为在老的消息处理中,针对新的字段是完全忽略掉的。因此,如果你在你的通讯协议中使用 protocol buffers 为数据结构的话,
|
||||
你可以对你的协议和消息进行扩展而不需要担心老的代码没有办法编译通过,或者损坏老的代码。
|
||||
|
||||
你可以访问 API Reference section 页面中的内容来了解完整 protocol buffer 代码的生成和使用。
|
||||
|
||||
你也可以在 Protocol Buffer Encoding 页面中了解更多protocol buffer 消息是如何进行编码的。
|
||||
|
||||
## 为什么不使用 XML
|
||||
针对 XML 来说 Protocol Buffers 具有更多的优势来对序列化结构数据。
|
||||
|
||||
* 更加简单
|
||||
* 小于 XML 3 到 10 倍
|
||||
* 快于 XML 20 到 100 倍
|
||||
* 松耦合
|
||||
* 使用程序工具来创建数据访问类,使数访问类更加简单
|
||||
* 假设,你需要讲 person 这个数据进行定义,在 XML 你需要使用:
|
||||
|
||||
```xml
|
||||
<person>
|
||||
<name>John Doe</name>
|
||||
<email>jdoe@example.com</email>
|
||||
</person>
|
||||
```
|
||||
来进行定义。
|
||||
|
||||
在 Protocol Buffers 中针对上面的消息文本化(text format)后显示为:
|
||||
|
||||
```
|
||||
# Textual representation of a protocol buffer.
|
||||
# This is *not* the binary format used on the wire.
|
||||
person {
|
||||
name: "John Doe"
|
||||
email: "jdoe@example.com"
|
||||
}
|
||||
```
|
||||
|
||||
当上面的消息被编码为 Protocol Buffer 二进制格式(binary format)上面的文字可能小于 28 bytes,并且可能需要 100-200 纳秒(nanoseconds)来进行处理。
|
||||
|
||||
我们将上面转换为可以人为读取的目的主要是为进行调试和编辑。
|
||||
|
||||
如果你使用 XML 的话,上面的信息至少需要 69 bytes (你需要删除所有的空格),同时你需要 5,000-10,000 纳秒(nanoseconds)来进行处理。
|
||||
|
||||
同时,对 protocol buffer 进行操作也是非常容易的:
|
||||
|
||||
```cpp
|
||||
cout << "Name: " << person.name() << endl;
|
||||
cout << "E-mail: " << person.email() << endl;
|
||||
```
|
||||
|
||||
如果使用的是 XML 的话,你需要进行下面的操作:
|
||||
|
||||
```cpp
|
||||
cout << "Name: "
|
||||
<< person.getElementsByTagName("name")->item(0)->innerText()
|
||||
<< endl;
|
||||
cout << "E-mail: "
|
||||
<< person.getElementsByTagName("email")->item(0)->innerText()
|
||||
<< endl;
|
||||
```
|
||||
|
||||
但是,protocol buffers 并不是任何时候都会比 XML 好。例如,针对基于文本的标记语言(例如,XML),protocol buffers 就不是一个很好的选项,
|
||||
因为你不能使用 protocol buffer 更好的在文档中进行交换。更主要的是 HTML 是人类可以阅读和编辑的。protocol buffer 也不是不可以人为的读取,但是针对原生的 protocol buffer 格式是没有办法人为进行读取和编辑的。
|
||||
|
||||
XML 与 HTML 一样,在某种程度上是一种自我描述数据。protocol buffer 只针对你在 .proto 文件中描述的内容进行表达。
|
||||
|
||||
## 看起来像一个解决方案,我应该如何开始呢
|
||||
Download the package – 这包中含有针对 Java, Python, 和 C++ protocol buffer 编译器源代码,和你需要进行 I/O 和测试的类。希望对你的编译器进行编译和构建,请参考代码中的 README 文件。
|
||||
|
||||
一旦你完成了所有的设置,请参考 tutorial 页面中的内容来选择你需要的语言——这个能够帮助你使用 protocol buffer 创建一个简单的应用程序。
|
||||
|
||||
## 介绍 proto3
|
||||
在我们最新的 version 3 发行版 中推出了新的语言版本 —— Protocol Buffers language version 3(另称 proto3),在这个版本中针对我们已经存在的语言版本(proto2)使用了一些新的特性。
|
||||
|
||||
Proto3 简化了 protocol buffer 语言,使其更加容易使用并且能够支持更多的语言:我们当前发行的 proto3 能够让你创建 Java, C++, Python, Java Lite, Ruby, JavaScript, Objective-C, and C#。
|
||||
|
||||
另外你也可以通过使用 Go protoc 插件来用 proto3 创建 Go 代码,这个插件你可以到 golang/protobuf Github 中下载到。更多的语言还在逐步进行支持中。
|
||||
|
||||
请注意,这 2 个版本的 API 并不是完全兼容的。为了照顾还在使用老版本的用户,我们将会在新的 protocol buffers 发行中同时支持老的版本。
|
||||
|
||||
你可以在下面的发行日志(release notes)查看 2 个版本的主要不同。有关 proto3 的句法,请参考 Proto3 Language Guide 中的内容,针对 proto3 的完整文档还没有编写完成,将会随后推出。
|
||||
|
||||
看起来 proto2 和 proto3 可能会产生一些混淆,这是因为原始的开源 protocol buffers 实际上是 Google 内部语言的第二个版本,同时我们的开源版本也是从 v2.0.0 开始的。
|
||||
|
||||
简单来说就是 proto 最开始的版本是 Google 内部使用的,在 proto 第二个版本的时候,Google 决定进行开源了,所以开源的 proto 是从 proto2 开始的。
|
||||
|
||||
## 一个简短的历史
|
||||
Protocol buffers 最开始是在 Google 内部进行开发的,用于处理在索引服务器上请求/响应(request/response)的协议。
|
||||
|
||||
在 Protocol buffers 之前,针对请求和响应,使用的是 marshalling/unmarshalling,这个能够支持一系列的协议。但是结果看起来却是非常的难看,例如:
|
||||
|
||||
```
|
||||
if (version == 3) {
|
||||
...
|
||||
} else if (version > 4) {
|
||||
if (version == 5) {
|
||||
...
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
明确格式化的的协议也使新版本的协议更加难以推出,这是因为开发者必须能够了解老协议在服务器之间是如何进行处理的,同时也需要了解新的协议。只有对新老协议都有所了解后才能逐步使用新的协议替换老的协议。
|
||||
|
||||
Protocol buffers 被用来设计解决上面的很多问题:
|
||||
|
||||
* 新的字段比较能够容易的进行定义,中级服务器不需要对数据进行检查,直接对数据进行处理,同时也可以直接传输数据而不需要了解数据是如何进行定义的。
|
||||
* 格式使用自描述,能够更加容易支持更多的语言(C++,Java 等)。
|
||||
|
||||
但是,用户还是需要手动书写他们自己的处理diam。
|
||||
|
||||
作为系统的进化来说,它获得了许多其他的特性和用途:
|
||||
|
||||
* 自动生成序列化和反序列化代码而避免手动书写这些代码。
|
||||
* 除了开始使用短期RPC(远程过程调用)请求,人们开始使用 protocol buffers 作为高效的自描述结构化数据格式(主要针对数据短期存在,例如在 Bigtable)。
|
||||
* 服务器RPC接口开始被声明为协议文件的一部分,协议编译器生成根类,用户可以通过服务器接口的实现和重载它们。
|
||||
|
||||
Protocol buffers 在 Google 中成为针对数据的通用语言—— 随着时间的流逝,在 Google 内部已经有超过 348,952 .proto 文件被定义。这些被用在 RPC 系统和存储系统中存储数据。
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# CWIKIUS SPRING 文档和项目快速导航
|
||||
|
||||
欢迎来到 CWIKIUS SPRING 文档和手册的空间
|
||||
|
||||
Spring 有关的文档和项目 Confluence Wiki,请访问:[https://www.cwiki.us/display/CWIKIUSDOCS/Spring+Docs](https://www.cwiki.us/display/CWIKIUSDOCS/Spring+Docs)
|
||||
|
||||
|
||||
如果您有兴趣参与我们的小组和项目,请使用下面的联系方式和我们联系:
|
||||
|
||||
| 联系方式名称 | 联系方式 |
|
||||
|---|---|
|
||||
| 电子邮件 | [service@ossez.com](mailto:service@ossez.com) |
|
||||
| QQ 或微信 | 103899765 |
|
||||
| QQ 交流群 Spring | 15186112 |
|
||||
| 社区论坛 | [https://www.ossez.com/](https://www.ossez.com/) |
|
||||
|
||||
## Spring Data JPA
|
||||
|
||||
在下面的表格中,我们列出了一些比较有用的 CWIKIUS 相关软件开发使用教程的导航,在我们文档的整理中,也参考了一些这些文档。
|
||||
|
||||
在这里对原作者表示感谢。
|
||||
|
||||
| 网站名称 | URL | NOTE |
|
||||
|---|---|---|
|
||||
| 源代码 | [https://github.com/cwiki-us-spring/spring-data-jpa](https://github.com/cwiki-us-spring/spring-data-jpa) | 下面编译结果使用的源代码 |
|
||||
| 参考 | [https://cdn.ossez.com/docs/spring-docs/spring-data-jpa/reference/html/index.html](https://cdn.ossez.com/docs/spring-docs/spring-data-jpa/reference/html/index.html) | 从官方代码库中 Fork 下来的代码后进行中文化编译后的的文档 |
|
||||
| API | [https://cdn.ossez.com/docs/spring-docs/spring-data-jpa/apidocs/index.html](https://cdn.ossez.com/docs/spring-docs/spring-data-jpa/apidocs/index.html) | 官方代码库中 Fork 下面的代码后编译的 API 参考 |
|
||||
|
||||
Reference in New Issue
Block a user