2.2通過(guò)服務(wù)ID找到服務(wù)在微服務(wù)Spring Cloud快速入門(mén)中我們定義了如下Feign接口,在這個(gè)接口上面,我們添加了一個(gè)注解@FeignClient,指定了服務(wù)ID:hutao-microservice-item 。這個(gè)Feign接口會(huì)給我發(fā)起Http調(diào)用 。當(dāng)然在這里我們可能無(wú)法看到負(fù)載均衡的效果,因此我們需要稍微深入下底層代碼 。
@FeignClient(value = "https://www.520longzhigu.com/shenghuo/hutao-microservice-item")@RequestMapping("/itemservice")public interface FeignOrderService {/*** @Description:使用聲明式HTTP客戶(hù)端發(fā)起請(qǐng)求:根據(jù)ID查詢(xún)訂單* @author hutao* @mail [email protected]* @date 2020年8月30日*/ @GetMapping(value = "https://www.520longzhigu.com/shenghuo/item/{itemId}") Items queryItem(@PathVariable("itemId")String itemId);}大家應(yīng)該還記得,我們的商品服務(wù)和訂單服務(wù),注冊(cè)到注冊(cè)中心的時(shí)候,我們?cè)趩?dòng)類(lèi)上面添加了一個(gè)注解:@EnableDiscoveryClient 。
現(xiàn)在我們來(lái)看下DiscoveryClient這個(gè)接口
2.2.1.DiscoveryClient 解讀DiscoveryClient接口源碼如下,他是用來(lái)發(fā)現(xiàn)服務(wù)的,比如發(fā)現(xiàn)Netflix服務(wù),其中有個(gè)方法List getInstances(String serviceId)是根據(jù)服務(wù)ID獲取服務(wù)實(shí)例集合 。
/** * Represents read operations commonly available to discovery services such as Netflix * Eureka or consul.io. * * @author Spencer Gibb * @author Olga Maciaszek-Sharma */public interface DiscoveryClient extends Ordered { /*** Default order of the discovery client.*/ int DEFAULT_ORDER = 0; /*** A human-readable description of the implementation, used in HealthIndicator.* @return The description.*/ String description(); /*** Gets all ServiceInstances associated with a particular serviceId.* @param serviceId The serviceId to query.* @return A List of ServiceInstance.*/ List<ServiceInstance> getInstances(String serviceId); /*** @return All known service IDs.*/ List<String> getServices(); /*** Default implementation for getting order of discovery clients.* @return order*/ @Override default int getOrder() {return DEFAULT_ORDER; }}我們可以看到DiscoveryClient 有4個(gè)實(shí)現(xiàn),當(dāng)然這里我們用的肯定是Eureka 。
2.2.2.使用DiscoveryClient獲取服務(wù)實(shí)例現(xiàn)在我們來(lái)使用下DiscoveryClient 這個(gè)接口
首先把這個(gè)接口依賴(lài)注入到我們的Controller中,我們看下能獲取到什么
@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping(value = "https://www.520longzhigu.com/shenghuo/order/{orderId}")public Order queryOrderById(@PathVariable("orderId") String orderId) { String serviceId = "hutao-microservice-item"; List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); System.out.println(instances); return null;}通過(guò)調(diào)試代碼,我們發(fā)現(xiàn)如我們之前所說(shuō),我們可以通過(guò)服務(wù)ID:hutao-microservice-item,找到兩個(gè)服務(wù)實(shí)例 。
查看每個(gè)實(shí)例,我們發(fā)現(xiàn)我們能夠看到每個(gè)服務(wù)的地址,端口等信息
如果不使用Feign這種聲明式的調(diào)用Http請(qǐng)求,那我們來(lái)如何調(diào)用?
2.2.3.從服務(wù)實(shí)例中獲取服務(wù)信息,發(fā)起Http請(qǐng)求那么這個(gè)時(shí)候,我們?cè)趺慈グl(fā)起請(qǐng)求恩?很簡(jiǎn)單,就是從服務(wù)實(shí)例中,獲取到服務(wù)信息后,將接口的請(qǐng)求地址拼接出來(lái) 。然后使用restTemplate發(fā)起Http請(qǐng)求
@Autowiredprivate RestTemplate restTemplate;@GetMapping(value = "https://www.520longzhigu.com/shenghuo/order/{orderId}")public Order queryOrderById(@PathVariable("orderId") String orderId) { String serviceId = "hutao-microservice-item"; List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); ServiceInstance serviceInstance = instances.get(0); String url = serviceInstance.getHost() + ":" + serviceInstance.getPort(); Items items = restTemplate.getForObject("http://" + url + "/itemservice/item/1" , Items.class); System.out.println(items); return null;}可以看到我們的請(qǐng)求是能正常訪(fǎng)問(wèn)的 。當(dāng)然也有問(wèn)題存在,那就是我們拿到的是多個(gè)服務(wù),程序怎么知道我要調(diào)用的是哪一個(gè)服務(wù)呢?
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問(wèn)題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專(zhuān)業(yè)人士給予相關(guān)指導(dǎo)!
「愛(ài)刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- linux安裝nginx詳細(xì)教程 查看linux位數(shù)系統(tǒng)
- nginx虛擬主機(jī)講解 nginx虛擬主機(jī)是什么
- 價(jià)位段內(nèi)體驗(yàn)最均衡的TWS降噪耳機(jī)
- 目前最均衡的三星手機(jī) 三星哪款手機(jī)性能最好
- 靠性能均衡而出名 就平均性能而言,目前最好的內(nèi)排序
- 納什均衡理論
- 服務(wù)器中用群集實(shí)現(xiàn)網(wǎng)絡(luò)負(fù)載平衡
- 什么是網(wǎng)絡(luò)負(fù)載平衡
- 群集節(jié)點(diǎn)安裝配置網(wǎng)絡(luò)負(fù)載平衡NLB
- 網(wǎng)絡(luò)負(fù)載平衡群集節(jié)點(diǎn)創(chuàng)建DNS主機(jī)記錄
