{"id":1727,"date":"2023-03-26T19:52:58","date_gmt":"2023-03-26T11:52:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1727"},"modified":"2023-04-23T21:34:30","modified_gmt":"2023-04-23T13:34:30","slug":"spring-cloud-synchronous-call-asynchronous-call-responsive-call","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/spring-cloud-synchronous-call-asynchronous-call-responsive-call\/","title":{"rendered":"Spring Cloud\u540c\u6b65\u8c03\u7528\u3001\u5f02\u6b65\u8c03\u7528\u3001\u54cd\u5e94\u5f0f\u8c03\u7528"},"content":{"rendered":"<h2>\u540c\u6b65\u8c03\u7528<\/h2>\n<pre><code class=\"language-java\">@Configuration\npublic class WebConfig {\n\n    @Bean\n    @LoadBalanced\n    RestTemplate restTemplate(){\n        return new RestTemplate();\n    }\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Service\npublic class HelloService {\n    @Autowired\n    RestTemplate restTemplate;\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public String hello() {\n        return restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n    }\n\n    public String helloFallBack() {\n        return &quot;error&quot;;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@RestController\npublic class HelloController {\n\n    @Autowired\n    HelloService helloService;\n\n    @RequestMapping(value = &quot;\/ribbon-hello&quot;, method = RequestMethod.GET)\n    public String hello() {\n      return helloService.hello();\n    }\n}<\/code><\/pre>\n<h2>\u5f02\u6b65\u8c03\u7528<\/h2>\n<p>\u4fee\u6539Service<\/p>\n<pre><code class=\"language-java\">@Service\npublic class HelloService {\n    @Autowired\n    RestTemplate restTemplate;\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public String hello() {\n        return restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n    }\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public Future&lt;String&gt; helloAsync() {\n        return new AsyncResult&lt;String&gt;() {\n            @Override\n            public String invoke() {\n                return restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n            }\n        };\n    }\n\n    public String helloFallBack() {\n        return &quot;error&quot;;\n    }\n}<\/code><\/pre>\n<p>\u4fee\u6539Controller<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class HelloController {\n\n    @Autowired\n    HelloService helloService;\n\n    @RequestMapping(value = &quot;\/ribbon-hello&quot;, method = RequestMethod.GET)\n    public String hello() {\n      return helloService.hello();\n    }\n\n    @RequestMapping(value = &quot;\/ribbon-hello-async&quot;, method = RequestMethod.GET)\n    public String helloAsync() throws ExecutionException, InterruptedException {\n        return helloService.helloAsync().get();\n    }\n}<\/code><\/pre>\n<h2>\u54cd\u5e94\u5f0f\u8c03\u7528<\/h2>\n<p>pom\u6587\u4ef6\u589e\u52a0\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/io.reactivex.rxjava2\/rxjava --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;io.reactivex.rxjava2&lt;\/groupId&gt;\n    &lt;artifactId&gt;rxjava&lt;\/artifactId&gt;\n    &lt;version&gt;2.2.19&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4fee\u6539Service<\/p>\n<pre><code class=\"language-java\">@Service\npublic class HelloService {\n    @Autowired\n    RestTemplate restTemplate;\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public String hello() {\n        return restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n    }\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public Future&lt;String&gt; helloAsync() {\n        return new AsyncResult&lt;String&gt;() {\n            @Override\n            public String invoke() {\n                return restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n            }\n        };\n    }\n\n    @HystrixCommand(fallbackMethod = &quot;helloFallBack&quot;)\n    public Observable&lt;String&gt; helloResponse() {\n        return Observable.create(new Observable.OnSubscribe&lt;String&gt;() {\n            @Override\n            public void call(Subscriber&lt;? super String&gt; subscriber) {\n                String body = restTemplate.getForEntity(&quot;http:\/\/HI-SERVICE\/hello?name=Joe&quot;, String.class).getBody();\n                subscriber.onNext(body);\n                subscriber.onCompleted();\n            }\n        });\n    }\n\n    public String helloFallBack() {\n        return &quot;error&quot;;\n    }\n}<\/code><\/pre>\n<p>\u4fee\u6539Controller<\/p>\n<pre><code class=\"language-java\">@RequestMapping(value = &quot;\/ribbon-response&quot;, method = RequestMethod.GET)\npublic String helloResponse() throws ExecutionException, InterruptedException {\n    helloService.helloConsumerResponse().subscribe(new Subscriber&lt;String&gt;() {\n        @Override\n        public void onCompleted() {\n            System.out.println(&quot;\u5b8c\u6210&quot;);\n        }\n\n        @Override\n        public void onError(Throwable e) {\n\n        }\n\n        @Override\n        public void onNext(String s) {\n            System.out.println(&quot;\u63a5\u6536\u5230\u4e86:&quot;+s);\n        }\n    });\n    return &quot;ok&quot;;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u6b65\u8c03\u7528 @Configuration public class WebConfig { @Bean @Loa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[433],"class_list":["post-1727","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-433"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1727"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1727\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}