I. BASIC configuration
1. Introduction of dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> </dependency> <dependency> <groupId> Org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-openfeign</artifactid > </dependency> <dependency> <groupid>org.springframework.cloud</groupid > <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
2. Create the main class and open the feign function by @EnableFeginClients annotations
@SpringBootApplication @enablediscoveryclient@enablefeignclients Public class Application { publicstaticvoid main (string[] args) { Springapplication.run (Application. class , args);} }
3. Define the Aservice interface, bind the service by specifying the service name @FeignClient annotations, and then use the annotations of SPRINGMVC to bind the REST interface provided by the specific service
@FeignClient ("aservice")//The service name here is case- insensitive publicinterface Aservice { @PostMapping ("/hello") String hello ();}
When calling Aservice, use the @Autowired annotations in the class to inject the Aservice instance directly and invoke the/hello interface
@RestController Public class Consumercontroller { @Autowired private aservice aservice; @RequestMapping ("/test") public String Test ( ) { return Aservice.hello (); }}
Two. Parameter binding
@FeignClient ("Aservice") public interface Aservice {
@RequestMapping ("/hello1") hello1 (@RequestParam (" Hello1 "
@RequestMapping("/hello2"
String Hello2 (@RequestHeader ("Hello2") string Hello2)
@ Requestmapping("/hello3"
String Hello2 (@RequestBody user user)
}
@RestController Public classbcontroller{@RequestMapping (value ="/hello1", method = Requestmethod.get) String hello1 (@RequestParamString Hello1) {
return "Hello";
}
@RequestMapping (value ="/hello2", method = Requestmethod.get)
String Hello2 (@RequestHeaderString Hello2) {
return "Hello";
}
@RequestMapping (value ="/hello3", method = Requestmethod.post)
String Hello3 (@RequestBody user user) {
return "Hello";
}
}
Three. Ribbon Configuration
Because feign client load balancing is implemented through the Ribbon, you can customize the parameters that each service client invokes by configuring the Ribbon client.
1. Global configuration
The global configuration uses the Ribbon.<key>=<value> method to set the Ribbon's default parameters directly. For example, modify the default client call time-out:
Ribbon. readtimeout=theribbon. ConnectTimeout=
2. Specify the service configuration
In most cases, the time-out for service calls will be adjusted based on the characteristics of the actual service, so the service configuration needs to be specified
Specifies that the service configuration is configured according to the <client>.ribbon.key=value format
aservice.ribbon.readtimeout=aservice.ribbon.ConnectTimeout=
3. Retry mechanism
Ribbon. maxautoretries=1Ribbon. Maxautoretriesnextserver=2
Maxautoretries is set to 1, so the retry policy first attempts to access the preferred instance once, the instance access is not replaced after the failure, and the number of replacement instance accesses is set to 2 by the Maxautoretriesnextserver parameter, so an attempt is made to replace two instances for retries.
Springcloud Feign configuration (consul based)