Spring Cloud 學(xué)習(xí)之路 ——RestTemplate

RestTemplate

在認(rèn)識(shí)RestTemplate之前我一直是使用httpclient工具類去訪問(wèn)資源。
某次群聊中,有位同學(xué)分享了一道面試題:
dubbo webservice hessian resttemplate它們之間啥關(guān)系?
當(dāng)時(shí)我就是一臉懵逼,因?yàn)槲疫@四樣都沒(méi)接觸過(guò),本著好學(xué)的心態(tài),我積極尋求度娘的幫助得出以下結(jié)論:

webservice就是個(gè)籠統(tǒng)概念,只要是網(wǎng)絡(luò)之間的數(shù)據(jù)傳輸和調(diào)用都叫webservice
dubbo是解決遠(yuǎn)程方法調(diào)用的問(wèn)題的一種工具(RPC)
hessian是一種遠(yuǎn)程過(guò)程調(diào)用的協(xié)議,類似的比如說(shuō)soap之類的,都可適用于dubbo之類的工具
resttemplate只是spring封裝的一個(gè)REST風(fēng)格的請(qǐng)求工具而已,和httpclient沒(méi)有本質(zhì)區(qū)別

下面就來(lái)看看RestTemplate是在實(shí)際開(kāi)發(fā)中如何應(yīng)用吧!
步驟一:
在Server服務(wù)中寫一個(gè)接口

package com.imooc.order.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author penn
 * @Date 2019/1/5 11:50
 **/
@RestController
public class ServerController {

    @PostMapping("/msg")
    public String msg(){
        return "msg";
    }
}

步驟二:
在需要調(diào)用的的服務(wù)中使用RestTemplate

package com.imooc.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
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;

/**
 * @Author penn
 * @Date 2019/1/5 11:41
 **/
@RestController
@Slf4j
public class ClientController {

    @Autowired   //方法二
    private LoadBalancerClient loadBalancerClient;

    @Autowired //方法三
    private RestTemplate restTemplate;

    @GetMapping("/getMsg")
    public String getMsg() {
        //1.第一種方式(直接使用restTemplate,url寫死)
        // 如采用負(fù)載均衡的策略,這種方法的弊端就很明顯,端口不能進(jìn)行實(shí)時(shí)更新如圖一:
        RestTemplate restTemplate = new RestTemplate();
        String response =  restTemplate.getForObject("http://localhost:8080/msg",String.class);
        log.info("response={}"+response);
        //2.第二種方式(利用loadBalancerClient通過(guò)serverID(注入到Eureka時(shí)自己定義的applicationName)獲取url,然后在使用restTemplate)
        //在這里我使用了String.format去拼接字符串,相對(duì)也比較繁瑣
        ServiceInstance serviceInstance = loadBalancerClient.choose("SERVER");
        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()+"/msg");
        String response1 =  restTemplate.getForObject(url,String.class);
        log.info("response1={}"+response1);
        //3.第三種方式(利用@LoadBalanced,可在restTemplate里面使用應(yīng)用名)
        //這可能才是正確的方式,將RestTemplate作為一個(gè)bean配置
        String response2 = restTemplate.getForObject("http://SERVER/msg",String.class);
        log.info("response2={}"+response2);
        
        
        return null;
    }
}
圖一
package com.imooc.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;

/**
 *  方法三使用
 * @Author penn
 * @Date 2019/1/5 12:04
 **/
@Component
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

這樣RestTemplate的基本使用就完成了!

簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容