Home >Java >javaTutorial >Feign of spring cloud uses HTTP to request remote services
In the spring Cloud Netflix stack, each microservice exposes its own service in the form of HTTP interface, so it must be used when calling remote services HTTP client. We can use JDK's native URLConnection, Apache's Http Client, Netty's asynchronous HTTP Client, and Spring's RestTemplate. However, the most convenient and elegant one to use is Feign.
Feign is a declarative, templated HTTP client. Using Feign in Spring Cloud, we can achieve the same coding experience as calling local methods when using HTTP to request remote services. Developers are completely unaware that this is a remote method, let alone an HTTP request.
1. Add dependencies
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2. Create FeignClient
@FeignClient(name="SPRING-PRODUCER-SERVER/spring")public interface FeignUserClient { @RequestMapping( value = "/findAll/{name}",method = RequestMethod.GET) public List<SpringUser> findAll(@PathVariable("name") String name); @RequestMapping( value = "/findUserPost",method = RequestMethod.POST) public SpringUser findUserPost(@RequestBody SpringUser springUser);//复合类型好像默认是POST请求 }
@FeignClient(name="SPRING-PRODUCER-SERVER/spring"): used to notify the Feign component to proxy the interface (no need to write the interface Implementation), the name attribute specifies which service we want to call. Users can inject directly through @Autowired.
@RequestMapping indicates that a GET request needs to be sent to /group/{groupId} when calling this method.
@PathVariable has the same meaning as the corresponding annotation in SpringMVC.
Principle: When a Spring Cloud application is started, Feign will scan the interface marked with @FeignClient annotation, generate a proxy, and register it in the Spring container. When generating a proxy, Feign will create a RequetTemplate object for each interface method. This object encapsulates all the information required for HTTP requests. The request parameter name, request method and other information are determined in this process. Feign's templating is reflected in here.
3. Add annotations to the startup class
@Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @EnableFeignClientspublic class SpringConsumerServerFeignApplication {public static void main(String[] args) { SpringApplication.run(SpringConsumerServerFeignApplication.class, args); } }
4. Configuration file application.yml
spring: application: name: spring-consumer-server-feign server: port: 8084 context-path: /spring #服务注册中心的配置内容,指定服务注册中心的位置 eureka: client: serviceUrl: defaultZone: http://user:password@localhost:8761/eureka/
1. Customized Configuration
@Configurationpublic class FooConfiguration { @Beanpublic Contract feignContract() {//这将SpringMvc Contract 替换为feign.Contract.Defaultreturn new feign.Contract.Default(); } }
2. Use customized Configuration
@FeignClient(name="SPRING-PRODUCER-SERVER/spring",configuration=FooConfiguration.class)public interface FeignUserClient { @RequestLine("GET /findAll/{name}")public List<SpringUser> findAll(@Param("name") String name); /* @RequestMapping( value = "/findAll/{name}",method = RequestMethod.GET) public List<SpringUser> findAll(@PathVariable("name") String name); @RequestMapping( value = "/findUserPost",method = RequestMethod.POST) public SpringUser findUserPost(@RequestBody SpringUser springUser);*/}
@RequestLine:是feign的注解 为每个创建的Feign客户端创建一个记录器。默认情况下,记录器的名称是用于创建Feign客户端的接口的完整类名。Feign日志记录仅响应DEBUG级别。logging.level.project.user.UserClient: DEBUG 在配置文件application.yml 中加入:
logging: level: com.jalja.org.spring.simple.dao.FeignUserClient: DEBUG
Add log level in the custom Configuration class
@Configurationpublic class FooConfiguration { /* @Bean public Contract feignContract() { //这将SpringMvc Contract 替换为feign.Contract.Default return new feign.Contract.Default(); }*/@Bean Logger.Level feignLoggerLevel() {//设置日志return Logger.Level.FULL; } }
PS: Feign request timeout problem
The default timeout of Hystrix is 1 second. If there is no response after this time, the fallback code will be entered. The first request is often slow (because of Spring's lazy loading mechanism, which requires instantiating some classes), and the response time may be greater than 1 second.
There are three solutions, taking feign as an example.
Method one
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
This configuration is to change the timeout of Hystrix to 5 seconds
Method two
hystrix.command. default.execution.timeout.enabled: false
This configuration is used to disable the timeout period of Hystrix
Method 3
feign.hystrix.enabled: false
This configuration is used to simply disable feign's hystrix . This approach is not recommended except in some special scenarios.
<br><br>
The above is the detailed content of Feign of spring cloud uses HTTP to request remote services. For more information, please follow other related articles on the PHP Chinese website!