springcloud-- Alibaba-nacos-- support of several service consumption

By "Spring Cloud Alibaba Basic Tutorial: Using Nacos realize service registration and discovery," a text of the study, we have learned how to use Nacos to achieve registration and discovery services, but also introduces specific examples of how to obtain a service through LoadBalancerClient interface and to initiate a service request according to an example interface for consumer information. However, this approach requires us to write by hand the service selection, links stitching and other tedious work for developers is very unfriendly. So then, let us look at in addition, what other services also support consumption.

Use RestTemplate

In the previous example, we have been used RestTemplateto initiate HTTP requests to a particular instance of the service, but the specific request path is accomplished by splicing, to develop experience is not good. But, in fact, in the Spring Cloud do for RestTemplate enhanced, just a little configuration, you can simplify the invocation before.

such as:

@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 Template Residual rest template () { return new Template Rest (); } }





It can be seen in the definition of RestTemplate time, an increase of @LoadBalancednotes, but when you actually call the service interface, the original host part through, when the direct use of hand-stitching service names ip and port to write request path. In real time call, Spring Cloud request will be intercepted, and then select a node through a load balancer, and replace part of the service name specific ip and port, in order to achieve load balancing based calling service name.

About this way, you can view the complete code sample warehouse at the end of the text. For the realization of the principle in this way, you can refer to before I wrote the first part of this article: the Spring Cloud source code analysis (two) Ribbon

Use WebClient

WebClient is newly introduced in Spring 5, it can be understood as a reactive version of RestTemplate. By way of specific example below, it would achieve the same above RestTemplate call request:

@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();
}

}

It can be seen in the definition of WebClient.Builder time, that adds @LoadBalancedcomment, the principle and the time before RestTemplate same. For a complete example WebClient can also be viewed in the warehouse by the end of the text.

Using Feign

RestTemplate and WebClient described above are tools Spring own package, here are a member of Netflix OSS, through which you can more easily define and use the service consumer clients. Here also cite a specific example, two ways to achieve the result that the content is consistent with the above:

Step 1: pom.xmlincreased reliance openfeign in:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Step two: Defining and using Feign Feign client client:

@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方法来触发对服务提供方的调用。关于使用Feign的完整例子也可以通过在文末的仓库中查看。

深入思考

如果之前已经用过Spring Cloud的读者,肯定会这样的感受:不论我用的是RestTempalte也好、还是用的WebClient也好,还是用的Feign也好,似乎跟我用不用Nacos没啥关系?我们在之前介绍Eureka和Consul的时候,也都是用同样的方法来实现服务调用的,不是吗?

确实是这样,对于Spring Cloud老手来说,就算我们更换了Nacos作为新的服务注册中心,其实对于我们应用层面的代码是没有影响的。那么为什么Spring Cloud可以带给我们这样的完美编码体验呢?实际上,这完全归功于Spring Cloud Common的封装,由于在服务注册与发现、客户端负载均衡等方面都做了很好的抽象,而上层应用方面依赖的都是这些抽象接口,而非针对某个具体中间件的实现。所以,在Spring Cloud中,我们可以很方便的去切换服务治理方面的中间件。

Guess you like

Origin www.cnblogs.com/ww25/p/11119635.html