<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## Https vs RPC * Dubbo(RPC框架) * Spring Cloud ## SpringCloud中服務間兩種restful調用方式 需要開啟Eureka,并且把order和product兩個服務注冊進來 ![](https://img.kancloud.cn/97/a1/97a14219424e5142745758581967e38d_2266x878.png) ### RestTemplate #### server端 ``` package com.cfun.product.Controller; ........ @RestController public class ServerController { @GetMapping("/msg") public String msg(){ return "this is msg server"; } } ``` #### client調用 ``` package com.cfun.order.controller; ....... @RestController @Slf4j public class ClientController { @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private RestTemplate restTemplate; @GetMapping("/getProductMsg") public String getProductMsg() { //1.使用restTemplate RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject("http://localhost:8081/msg", String.class); //2.第二種方式 RestTemplate restTemplate = new RestTemplate(); ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT"); String url=String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()); String response = restTemplate.getForObject("http://localhost:8081/msg", String.class); //3.第三種方式.利用注解 String response =restTemplate.getForObject("http://PRODUCT/msg",String.class); log.info("response={}",response); return response; } } ``` #### RestTemplateConfig類 ~~~ package com.cfun.order.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } } ~~~ ### Feign 聲明式REST客戶端(偽RPC) 采用了基于接口的注解. #### 添加依賴 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` #### 主類添加注解 ~~~ @EnableFeignClients ~~~ #### 添加feign接口類 ~~~ package com.cfun.order.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "Product") public interface ProductClient { @GetMapping("/msg") String productMSg(); } ~~~ #### 使用 ~~~ package com.cfun.order.controller; import com.cfun.order.client.ProductClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @Slf4j public class ClientFeginController { @Autowired private ProductClient productClient; @GetMapping("/getProductMsg") public String getProductMsg() { String response =productClient.productMSg(); log.info("response={}",response); return response; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看