Introduction to OpenFeign
Feign is a declarative Web Service client. Its appearance makes it easy to develop Web Service clients. To use feign, you only need to create an interface and add corresponding annotations, such as FeignClient annotation. Feign has pluggable annotations, including feign annotations and JAX-RS annotations.
Feign also supports encoders and decoders. Spring Cloud Open Feign enhances feign, supports Spring MVC annotations, and can use HttpMessageConverters like Spring Web.
Feign is a declarative and templated HTTP client. Using feign in Spring Cloud, you can use HTTP requests to access remote services, just like calling local methods. Developers are completely unaware that they are calling remote methods, let alone accessing HTTP requests.
Functions of openFeign
1. Pluggable annotation support, including Feign annotation and JAX-RS annotation.
2. Support pluggable HTTP encoder and decoder (Gson, Jackson, Sax, JAXB, JAX-RS, SOAP).
3. Support Hystrix and its Fallback.
4. Support Ribbon load balancing.
5. Support HTTP request and response compression.
6. Flexible configuration: configure based on name granularity
7. Support multiple clients: JDK URLConnection, apache httpclient, okhttp, ribbon)
8. Support log
9. Support error retry
10.url support placeholder
11. It can operate independently without relying on the registration center
Original link: https://blog.csdn.net/qq_43565087/article/details/106207867
Create project
Continue to create a Springboot project named Nacos feign under the Nacos project. Add the dependency of OpenFeign when creating, as shown in the figure:
POM of Nacos fegin The XML file is as follows:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
Add project profile
Under the resource directory, add application YML configuration
server: port: 10003 spring: application: name: nacos-feign cloud: nacos: discovery: server-addr: 192.168.230.136:8848
Startup class settings
package com.jq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosFeignApplication { public static void main(String[] args) { SpringApplication.run(NacosFeignApplication.class, args); } }
Create control layer test:
package com.jq.controller; import com.jq.config.RemoteClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("feign") public class FeignClientsController { @Autowired private RemoteClient remoteClient; @GetMapping("/feign") public String test() { return remoteClient.helloNacos(); } }
Create FeignClient interface
package com.jq.config; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "server",fallback = RemoteHystrix.class)//Specify degraded services //Create a RemoteClient interface to define the remote service interface to be called by OpenFeign. // At the same time, specify the service name of the callee through the @ FeginClient annotation and the RemoteHystrix class through the fallback attribute to fuse and degrade the remote call. public interface RemoteClient { //Method of degradation @GetMapping("/hell/helloNacos") String helloNacos(); }
Specify the RemoteHystrix class to perform the fusing and degradation processing of remote calls.
package com.jq.config; import org.springframework.stereotype.Component; @Component public class RemoteHystrix implements RemoteClient{ public String helloNacos() { return "The request timed out?????"; } }
Browser access http://localhost:10003/feign/feign , you can see that the returned result is the same as the RestTemplate result, but it is more elegant for coding and operation mode.
Interface for accessing Nacos feign hhttp://localhost:10003/feign/feign , you can call the server interface remotely through OpenFeign and return the result:
Caller:
Provider:
summary
OpenFegin integrates Ribbon and Hystrix to provide a more elegant calling method for remote calls in microservices. It supports load balancing and fault-tolerant mechanisms.
Original text https://blog.csdn.net/qq_33619378/article/details/95353326?spm=1001.2014.3001.5501