Please state the source of the article. Welcome to add Echo wechat (wechat id: T2421499075) for exchange and learning.


This is a series of articles, so if you’re not familiar with west SpringCloud, you should check out the previous ones.

Once we had completed service registration and discovery, we realized that our communication was a little bit more complicated, so we’ll focus solely on Feign integration. This is based on the project in the article “Spring Cloud Alibaba- Access the Nacos Registry to understand Service registration and discovery”

Add related dependencies

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

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-dependencies</artifactId>
	<version>Hoxton.SR9</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>
Copy the code

Adjust the way the consumer project interface is invoked

  • Step 1: Add the following annotations to the startup class
@EnableFeignClients
Copy the code

Add the Feign interface class

The corresponding interface of this interface class is the method in our service provider

@FeignClient("spring-cloud-nacos-server")
public interface TestClient {

    @GetMapping("/test")
    public String test(a);

}
Copy the code

Rewrite the way test is called from consumer

@RestController
public class Test {

    @Autowired
    private TestClient testClient;

    @GetMapping("/test")
    public String test(a) {
        String result = testClient.test();
        return "Consumer calls provider returns message:"+ result; }}Copy the code

Start the service, use the tool invocation, and the result is as follows

  • Spring Cloud Alibaba- Access Nacos Registry to learn about service registration and discovery
  • Spring Cloud Alibaba- Using NACOS as a registry