Quickly set up Feign in microservices and set up Feign in microservices
Build a declarative REST client feign in the microservice architecture ].
Feign is a declarative Web service client. This makes writing to the Web service client more convenient. Use Feign to create an interface and annotate it. It has support for pluggable comments, including Feign comments and JAX-RS comments. Spring Cloud supports Spring MVC annotations and uses HttpMessageConverters, which is used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide an http client for Load Balancing when using Feign.
For more information about Feign microservice downgrade, see Hystrix service downgrade in Fegin.
How to join
Feign
Step 1
Add feign Jar
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
Step 2
Modify the configuration file application. yml
spring: application: name: microservice-consumer-movieserver: port: 7901eureka: client: healthcheck: enabled: true serviceUrl: defaultZone: http://user:password123@localhost:8761/eureka instance: prefer-ip-address: true
Step 3
Add the fegin client interface
Import org. springframework. cloud. netflix. feign. feignClient; import org. springframework. web. bind. annotation. pathVariable; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. yonggan. entity. user; @ FeignClient ("microservice-provider-user") // service name public interface UserFeignClient {@ RequestMapping (value = "/simple/{id}", method = RequestMethod. GET) User findById (@ PathVariable ("id") Long id );}
Step 4
Add the fegin interface and inject it to our web interface layer.
@ RestController public class MovieController {/*** Add a remote fegin client * learn more: java8733 */@ Autowired private UserFeignClient feignClient; @ GetMapping ("/movie/{id}") public User findById (@ PathVariable Long id) {return feignClient. findById (id );}}
The above is a demo of building the Fegin environment quickly. It is supported by Shanghai shangxuexiang java. Thank you!