使用RestTemplate和Feign進行微服務通訊

  • 在微服務架構中,咱們常常須要和其餘服務進行通訊實現數據交互,經常使用的方式有兩種
    • RPC
    • HTTP
  • SpirngCloud 中,默認是使用HTTP進行微服務間通訊,其中最經常使用的有兩種實現形式
    • RestTemplate
    • Feign

RestTempale

  • 其實在SpringWeb裏面,已經原生支持了 RestTemplate,只不過咱們通常使用的是把請求URL直接寫死,而不是經過服務名的形式來調用,可是在微服務架構中,由於有了註冊中心的存在,咱們的負載均衡能夠不須要使用第三方軟件或者硬件實現了,全部,咱們最佳的方式是通過服務名訪問,請求到那個實例,由 Ribbon 的負載均衡策略來替咱們決定。

第一種方式 (不推薦)

@GetMapping("/RibbonServer/1")
    public Map getRibbonServer1() {
        // 新建對象
        RestTemplate restTemplate = new RestTemplate();
        //請求目標地址
        String requestMsg = "方式一 GET 請求 RibbonServer";
        Map response = restTemplate.getForObject("http://localhost:9999/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class);
        log.info("response={}", response);
        return response;
    }
    
    @PostMapping("/RibbonServer/1")
    public Map postRibbonServer1() {
        // 新建對象
        RestTemplate restTemplate = new RestTemplate();
        //請求目標地址
        String requestMsg = "方式一 POST 請求 RibbonServer";
        Map<String, Object> requestParam = new HashMap<>(16);
        requestParam.put("requestMsg", requestMsg);
        Map response = restTemplate.postForObject("http://localhost:9999/RibbonServer/RibbonTest", requestParam, Map.class);
        log.info("response={}", response);
        return response;
    }
複製代碼

直接使用 RestTemplate , Url寫死java


第二種方式

@Autowired
    private LoadBalancerClient loadBalancerClient;
    
    @GetMapping("/RibbonServer/2")
    public Map getRibbonServer2() {
        // 獲取IP地址
        ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER");
        String requestMsg = "方式二 GET 請求 RibbonServer";
        String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest?requestMsg=" + requestMsg);
        RestTemplate restTemplate = new RestTemplate();
        Map response = restTemplate.getForObject(url, Map.class);
        log.info("response={}", response);
        return response;
    }
    
    @PostMapping("/RibbonServer/2")
    public Map postRibbonServer2() {
        // 獲取IP地址
        ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER");
        // 組裝URL
        String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest");
        RestTemplate restTemplate = new RestTemplate();
        String requestMsg = "方式二 POST 請求 RibbonServer";
        Map<String, Object> requestParam = new HashMap<>(16);
        requestParam.put("requestMsg", requestMsg);
        Map response = restTemplate.postForObject(url, requestParam, Map.class);
        log.info("response={}", response);
        return response;
    }
複製代碼

利用 LoadBalancerClient 經過應用名獲取 url,而後再使用 RestTemplate 請求git


第三種方式 (推薦)

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 //添加該註解,能夠直接經過服務名找到對應的IP地址
    public RestTemplate restTemplate() {

        return new RestTemplate();
    }
}
複製代碼
@Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/RibbonServer/3")
    public Map getRibbonServer3() {

        String requestMsg = "方式三 GET 請求 RibbonServer";
        Map response = restTemplate.getForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class);
        log.info("response={}", response);
        return response;
    }
    
    @PostMapping("/RibbonServer/3")
    public Map postRibbonServer3() {

        String requestMsg = "方式三 POST 請求 RibbonServer";
        Map response = restTemplate.postForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest", getRequestParam(requestMsg), Map.class);
        log.info("response={}", response);
        return response;
    }
複製代碼

利用 @LoadBalanced,可在 RestTemplate 裏面直接使用應用名字github


Feign

  • Feign 是 Netflix 開發的聲明式、模板化的 HTTP客戶端, Feign 能夠幫助咱們更快捷、優雅地調用 HTTP API

使用步驟

1.導入 OpenFeign 的包(SpringCloud2之後,Feign更名爲OpenFeign)web

<!--能夠不導入ribbon,由於feign裏面依賴了ribbon-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
複製代碼

2.啓動類添加 @EnableFeignClientsspring

@SpringBootApplication
@EnableFeignClients
public class SpringCloudRibbonClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudRibbonClientApplication.class, args);
    }
}
複製代碼

3.寫一個接口,用來調用指定的微服務(能夠由接口提供方寫好SDK,咱們直接調用)架構

@FeignClient(name = "ribbon-server") //調用的服務名
public interface RibbonServerClient {
    /** * 測試請求 RibbonServer * @Method: GET * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口請求參數 * @return java.util.Map 接口返回值,建議用對象接收 */
    @GetMapping("/RibbonServer/RibbonTest")
    Map getRibbonServer(@RequestParam("requestMsg") String requestMsg);

    /** * 測試請求 RibbonServer * @Method: POST * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口請求參數 * @return java.util.Map 接口返回值,建議用對象接收 */
    @PostMapping("/RibbonServer/RibbonTest")
    Map PostRibbonServer(@RequestBody Map requestMsg);
}
複製代碼

4.利用Spring的依賴注入,直接調用該接口方法,便可調用到 ribbon-server 服務的接口app

@RestController
@RequestMapping("/Feign")
@Slf4j
public class FeignController {
    @Autowired
    private RibbonServerClient ribbonServerClient;

    /** * Feign 訪問微服務測試 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */
    @GetMapping("/RibbonServer")
    public Map getRibbonServer() {
        String requestMsg = "Feign GET 請求 RibbonServer";
        Map response = ribbonServerClient.getRibbonServer(requestMsg);
        log.info("response={}", response);
        return response;
    }

    /** * Feign 訪問微服務測試 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */
    @PostMapping("/RibbonServer")
    public Map postRibbonServer1() {
        String requestMsg = "Feign Post 請求 RibbonServer";
        Map<String, Object> map = new HashMap<>(16);
        map.put("requestMsg", requestMsg);
        Map response = ribbonServerClient.PostRibbonServer(map);
        log.info("response={}", response);
        return response;
    }
}
複製代碼
相關文章
相關標籤/搜索