@FeignClient
is an annotation provided by Spring Cloud to declare a REST client based on Feign implementation.
Feign is a declarative HTTP client that simplifies the process of writing HTTP clients, describing HTTP APIs by defining interfaces and annotations without writing implementation code.
Some main parameters and detailed explanations of @FeignClient annotation
value
/name
: Used to specify the name of the target service.
- Available
value
orname
To specify the name of the service, this name will be registered in the Service Discovery Center (such as Eureka) and used for service discovery. - Example:
@FeignClient(name = "example-service")
url
: Used to specify the URL address of the target service.
- If you know the exact address of the target service, you can use
url
Parameters are specified. - Example:
@FeignClient(url = "")
path
: Optional, used to specify the base path requested by the client.
- If the target service's API has a public base path, you can use
path
Specify the parameters so that the portion of the public path can be omitted when defining the request method. - Example:
@FeignClient(name = "example-service", path = "/api")
configuration
: Optional, to specify the configuration class for Feign client.
- You can specify a configuration class through this parameter to customize the Feign client.
- Example:
@FeignClient(name = "example-service", configuration = )
fallback
/fallbackFactory
: Optional, used to specify the fallback processing logic when the Feign client request fails.
-
fallback
The parameters can directly specify the class for the fallback process, andfallbackFactory
It is a factory class that creates an instance of the fallback processing class. - Example:
@FeignClient(name = "example-service", fallback = )
contextId
: Optional, to specify the context ID of the Feign client.
- There may be multiple Feign clients in one application, through
contextId
You can specify a unique context ID for each client. - Example:
@FeignClient(name = "example-service", contextId = "myFeignClient")
By using@FeignClient
Annotation, you can easily declare a REST client and define an interface to communicate with the target service. Feign will automatically generate corresponding HTTP requests based on these definitions.
Give an example
Suppose you have a microservice architecture, with a service nameduser-service
, it provides some user-related APIs, such as obtaining user information, creating users, etc.
Now you want to call it in another serviceuser-service
API.
You can use Feign to achieve this.
First, you need to declare a Feign client interface in the caller's service:
import ; import ; import ; @FeignClient(name = "user-service") public interface UserServiceClient { @GetMapping("/users/{userId}") User getUserById(@PathVariable("userId") Long userId); }
The above code defines a name calledUserServiceClient
The interface through@FeignClient
The annotation specifies the name of the target service asuser-service
。
A method is declared in the interfacegetUserById
, used to calluser-service
In-house/users/{userId}
API, obtain user information based on user ID.
You can then inject it into the caller's serviceUserServiceClient
And call the corresponding method:
import ; import ; import ; import ; @RestController public class UserController { private final UserServiceClient userServiceClient; @Autowired public UserController(UserServiceClient userServiceClient) { = userServiceClient; } @GetMapping("/users/{userId}") public User getUser(@PathVariable("userId") Long userId) { return (userId); } }
In this example,UserController
is a Spring MVC controller, by injectionUserServiceClient
Come to calluser-service
API.
When called/users/{userId}
When interfaced, an HTTP request will actually be sent to the Feign client proxyuser-service
and return the corresponding result.
In this way, you can easily implement communication between services in the microservice architecture, and Feign simplifies the caller's code, making the whole process more concise and easy to maintain.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.