Simplify Spring Cloud Service Calls with Nacos: RestTemplate, WebClient & Feign
This article demonstrates how to use Nacos for service registration and discovery in Spring Cloud and shows three convenient client consumption methods—enhanced RestTemplate, reactive WebClient, and declarative Feign—complete with configuration, code examples, and the underlying load‑balancing mechanism.
Using RestTemplate
Spring Cloud enhances RestTemplate so that, after a simple configuration, you no longer need to manually concatenate host, IP, and port. By adding @LoadBalanced to the bean definition, the request URL can be written with the service name, and Spring Cloud will resolve it to an actual instance.
<ol>
<li><code>@EnableDiscoveryClient</code></li>
<li><code>@SpringBootApplication</code></li>
<li><code>public class TestApplication { }</code></li>
<li><code>@Slf4j</code></li>
<li><code>@RestController</code></li>
<li><code>static class TestController { }</code></li>
<li><code>@Autowired</code></li>
<li><code>RestTemplate restTemplate;</code></li>
<li><code>@GetMapping("/test")</code></li>
<li><code>public String test() {</code></li>
<li><code>String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=didi", String.class);</code></li>
<li><code>return "Return : " + result;</code></li>
<li><code>}</code></li>
<li><code>@Bean</code></li>
<li><code>@LoadBalanced</code></li>
<li><code>public RestTemplate restTemplate() { return new RestTemplate(); }</code></li>
</ol>The @LoadBalanced annotation on the RestTemplate bean enables Spring Cloud to intercept the request, select a service instance via the load balancer, and replace the service name with the concrete IP and port.
Using WebClient
WebClient, introduced in Spring 5, is the reactive counterpart of RestTemplate. The usage pattern mirrors the RestTemplate example, but returns a Mono<String> for asynchronous handling.
<ol>
<li><code>@EnableDiscoveryClient</code></li>
<li><code>@SpringBootApplication</code></li>
<li><code>public class TestApplication { }</code></li>
<li><code>@Slf4j</code></li>
<li><code>@RestController</code></li>
<li><code>static class TestController { }</code></li>
<li><code>@Autowired</code></li>
<li><code>WebClient.Builder webClientBuilder;</code></li>
<li><code>@GetMapping("/test")</code></li>
<li><code>public Mono<String> test() {</code></li>
<li><code>Mono<String> result = webClientBuilder.build()</code></li>
<li><code>.get()</code></li>
<li><code>.uri("http://alibaba-nacos-discovery-server/hello?name=didi")</code></li>
<li><code>.retrieve()</code></li>
<li><code>.bodyToMono(String.class);</code></li>
<li><code>return result;</code></li>
<li><code>}</code></li>
<li><code>@Bean</code></li>
<li><code>@LoadBalanced</code></li>
<li><code>public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); }</code></li>
</ol>Just like with RestTemplate, adding @LoadBalanced to the WebClient.Builder bean lets the service name be resolved automatically.
Using Feign
Feign, part of Netflix OSS, provides a declarative way to define service‑consumption interfaces. By enabling @EnableFeignClients and annotating an interface with @FeignClient("alibaba-nacos-discovery-server"), you can call remote endpoints as regular Java methods.
<ol>
<li><code>@EnableDiscoveryClient</code></li>
<li><code>@SpringBootApplication</code></li>
<li><code>@EnableFeignClients</code></li>
<li><code>public class TestApplication { }</code></li>
<li><code>@Slf4j</code></li>
<li><code>@RestController</code></li>
<li><code>static class TestController { }</code></li>
<li><code>@Autowired</code></li>
<li><code>Client client;</code></li>
<li><code>@GetMapping("/test")</code></li>
<li><code>public String test() {</code></li>
<li><code>String result = client.hello("didi");</code></li>
<li><code>return "Return : " + result;</code></li>
<li><code>}</code></li>
<li><code>@FeignClient("alibaba-nacos-discovery-server")</code></li>
<li><code>interface Client {</code></li>
<li><code>@GetMapping("/hello")</code></li>
<li><code>String hello(@RequestParam("name") String name);</code></li>
<li><code>}</code></li>
</ol>The @EnableFeignClients annotation activates scanning for Feign interfaces, and the @FeignClient annotation binds the interface to the Nacos‑registered service.
Deep Thinking
For developers familiar with Spring Cloud, switching the service registry to Nacos does not affect application code because Spring Cloud abstracts service registration, discovery, and client load balancing behind common interfaces. This abstraction allows seamless replacement of underlying middleware without code changes.
Code Samples
All examples referenced in this article are available in the following repositories:
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
Key projects: alibaba-nacos-discovery-server – the service provider (must be started). alibaba-nacos-discovery-client-resttemplate – RestTemplate consumption. alibaba-nacos-discovery-client-webclient – WebClient consumption. alibaba-nacos-discovery-client-feign – Feign consumption.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
