使用Spring-Cloud项目进行REST客户端调用有几种有趣的方法。 Spring-Cloud REST支持建立在核心Netflix OSS库的基础上,但将它们抽象化并在此过程中简化了库的使用。

RestTemplate

首先,让我们考虑使用RestTemplate通过基于Spring的应用程序进行Rest调用的传统方法:

public class RestTemplateIntegrationTest {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public 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-pong
    NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
    ReadTimeout: 5000
    MaxAutoRetries: 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