Overview
Regarding that consumers can use RestTemplate, WebClient, Feign and other methods when consuming registered services through Nacos, what is the difference between them?
Use RestTemplate
RestTemplate initiates an HTTP request to a specific instance of the service, but the specific request path is completed by splicing, which is not good for the development experience. However, in fact, the RestTemplate has been enhanced in Spring Cloud, and only a little configuration is required to simplify the previous calling method.
@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();
}
}
When defining RestTemplate, the @LoadBalanced annotation is added, and when the service interface is actually called, the original host part is manually spliced ip and port, and the request path can be written when the service name is directly used.
@LoadBalanced: Yes Get the service provider application name through the registration center, select the node through the load balancer, and replace the service name part with the specific IP and port, so as to realize the load balancing call based on the service name.
Use WebClient
WebClient is the latest introduction in Spring 5, which can be understood as a reactive version of RestTemplate. Here is a specific example, which will implement the same request call as the above 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();
}
}
Annotations WebClient.Builder
were added during the definition , @LoadBalanced
and the principle is the same as the previous RestTemplate.
Use Feign
The first step: increase the dependency of openfeign in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Step 2: Define Feign client and use Feign client:
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients //开启扫描Spring Cloud Feign客户端的功能
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") //指定这个接口所要调用的服务名称,接口中定义的各个函数使用Spring MVC的注解就可以来绑定服务提供方的REST接口
interface Client {
@GetMapping("/hello")
String hello(@RequestParam(name = "name") String name);
}
}
Note: Text citation