{"id":2270,"date":"2025-03-03T23:12:02","date_gmt":"2025-03-03T15:12:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2270"},"modified":"2025-03-03T23:12:02","modified_gmt":"2025-03-03T15:12:02","slug":"java-%e5%ba%8f%e5%88%97%e5%8c%96%e5%9c%a8-spring-%e5%92%8c-rpc-%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/03\/java-%e5%ba%8f%e5%88%97%e5%8c%96%e5%9c%a8-spring-%e5%92%8c-rpc-%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8\/","title":{"rendered":"Java \u5e8f\u5217\u5316\u5728 Spring \u548c RPC \u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"\n<p>\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u800c <strong>\u53cd\u5e8f\u5217\u5316<\/strong> \u5219\u662f\u5c06\u5b57\u8282\u6d41\u6062\u590d\u4e3a\u5bf9\u8c61\u3002Java \u63d0\u4f9b\u4e86 <strong>Serializable<\/strong> \u63a5\u53e3\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u6b64\u5916\u8fd8\u6709 <strong>JSON\u3001XML\u3001Protobuf<\/strong> \u7b49\u5e8f\u5217\u5316\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u5728 <strong>Spring<\/strong> \u548c <strong>RPC<\/strong>\uff08\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u4e2d\uff0c\u5e8f\u5217\u5316\u7528\u4e8e\u6570\u636e\u4f20\u8f93\u3001\u7f13\u5b58\u3001\u6301\u4e45\u5316\u7b49\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u5e94\u7528\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Spring \u4e2d\u7684\u5e8f\u5217\u5316\u5e94\u7528<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 Spring \u7f13\u5b58\uff08Spring Cache\uff09<\/strong><\/h3>\n\n\n\n<p>Spring Cache \u4f7f\u7528 <strong>\u5e8f\u5217\u5316<\/strong> \u673a\u5236\u5c06\u5bf9\u8c61\u5b58\u5165 Redis \u6216\u5176\u4ed6\u7f13\u5b58\u7cfb\u7edf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u9ed8\u8ba4\u4f7f\u7528 JDK \u5e8f\u5217\u5316<\/strong><\/h4>\n\n\n\n<p>Spring Boot \u9ed8\u8ba4\u4f7f\u7528 <strong>JDK \u5e8f\u5217\u5316<\/strong>\uff0c\u4f46\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u5360\u7528\u7a7a\u95f4\u5927\uff0c\u6027\u80fd\u8f83\u4f4e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.Serializable;\n\npublic class User implements Serializable {\n    private static final long serialVersionUID = 1L;\n    private String name;\n    private int age;\n\n    \/\/ \u6784\u9020\u65b9\u6cd5\u3001getter \u548c setter\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>@Cacheable(value = \"users\", key = \"#id\")\npublic User getUserById(Long id) {\n    return userRepository.findById(id).orElse(null);\n}<\/code><\/pre>\n\n\n\n<p>JDK <strong>\u9ed8\u8ba4\u7684\u5e8f\u5217\u5316\u65b9\u5f0f<\/strong> \u53ef\u80fd\u5bfc\u81f4\u7f13\u5b58\u5b58\u50a8\u7684\u5bf9\u8c61\u4e0d\u517c\u5bb9\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528 <strong>JSON<\/strong> \u6216 <strong>Protobuf<\/strong> \u4ee3\u66ff\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 Redis \u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<p>Spring Boot <strong>Redis<\/strong> \u9ed8\u8ba4\u4f7f\u7528 <strong>JdkSerializationRedisSerializer<\/strong> \u8fdb\u884c\u5e8f\u5217\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Bean\npublic RedisTemplate&lt;String, Object&gt; redisTemplate(RedisConnectionFactory factory) {\n    RedisTemplate&lt;String, Object&gt; template = new RedisTemplate&lt;&gt;();\n    template.setConnectionFactory(factory);\n\n    \/\/ \u8bbe\u7f6e JSON \u5e8f\u5217\u5316\n    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());\n    template.setKeySerializer(new StringRedisSerializer());\n\n    return template;\n}<\/code><\/pre>\n\n\n\n<p>\u6539\u7528 <strong>JSON<\/strong> \u8fdb\u884c\u5e8f\u5217\u5316\u540e\uff0cRedis \u5b58\u50a8\u7684\u6570\u636e\u66f4\u6613\u8bfb\u4e14\u8de8\u8bed\u8a00\u517c\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 Spring MVC \u4f20\u8f93 JSON<\/strong><\/h3>\n\n\n\n<p>Spring MVC \u4f9d\u8d56 <strong>Jackson<\/strong> \u8fdb\u884c JSON \u5e8f\u5217\u5316\uff0c\u9ed8\u8ba4\u652f\u6301 <code>@RestController<\/code> \u65b9\u6cd5\u7684 <strong>\u81ea\u52a8 JSON \u8f6c\u6362<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@RestController\npublic class UserController {\n    @GetMapping(\"\/user\")\n    public User getUser() {\n        return new User(\"Alice\", 25);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Spring \u81ea\u52a8\u4f7f\u7528 <strong>Jackson<\/strong> \u5e8f\u5217\u5316 <code>User<\/code> \u5bf9\u8c61\u4e3a JSON\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"name\": \"Alice\",\n    \"age\": 25\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. RPC\uff08\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff09\u4e2d\u7684\u5e8f\u5217\u5316<\/strong><\/h2>\n\n\n\n<p>RPC \u7528\u4e8e\u8de8\u8fdb\u7a0b\u901a\u4fe1\uff0c\u9700\u8981\u9ad8\u6548\u7684 <strong>\u5e8f\u5217\u5316<\/strong> \u548c <strong>\u53cd\u5e8f\u5217\u5316<\/strong> \u673a\u5236\u3002\u5e38\u89c1\u7684 RPC \u6846\u67b6\u5305\u62ec <strong>Dubbo\u3001gRPC\u3001Spring Cloud<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 Dubbo \u7684\u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<p>Dubbo \u652f\u6301\u591a\u79cd\u5e8f\u5217\u5316\u534f\u8bae\uff0c\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Hessian<\/strong>\uff08\u4e8c\u8fdb\u5236\uff0c\u6027\u80fd\u8f83\u597d\uff09<\/li>\n\n\n\n<li><strong>FastJSON<\/strong>\uff08\u6587\u672c\u683c\u5f0f\uff0c\u9002\u7528\u4e8e Web\uff09<\/li>\n\n\n\n<li><strong>Protobuf<\/strong>\uff08\u9ad8\u6548\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u8de8\u8bed\u8a00\u652f\u6301\uff09<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528 Hessian \u4f5c\u4e3a Dubbo \u5e8f\u5217\u5316\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dubbo:protocol name=\"dubbo\" serialization=\"hessian2\" port=\"20880\"\/&gt;<\/code><\/pre>\n\n\n\n<p>Dubbo \u670d\u52a1\u5668\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@DubboService\npublic class UserServiceImpl implements UserService {\n    public User getUser(Long id) {\n        return new User(\"Alice\", 25);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Dubbo \u5ba2\u6237\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@DubboReference\nprivate UserService userService;\n\npublic void test() {\n    User user = userService.getUser(1L);\n    System.out.println(user);\n}<\/code><\/pre>\n\n\n\n<p><strong>Hessian<\/strong> \u91c7\u7528\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u6bd4 Java \u5e8f\u5217\u5316\u66f4\u9ad8\u6548\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 gRPC\uff08Google RPC\uff09\u4e2d\u7684 Protobuf \u5e8f\u5217\u5316<\/strong><\/h3>\n\n\n\n<p><strong>gRPC<\/strong> \u91c7\u7528 <strong>Protocol Buffers\uff08Protobuf\uff09<\/strong> \u8fdb\u884c\u9ad8\u6548\u5e8f\u5217\u5316\uff0c\u8de8\u8bed\u8a00\u517c\u5bb9\u6027\u5f3a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>(1) \u5b9a\u4e49 Protobuf<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>syntax = \"proto3\";\npackage user;\n\nmessage User {\n  string name = 1;\n  int32 age = 2;\n}\n\nservice UserService {\n  rpc GetUser (UserRequest) returns (User);\n}\n\nmessage UserRequest {\n  int64 id = 1;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>(2) \u751f\u6210 Java \u4ee3\u7801<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>protoc --java_out=. user.proto<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>(3) \u4f7f\u7528 gRPC \u670d\u52a1<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>@GrpcService\npublic class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {\n    public void getUser(UserRequest request, StreamObserver&lt;User&gt; responseObserver) {\n        User user = User.newBuilder().setName(\"Alice\").setAge(25).build();\n        responseObserver.onNext(user);\n        responseObserver.onCompleted();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5ba2\u6237\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(channel);\nUser user = stub.getUser(UserRequest.newBuilder().setId(1L).build());\nSystem.out.println(user);<\/code><\/pre>\n\n\n\n<p><strong>Protobuf \u5177\u6709\u9ad8\u6548\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\uff0c\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1 RPC \u4f20\u8f93\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5e8f\u5217\u5316\u65b9\u5f0f\u5bf9\u6bd4<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e8f\u5217\u5316\u65b9\u5f0f<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>JDK \u5e8f\u5217\u5316<\/strong><\/td><td>Java \u5185\u90e8\u5bf9\u8c61\u5b58\u50a8<\/td><td>\u7b80\u5355\u3001\u5185\u7f6e\u652f\u6301<\/td><td>\u4f53\u79ef\u5927\u3001\u6027\u80fd\u4f4e<\/td><\/tr><tr><td><strong>JSON\uff08Jackson\uff09<\/strong><\/td><td>Web API\u3001Redis<\/td><td>\u53ef\u8bfb\u6027\u5f3a\u3001\u901a\u7528\u6027\u597d<\/td><td>\u89e3\u6790\u6027\u80fd\u6bd4\u4e8c\u8fdb\u5236\u5dee<\/td><\/tr><tr><td><strong>Hessian<\/strong><\/td><td>Dubbo\u3001RPC<\/td><td>\u9ad8\u6548\u3001\u652f\u6301\u5bf9\u8c61<\/td><td>Java \u4e13\u5c5e<\/td><\/tr><tr><td><strong>Protobuf<\/strong><\/td><td>gRPC\u3001\u9ad8\u6027\u80fd RPC<\/td><td>\u9ad8\u6548\u3001\u8de8\u8bed\u8a00<\/td><td>\u4e0d\u652f\u6301\u5bf9\u8c61\u7c7b\u578b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Spring<\/strong> \u4e3b\u8981\u4f7f\u7528 <strong>JSON<\/strong> \u6216 <strong>Redis<\/strong> \u8fdb\u884c\u5e8f\u5217\u5316\u3002<br>\u2705 <strong>Dubbo<\/strong> \u63a8\u8350\u4f7f\u7528 <strong>Hessian2<\/strong> \u4f5c\u4e3a\u5e8f\u5217\u5316\u65b9\u5f0f\u3002<br>\u2705 <strong>gRPC<\/strong> \u4f7f\u7528 <strong>Protobuf<\/strong> \u8fdb\u884c\u9ad8\u6548\u6570\u636e\u4f20\u8f93\u3002<br>\u2705 <strong>\u9009\u62e9\u5408\u9002\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\uff0c\u5e73\u8861\u6027\u80fd\u3001\u517c\u5bb9\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/strong><\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u800c \u53cd\u5e8f\u5217\u5316 \u5219\u662f\u5c06\u5b57\u8282\u6d41\u6062\u590d\u4e3a\u5bf9\u8c61\u3002Java \u63d0\u4f9b\u4e86 Serializab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2270","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2270"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2270\/revisions"}],"predecessor-version":[{"id":2271,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2270\/revisions\/2271"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}