{"id":97433,"date":"2020-05-14T20:52:00","date_gmt":"2020-05-14T12:52:00","guid":{"rendered":"http:\/\/4563.org\/?p=97433"},"modified":"2020-05-14T20:52:00","modified_gmt":"2020-05-14T12:52:00","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%8b-webflux-reactor-%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=97433","title":{"rendered":"\u8bf7\u6559\u4e00\u4e0b Webflux Reactor \u7f16\u7a0b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e0b Webflux Reactor \u7f16\u7a0b               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : binbinyouliiii <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>public People getPeople(String name) {     People people = getCache(name);     if (people != null) {         return people;     }     \/\/Mono&lt;People&gt; people = httpService.monoRequest(\"http:\/\/www.xxx.com\/path\", People.class);     people = httpService.syncRequest(\"http:\/\/www.xxx.com\/path\/info?name=\" + name, People.class);     setCache(msisdn, operatorInfo);     return people; }  private People getCache(String name) {     String str = redisTemplate.opsForValue().get(name);     if (str == null) {         return null;     }     return deserialize(str); }  private void setCache(String name, People people) {     String str = serialize(people);     redisTemplate.opsForValue().set(name, str); } <\/code><\/pre>\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u5e26\u6709\u7f13\u5b58\u7684\u83b7\u53d6\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u6211\u5c1d\u8bd5\u4f7f\u7528 Spring Webflux \u6765\u505a\uff0c\u4f46\u662f\u5199\u8d77\u6765\u53d1\u73b0\u4e3e\u6b65\u7ef4\u8270\uff0c\u5c24\u5176\u5bf9\u4e8e null \u503c\u7684\u6d41\u5f0f\u6761\u4ef6\u5224\u65ad\u6839\u672c\u60f3\u4e0d\u51fa\u6709\u4ec0\u4e48\u529e\u6cd5\u53bb\u5199 <\/p>\n<p> \u4ee5\u4e0b\u662f\u6211\u81ea\u5df1\u7684\u7406\u89e3\u5199\u7684\uff0c\u770b\u8d77\u6765\u975e\u5e38\u4e0d\u4f18\u96c5\uff0c\u800c\u4e14\u6ca1\u6709 null \u5224\u65ad\uff0c\u8bf7\u95ee\u54ea\u4f4d\u5927\u4f6c\u53ef\u4ee5\u7ed9\u4e2a\u4f18\u96c5\u7684\u6837\u4f8b\u5417\uff1f\u7f51\u4e0a\u7684\u4f8b\u5b50\u5c11\u5f97\u53ef\u601c<\/p>\n<pre><code>public Mono&lt;People&gt; getPeople(String name) {     Mono&lt;People&gt; peopleMono = httpService             .monoRequest(\"http:\/\/www.xxx.com\/path\/info?name=\" + name, People.class);     Mono&lt;Boolean&gt; setCacheMono = peopleMono.flatMap(people -&gt; setCache(name, people));     return peopleMono             .and(setCacheMono)             .then(peopleMono); }  private Mono&lt;People&gt; getnCache(String name) {     return reactiveRedisTemplate.opsForValue()             .get(name)             .map(this::deserialize); }  private Mono&lt;Boolean&gt; setCache(String name, People people) {     return reactiveRedisTemplate.opsForValue()             .set(name, serialize(people)); } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1623551\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jaylee4869 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Optional                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623552\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TtTtTtT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5176\u5b9e\u662f\u4e00\u6837\u7684\u3002\u3002<\/p>\n<p>People people = getCache(name);<br \/>if (people != null) {<br \/> return people;<br \/>}<\/p>\n<p>=&gt;<\/p>\n<p>getCahe(name).switchIfEmpty(&lt;after logic&gt;)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623553\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TtTtTtT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6362\u4e2a\u59ff\u52bf\u89e3\u91ca\u4e00\u4e0b\uff0c\u4f20\u7edf\u4ee3\u7801\u662f\u8fd9\u6837\u5b50\u7684\uff1a<br \/>statement1;<br \/>statement2;<br \/>statement3;<\/p>\n<p>\u90a3\u4e48\u5bf9\u4e8e Reactor\uff0c\u5176\u6839\u672c\u76ee\u6807\u5c31\u662f\u5728\u4e8e\u8ba9 statement1 \u548c statement2 \u4e4b\u95f4\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u4ee3\u7801\uff0c\u6765\u4fdd\u8bc1\u4ee3\u7801\u662f\u53ef\u8c03\u5ea6\u7684\uff0c\u8fdb\u800c\u8d77\u5230\u6240\u8c13\u7684 IO \u4f18\u5316\u3002<\/p>\n<p>\u90a3\u4e48\u9996\u5148\uff0c\u5c31\u9700\u8981\u6539\u9020\u6210\uff1a<\/p>\n<p>(statement1 as Mono).then(statement2 as Mono).then(statement3 as Mono)<\/p>\n<p>\u8fd9\u6837\u5c31\u80fd\u5728\u6bcf\u4e2a Mono \u4e4b\u95f4\u63d2\u5165\u4e00\u4e9b\u8c03\u5ea6\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u517c\u5bb9\u5176\u4ed6\u7684\u8bed\u6cd5\uff0c\u6bd4\u5982 return\uff0c\u6bd4\u5982 null\uff0cMono \u5c31\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6210\u5458\u65b9\u6cd5\u6765\u7b80\u5316\u8fd9\u4e2a then\uff0c\u8ba9\u5b83\u5199\u8d77\u6765\u66f4\u723d\u3002<br \/>\u6bd4\u5982 null \u5bf9\u5e94\u4e86 Mono.empty()\uff0creturn \u5bf9\u5e94\u4e86 Mono&lt;T&gt;\u91cc\u7684 T \u3002<\/p>\n<p>\u4ee5\u4e0a\uff0c\u4f60\u9700\u8981\u8003\u8651\u6bcf\u4e2a\u903b\u8f91\u5bf9\u5e94\u7684\u662f Mono \u7684\u54ea\u4e2a\u65b9\u6cd5\u3002<br \/>\u6bd4\u5982 if(x != null) { return x;} &lt;after statement&gt;\u5bf9\u5e94\u7684\u5c31\u662f switchIfEmpty(&lt;after statement&gt; as Mono);<br \/>\u6bd4\u5982 if(x != null) { return x;} else { return y; } \u5bf9\u5e94\u7684\u5c31\u662f defaultIfEmpty(y)\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623554\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u5eb7\u5eb7 js \u7684 promise \u53ef\u80fd\u4f1a\u6709\u70b9\u5e2e\u52a9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623555\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Mono \u4e2d Empty \u65f6\u7528 SwitchIfEmpty \u6216\u8005 defaultIfEmpty \u3002<\/p>\n<p>https:\/\/github.com\/hantsy\/spring-reactive-sample<\/p>\n<p>ReactiveStreams \u5b9e\u73b0\u592a\u591a\u4e86\uff0cReactor\uff0cRxjava2\/3, SmallRye Munity, Microprofile Reactive message Operators\uff0cHelidon \u4e5f\u5305\u542b\u4e86\u4e00\u4e2a\u5b9e\u73b0\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623556\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @azcvcza js \u4e16\u754c\u7684 Rxjs \u624d\u662f\u7528\u7684 ReactiveStreams \u6807\u51c6\u3002Promise \u7b97\u662f\u8001\u4e00\u4ee3\u6d41\u5f0f\u5904\u7406\uff0c\u6ca1\u6709\u8ba2\u9605\u6a21\u5f0f\uff0c\u901a\u77e5\u673a\u5236\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623557\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : binbinyouliiii <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @TtTtTtT #3 \u770b\u4e86\u4ee5\u4e0b switchIfEmpty \u770b\u8d77\u6765\u50cf\u662f\u4e3a\u4e86\u4fdd\u8bc1\u4e0d\u51fa\u9519\uff0c\u78b0\u5230 empty \u7684\u65f6\u5019\u4ee5\u8865\u507f\u7684\u5f62\u5f0f\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u7684\uff0c\u8d70\u5230 setCache \u7684\u65f6\u5019\uff0c\u56e0\u4e3a\u65e0\u6cd5\u5224\u65ad\u662f\u5426\u662f\u4ece\u7f13\u5b58\u62ff\u51fa\u6765\u7684\uff0c\u6240\u4ee5\u53ea\u80fd\u4e00\u80a1\u8111\u7ee7\u7eed\u6267\u884c setCahe \u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623558\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hantsy keyi                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623559\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TtTtTtT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @binbinyouliiii \u4e0d\u4e0d\u4e0d\uff0cswitchIfEmpty \u5e94\u8be5\u8fd9\u4e48\u7528:<br \/>getFromCache(key).switchIfEmpty(<br \/> getFromDb(key).flatMap(value -&gt; setCache(key,value))<br \/>)<br \/>\u5982\u679c\u8bbe\u7f6e\u7f13\u5b58\u662f\u7eaf\u5f02\u6b65\u7684\uff0c\u4e5f\u53ef\u4ee5\uff1a<br \/>getFromCache(key).switchIfEmpty(<br \/> getFromDb(key).doOnSuccess(value -&gt; setCache(key,value).subscribe())<br \/>)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1623560\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jinzhongyuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6d3b\u6349\u5927\u4f6c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e0b Webflux Reac&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/97433"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97433"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/97433\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97433"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}