javascript
使用Spring Cloud休息客户电话
使用Spring-Cloud項目進行REST客戶端調用有幾種有趣的方法。 Spring-Cloud REST支持建立在核心Netflix OSS庫的基礎上,但將它們抽象化并在此過程中簡化了庫的使用。
RestTemplate
首先,讓我們考慮使用RestTemplate通過基于Spring的應用程序進行Rest調用的傳統方法:
public class RestTemplateIntegrationTest {@Autowiredprivate RestTemplate restTemplate;@Testpublic void testCallPongService() {ResponseEntity<MessageAcknowledgement> ack =restTemplate.exchange("http://servicehost/message",HttpMethod.POST,new HttpEntity<>(new Message("test", "hello")),MessageAcknowledgement.class,Collections.emptyMap());assertThat(ack.getBody().getPayload(), equalTo("Pong From Configuration Server"));} }在此特定實例中,預期客戶端將完全知道url的主機部分,RestTemplate將負責將Java對象編組為適當的媒體類型,進行REST調用,并將響應編組回Java對象。 。
帶功能區和尤里卡的RestTemplate
Netflix Ribbon提供了一個用于進行基于REST的調用的庫,而使用RestTemplate可以使客戶端完全了解該主機,而使用Ribbon則通常可以通過集中式Netflix Eureka服務器來解析該主機,并且Ribbon可以在以下情況下實現負載均衡找到一個服務的多個主機。 如果在類路徑中存在Spring-cloud庫和Ribbon相關的庫,則Spring-Cloud將RestTemplate增強為基于Ribbon,而不需要其他配置,而Spring-Cloud的調用完全像以前一樣,但有一些變化。
ResponseEntity<MessageAcknowledgement> ack =restTemplate.exchange("http://sample-pong/message",HttpMethod.POST,new HttpEntity<>(new Message("test", "hello")),MessageAcknowledgement.class,Collections.emptyMap());所不同的是,在這種情況下,主機名是“ sample-pong”,不是真正的主機名,而是試圖在Eureka中查找以該名稱作為注冊名稱的服務器列表。結果主機/端口用于發出請求。
如果需要自定義,則可以為命名客戶端提供為命名客戶端指定的Ribbon功能區特定屬性,方法如下:
ResponseEntity<MessageAcknowledgement> ack =restTemplate.exchange("http://samplepong/message",HttpMethod.POST,new HttpEntity<>(new Message("test", "hello")),MessageAcknowledgement.class,Collections.emptyMap());上面命名的客戶端為“ samplepong”,此客戶端的功能區特定屬性如下:
samplepong:ribbon:DeploymentContextBasedVipAddresses: sample-pongNIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerListReadTimeout: 5000MaxAutoRetries: 2- 如果您對功能區的更多低級配置感興趣, 請參閱此處
Ribbon是進行REST調用的一種相當復雜的底層方法,RestTemplate抽象化Ribbon的實現并使其從客戶端的角度看起來很容易。
Netflix Feign
Netflix Feign是對基于REST的服務進行調用的另一種簡化方法,它所需要的只是一個帶有相關注釋的接口,最好通過一個示例進行演示:
import org.bk.consumer.domain.Message; import org.bk.consumer.domain.MessageAcknowledgement; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;@FeignClient("samplepong") public interface PongClient {@RequestMapping(method = RequestMethod.POST, value = "/message",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)MessageAcknowledgement sendMessage(@RequestBody Message message); }盡管注釋是特定于Spring的,但Spring-Cloud通過添加支持Spring MVC注釋的編碼器和解碼器來簡化此操作。
接口上的@FeignClient批注將其標識為FeignClient代碼。 在Spring Configuration中需要@EnableFeignClients來加載所有這樣的FeignClient。
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class PingApplication {public static void main(String[] args) {SpringApplication.run(PingApplication.class, args);} }資源資源
- Spring Cloud文檔
- Josh Long的博客,關于使用Spring Cloud和Netflix的Eureka進行微服務注冊和發現
翻譯自: https://www.javacodegeeks.com/2015/09/rest-client-calls-with-spring-cloud.html
總結
以上是生活随笔為你收集整理的使用Spring Cloud休息客户电话的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云闪付手机闪付怎么用?
- 下一篇: rest spring_Spring的R