Quickly set up Feign in microservices and set up Feign in microservices

Source: Internet
Author: User

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.