In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use RestTemplate and WebClient as well as Feign. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
We have learned how to use Nacos to achieve service registration and discovery. We also introduce how to obtain a specific instance of a service through LoadBalancerClient interface and initiate a service interface consumption request according to the instance information. But this approach requires us to manually write service selection, link splicing and other tedious work, which is very unfriendly for developers. So next, let's take a look at what other ways of consuming services are supported.
Use RestTemplate
In the previous example, RestTemplate has been used to initiate an HTTP request to a specific instance of the service, but the specific request path is done through splicing, which is not good for the development experience. However, in fact, RestTemplate has been enhanced in Spring Cloud to simplify the previous invocation with only a little configuration.
@ EnableDiscoveryClient@SpringBootApplicationpublic 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 ();}}
As you can see, the @ LoadBalanced annotation is added when defining RestTemplate, but when the service interface is actually called, the original host part is by manually concatenating the ip and the port, and the request path is written when the service name is directly used. In the actual call, Spring Cloud will intercept the request, then select the node through the load balancer, and replace the service name part with the specific ip and port, thus realizing the load balancer call based on the service name.
Use WebClient
WebClient is the latest introduction in Spring 5 and can be understood as a reactive version of RestTemplate. Here is a concrete example that will implement the same request call as the RestTemplate above:
@ EnableDiscoveryClient@SpringBootApplicationpublic 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 test () {Mono result = webClientBuilder.build () .get () .uri ("http://alibaba-nacos-discovery-server/hello?name=didi") .resume () .bodyToMono (String.class); return result) @ Bean @ LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder () {return WebClient.builder ();}}
As you can see, the @ LoadBalanced annotation is also added when defining WebClient.Builder, which works the same as the previous RestTemplate. A complete example of WebClient can also be seen in the warehouse at the end of the article.
Use Feign
The RestTemplate and WebClient described above are tools packaged by Spring itself. Here is a member of Netflix OSS that makes it easier to define and use service consumption clients. The following is a specific example, and its implementation is consistent with the results of the above two methods:
Step 1: increase the dependency of openfeign in pom.xml:
@ EnableDiscoveryClient@SpringBootApplication@EnableFeignClientspublic 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);}}
Here, the function of scanning Spring Cloud Feign client is enabled through the @ EnableFeignClients annotation, and then a client interface definition of Feign is created. Use the @ FeignClient annotation to specify the name of the service to be called by this interface, and the functions defined in the interface can bind the service provider's REST interface using the Spring MVC annotation. For example, the following is an example of binding the / hello interface of an alibaba-nacos-discovery-server service. Finally, in Controller, the implementation of the Client interface is injected and the hello method is called to trigger the call to the service provider.
Think deeply
Readers who have used Spring Cloud before will certainly feel this way: whether I use RestTempalte, WebClient or Feign, it doesn't seem to have anything to do with whether I use Nacos or not. When we introduced Eureka and Consul earlier, we all implemented service invocation in the same way, didn't we?
Indeed, for Spring Cloud veterans, even if we replace Nacos as the new service registry, it will have no impact on our application-level code. So why can Spring Cloud give us such a perfect coding experience? In fact, this is entirely due to the encapsulation of Spring Cloud Common, because it has done a good abstraction in service registration and discovery, client load balancing and other aspects, while the upper application depends on these abstract interfaces, rather than the implementation of a specific middleware. Therefore, in Spring Cloud, we can easily switch between middleware in service governance.
The above is how to use RestTemplate and WebClient as well as Feign. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.