{"id":381,"date":"2018-12-18T14:05:31","date_gmt":"2018-12-18T06:05:31","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=381"},"modified":"2022-05-17T09:44:19","modified_gmt":"2022-05-17T01:44:19","slug":"nats-client","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=381","title":{"rendered":"java\u4f7f\u7528nats-client\u8fdb\u884cpub\/sub\u53d1\u9001json\u7684\u5b9e\u4f8b"},"content":{"rendered":"<h1>\u9700\u6c42\u4ecb\u7ecd\uff1a<\/h1>\n<p>NATS\u662f\u4e00\u4e2a\u5f00\u6e90\u4e14\u9ad8\u6027\u80fd\u7684\u6d88\u606f\u7cfb\u7edf,\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f&#8221;\u4e00\u4e2a\u4e3a\u4e91\u670d\u52a1\u7684\u4e2d\u592e\u795e\u7ecf\u7cfb\u7edf&#8221;.\u5b83\u6bcf\u79d2\u949f\u53ef\u4ee5\u4f20\u9001\u767e\u4e07\u6761\u6d88\u606f,\u6240\u4ee5\u975e\u5e38\u9002\u5408\u7528\u6765\u8fde\u63a5\u5fae\u670d\u52a1\u548cIOT\u8bbe\u5907\u3002<br \/>NATS\u662f\u4e00\u4e2a\u53d1\u5e03\u8ba2\u9605\u65b9\u5f0f\u7684\u6d88\u606f\u7cfb\u7edf\u3002\u5728\u8fd9\u7c7b\u7cfb\u7edf\u4e2d,\u4e00\u4e2a\u6216\u591a\u4e2a\u6d88\u606f\u53d1\u5e03\u8005\u5c06\u7279\u5b9a\u7684\u4e3b\u9898\u53d1\u9001\u7ed9\u4e00\u4e2a\u6d88\u606f\u4e2d\u4ecb\u8005,\u7136\u540e\u6d88\u606f\u4e2d\u4ecb\u8005\u518d\u5c06\u6d88\u606f\u5206\u53d1\u7ed9\u4efb\u610f\u5ba2\u6237\u7aef(\u6216\u8005\u8fd9\u4e2a\u4e3b\u9898\u7684\u8ba2\u9605\u8005)\u3002\u6d88\u606f\u53d1\u5e03\u8005\u4e0d\u77e5\u9053\u4e5f\u4e0d\u5173\u5fc3\u6d88\u606f\u8ba2\u9605\u8005\u662f\u8c01,\u53cd\u4e4b\u4f9d\u7136\u3002\u7531\u4e8e\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u7cfb\u7edf\u5176\u5b83\u90e8\u5206\u7684\u60c5\u51b5\u4e0b\u589e\u52a0\u65b0\u7684\u6d88\u606f\u53d1\u5e03\u8005\u548c\u8ba2\u9605\u8005,\u8fd9\u6837\u7684\u67b6\u6784\u4f7f\u5f97\u7cfb\u7edf\u7684\u4f38\u7f29\u6027\u53d8\u5f97\u5f88\u597d\u5e76\u4e14\u53ef\u4ee5\u6bd4\u8f83\u5bb9\u6613\u5730\u589e\u52a0\u7cfb\u7edf\u7684\u5bb9\u91cf\u3002\u8fd9\u79cd\u7c7b\u578b\u7684\u7cfb\u7edf\u975e\u5e38\u9002\u5408\u7528\u6765\u76d1\u6d4b\u670d\u52a1\u5668\u548c\u7ec8\u7aef\u8bbe\u5907;\u7ec8\u7aef\u8bbe\u5907\u53ef\u4ee5\u53d1\u9001\u6d88\u606f,\u6211\u4eec\u53ef\u4ee5\u8ba2\u9605\u8fd9\u4e9b\u6d88\u606f,\u7136\u540e\u901a\u8fc7\u90ae\u4ef6\u6216\u8005\u5176\u4ed6\u7684\u65b9\u5f0f\u53d1\u9001\u6d88\u606f\u901a\u77e5\u3002<\/p>\n<p data-spm-anchor-id=\"5176.100033.1.i0.2f2762d0LSAyol\">\u4e0b\u9762\u6211\u4eec\u4f1a\u90e8\u7f72\u4e00\u4e2anats-server\u5728windows\u4e0a\uff0c\u7136\u540e\u4f7f\u7528java\u521b\u5efa\u4e24\u4e2anats\u5ba2\u6237\u7aef\uff0c\u4e00\u4e2a\u53d1\u9001pub\u8bf7\u6c42,\u4e00\u4e2a\u53d1\u9001sub\u8bf7\u6c42\uff0c\u68c0\u67e5sub\u7684\u8fd9\u4e2a\u5ba2\u6237\u7aef\u4e0a\u80fd\u5426\u6536\u5230\u4fe1\u606f\u3002<\/p>\n<h1 data-spm-anchor-id=\"5176.100033.1.i0.2f2762d0LSAyol\">\u670d\u52a1\u7aef\u90e8\u7f72<\/h1>\n<p>1.\u8fdb\u5165\u7f51\u7ad9<a href=\"https:\/\/www.nats.io\/download\/nats-io\/gnatsd\/\">https:\/\/www.nats.io\/download\/nats-io\/gnatsd\/<\/a><br \/>2.\u4e0b\u8f7dwindows\u7248\u672c\uff0c\u4e5f\u53ef\u4ee5\u7528docker-image\u7248\uff0c\u90fd\u5f88\u65b9\u4fbf\u3002<br \/>3.windows\u7248\u672c\uff0c\u4e0b\u8f7d\u4e4b\u540e\uff0c\u89e3\u538b\uff0c\u53cc\u51fbgnatsd.exe\u5373\u53ef\u8fd0\u884c<br \/>4.\u83b7\u5f97\u670d\u52a1\u5668\u5730\u5740\uff1anats:\/\/localhost:4222<\/p>\n<h1>nats\u4ee3\u7801<\/h1>\n<p>1.\u5148maven\u5b89\u88c5\u76f8\u5173\u4f9d\u8d56\uff0c\u4e3b\u8981\u662fnats\u7684\u548cjson\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u8981pub\u4e00\u4e2ajson\u8fc7\u53bb<\/p>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;dependency&gt;\n&lt;groupId&gt;io.nats&lt;\/groupId&gt;\n&lt;artifactId&gt;jnats&lt;\/artifactId&gt;\n&lt;version&gt;2.2.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n&lt;groupId&gt;com.google.code.gson&lt;\/groupId&gt;\n&lt;artifactId&gt;gson&lt;\/artifactId&gt;\n&lt;version&gt;2.8.5&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u00a0<\/p>\n<h1>nats\u5ba2\u6237\u7aef(sub\u8ba2\u9605)<\/h1>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package nats.lzwtest;\nimport java.nio.charset.StandardCharsets;\nimport java.util.concurrent.CountDownLatch;\nimport io.nats.client.Connection;\nimport io.nats.client.Dispatcher;\nimport io.nats.client.Nats;\npublic class SubscribeAsync {\n    public static void main(String[] args) {\n        try {\n            \/\/ [begin subscribe_async]\n        \tConnection nc = Nats.connect(\"nats:\/\/localhost:4222\");\n            \/\/ Use a latch to wait for a message to arrive\n            CountDownLatch latch = new CountDownLatch(10);\n            \/\/ Create a dispatcher and inline message handler\n            Dispatcher d = nc.createDispatcher((msg) -&gt; {\n                String str = new String(msg.getData(), StandardCharsets.UTF_8);\n                System.out.println(str);\n                latch.countDown();\n            });\n            \/\/ Subscribe\n            d.subscribe(\"lizhenwei\");\n            \/\/ Wait for a message to come in\n            latch.await();\n            \/\/ Close the connection\n            nc.close();\n            \/\/ [end subscribe_async]\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>\u00a0<\/p>\n<h1>nats\u5ba2\u6237\u7aef(pub\u53d1\u5e03)<\/h1>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package nats.lzwtest;\nimport java.nio.charset.StandardCharsets;\nimport java.time.Duration;\nimport com.google.gson.Gson;\nimport com.google.gson.GsonBuilder;\nimport io.nats.client.Connection;\nimport io.nats.client.Nats;\n\/\/ [begin publish_json]\nclass StockForJsonPub {\n    public String symbol;\n    public float price;\n}\npublic class PublishJSON {\n    public static void main(String[] args) {\n        try {\n        \tConnection nc = Nats.connect(\"nats:\/\/localhost:4222\");\n            \/\/ Create the data object\n            StockForJsonPub stk = new StockForJsonPub();\n            stk.symbol=\"GOOG\";\n            stk.price=1200;\n            \/\/ use Gson to encode the object to JSON\n            GsonBuilder builder = new GsonBuilder();\n            Gson gson = builder.create();\n            String json = gson.toJson(stk);\n            \/\/ Publish the message\n            nc.publish(\"lizhenwei\", json.getBytes(StandardCharsets.UTF_8));\n            \/\/ Make sure the message goes through before we close\n            nc.flush(Duration.ZERO);\n            nc.close();\n            System.out.println(\"pub success\");\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n\/\/ [end publish_json]<\/code><\/pre>\n<h1>\u8fd0\u884c\u7ed3\u679c<\/h1>\n<p>sub\u7aef\u6536\u5230\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">{\"symbol\":\"GOOG\",\"price\":1200.0}<\/code><\/pre>\n<p>\u00a0<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\u4ecb\u7ecd\uff1a NATS\u662f\u4e00\u4e2a\u5f00\u6e90\u4e14\u9ad8\u6027\u80fd\u7684\u6d88\u606f\u7cfb\u7edf,\u5b83\u5e38\u5e38\u88ab\u8ba4\u4e3a\u662f&#8221;\u4e00\u4e2a\u4e3a\u4e91\u670d\u52a1\u7684\u4e2d\u592e\u795e\u7ecf\u7cfb\u7edf&#038;#8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=381"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":929,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions\/929"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}