咱们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何经过LoadBalancerClient接口来获取某个服务的具体实例,并根据实例信息来发起服务接口消费请求。可是这样的作法须要咱们手工的去编写服务选取、连接拼接等繁琐的工做,对于开发人员来讲很是的不友好。因此接下来,咱们再来看看除此以外,还支持哪些其余的服务消费方式。react
在以前的例子中,已经使用过RestTemplate
来向服务的某个具体实例发起HTTP请求,可是具体的请求路径是经过拼接完成的,对于开发体验并很差。可是,实际上,在Spring Cloud中对RestTemplate作了加强,只须要稍加配置,就能简化以前的调用方式。web
@EnableDiscoveryClient @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired RestTemplate restTemplate; @GetMapping("/test") public String test() { String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=didi", String.class); return "Return : " + result; } } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } } |
能够看到,在定义RestTemplate的时候,增长了@LoadBalanced
注解,而在真正调用服务接口的时候,原来host部分是经过手工拼接ip和端口的,直接采用服务名的时候来写请求路径便可。在真正调用的时候,Spring Cloud会将请求拦截下来,而后经过负载均衡器选出节点,并替换服务名部分为具体的ip和端口,从而实现基于服务名的负载均衡调用。app
WebClient是Spring 5中最新引入的,能够将其理解为reactive版的RestTemplate。下面举个具体的例子,它将实现与上面RestTemplate同样的请求调用:负载均衡
@EnableDiscoveryClient @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired private WebClient.Builder webClientBuilder; @GetMapping("/test") public Mono<String> test() { Mono<String> result = webClientBuilder.build() .get() .uri("http://alibaba-nacos-discovery-server/hello?name=didi") .retrieve() .bodyToMono(String.class); return result; } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } }
能够看到,在定义WebClient.Builder的时候,也增长了@LoadBalanced
注解,其原理与以前的RestTemplate时同样的。关于WebClient的完整例子也能够经过在文末的仓库中查看。函数
上面介绍的RestTemplate和WebClient都是Spring本身封装的工具,下面介绍一个Netflix OSS中的成员,经过它能够更方便的定义和使用服务消费客户端。下面也举一个具体的例子,其实现内容与上面两种方式结果一致:工具
第一步:在pom.xml
中增长openfeign的依赖:ui
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired Client client; @GetMapping("/test") public String test() { String result = client.hello("didi"); return "Return : " + result; } } @FeignClient("alibaba-nacos-discovery-server") interface Client { @GetMapping("/hello") String hello(@RequestParam(name = "name") String name); } }
这里主要先经过@EnableFeignClients
注解开启扫描Spring Cloud Feign客户端的功能;而后又建立一个Feign的客户端接口定义。使用@FeignClient
注解来指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就能够来绑定服务提供方的REST接口,好比下面就是绑定alibaba-nacos-discovery-server
服务的/hello
接口的例子。最后,在Controller中,注入了Client接口的实现,并调用hello方法来触发对服务提供方的调用。编码
若是以前已经用过Spring Cloud的读者,确定会这样的感觉:不论我用的是RestTempalte
也好、仍是用的WebClient
也好,仍是用的Feign
也好,彷佛跟我用不用Nacos没啥关系?咱们在以前介绍Eureka和Consul的时候,也都是用一样的方法来实现服务调用的,不是吗?spa
确实是这样,对于Spring Cloud老手来讲,就算咱们更换了Nacos做为新的服务注册中心,其实对于咱们应用层面的代码是没有影响的。那么为何Spring Cloud能够带给咱们这样的完美编码体验呢?实际上,这彻底归功于Spring Cloud Common的封装,因为在服务注册与发现、客户端负载均衡等方面都作了很好的抽象,而上层应用方面依赖的都是这些抽象接口,而非针对某个具体中间件的实现。因此,在Spring Cloud中,咱们能够很方便的去切换服务治理方面的中间件。rest