Function: convenient and quick implementation of the invocation between each service

1. Introduce dependencies

<! - introduction of feign - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>Copy the code

Add an annotation to the startup class (@enableFeignClients)

package cn.itcast;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringCloudApplication
public class ConsumerApplication {

    public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class); }}Copy the code

3. Write an excuse on the interface to tell it how to request, the request name, etc., in SpringMVC fashion, with a (@feignClient (” service name “)) annotation to tell it the service name

package cn.itcast.consumer.client;

import cn.itcast.consumer.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("user-service")
public interface UserClient {

    @GetMapping("user/{id}")
    User queryUserById(@PathVariable("id") Long id);
}
Copy the code

4. In the use class, if the method of calling the class in this project is the same, it will be automatically injected and used

@Autowired
private UserClient userClient;


public User queryUserById(@PathVariable("id") Long id) {

   return userClient.queryUserById(id);
}
Copy the code