SpringCloud Feign use
- SpringCloud version: Hoxton.RELEASE
Spring Cloud of Feign -. JMCui - blog Park
@FeignClient arguments detailed
- Introduced maven dependence
- 配置 Feign, @EnableFeignClients
- Interface declaration
1. introduced maven dependent
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. Configure Feign
Marked notes in SpringBootApplication place, @EnableFeingClients
3. The interface declaration
Disclaimer configuration feign interface, simple example
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* feign声明
* @author suwenguang
**/
@FeignClient(value = "test-user-provider-service")
public interface FeignUserService {
@RequestMapping("/user/get")
String get();
}
The interface declaration includes but is not limited to the following section
3.1 of the path statement
3.2 parameter declaration
3.3 Back Declaration
Spring Cloud OpenFeign
3.1 path statement
3.1.1. Internal service registry calls
3.1.2 Call external interface
3.1.1. Internal call service registry
- By service name specified registry
@FeignClient(value="xxx-service")
example:
@FeignClient(value = "test-user-provider-service")
public interface FeignUserService {
@RequestMapping("/user/get")
String get();
}
3.1.2 Calling external interface
feign not only it can be used as micro-service calls, as long as the HTTP requests are basically meet
- Complete address with the specified url
example:
@FeignClient(url = "192.168.9.233:19192/ttc",name = "commision-service")
public interface FeignCommisionService {
@RequestMapping(method = RequestMethod.GET,value = "/health/get")
Object health();
}
3.2 parameter declaration
For internal request, we generally limited to a request parameter json format, but not just HTTP request json
here only way json
feign complex parameter passing
Main notes:
@RequestBody
@RequestParam
@PathVariable
If the format is received by the corresponding interface json format, simply serialize json corresponding object passing objects using @RequestBody
serialization, and can be used @RequestParam
to specify the data
How to use Feign request construct multi-parameter | Zhou's blog - focus Spring Cloud, Docker
example:
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);
}
How to feign with file parameter?
I used to be able to specify multipartForm in restTemplate, passing the File object can be. So feign how to make
Spring Cloud OpenFeign
Use @SpringQueryMap
support
example:
@FeignClient("demo")
public interface DemoTemplate {
@GetMapping(path = "/demo")
String demoEndpoint(@SpringQueryMap Params params);
}
3.3 return statement
In fact, you can just use object to accept.
Or directly with a unified body returns to the receiving object. If the call both sides to coordinate the agreement of words.
Configuration 4. feign
- Global Configuration
- Alone configuration
Check the official website of Spring Cloud OpenFeign
The circuit breaker configuration Hystrix
Hoxton hystrix breaker has integrated, using the configuration file to configure the switch hystrix
Can global configuration, may be arranged separately