Feign's experience
Feign is a declarative WebService client. Using Feign makes it easier to write WebService clients by defining an interface and then adding annotations to the interface, as well as supporting JAX-RS standard annotations. Feign also supports pluggable encoders and × ×. SpringCloud encapsulates Feign to support SpringMVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing.
How to use Feign?
Add dependencies for Feign
Org.springframework.cloud
Spring-cloud-starter-feign
Org.springframework.cloud
Spring-cloud-starter-eureka
Launch the portal to join:
@ SpringBootApplication
@ EnableEurekaClient
@ EnableFeignClients
Finally, don't forget the most important one. I never thought of that. Don't forget. Hehe
Org.springframework.cloud
Spring-cloud-dependencies
Dalston.SR3
Pom
Import
Add annotations and request services to the defined interface: @ FeignClient (name= "name of service")
API method join request method: @ RequestMapping (method = RequestMethod.GET, value = "/ hello")
As for service, there is basically nothing special. The best thing is the client side.
Service just put in @ RestController and @ RequestMapping and can
The stores attribute in the @ FeignClient annotation can be an arbitrary string, and if combined with Eureka, stores should be the service name in Eureka, which Feign uses to create an Ribbon load balancer. You can also specify an address through the url attribute, either the full URL or a hostname. The @ FeignClient annotated interface is marked, and the Bean instance name in ApplicationContext is the fully qualified name of this interface, and the Bean also has an alias, Bean name + FeignClient.
Override the default configuration of Feign
A core concept in SpringCloud's encapsulation of Feign is that the client should have a name. Each client can make a request to a remote service at any time, and each service can specify a name as if it were the @ FeignClient annotation. SpringCloud combines all the @ FeignClient together to create a new ApplicationContext and uses FeignClinetsConfiguration to configure Clients. The configuration includes an encoder, a × ×, and a feign.Contract.
SpringCloud allows you to fully control the configuration information of Feign through the configuration attribute, which has higher priority than FeignClientsConfiguration.
In this way, a basic feign project is completed.