Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Feign in SpringCloud micro-service architecture

Shulou Source: shulou.com Published: 2022-06-01 12:26:50 10月04日 Update

This article mainly explains "how to use Feign in Spring Cloud micro-service architecture". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Feign in Spring Cloud micro-service architecture".

Brief introduction

Feign is a declarative web service client that makes calls between microservices easier, similar to controller calling service. Spring Cloud integrates Ribbon and Eureka to provide a load-balanced http client when using Feign.

In springcloud, you can not only use Ribbo for load balancing, but also use Feign. Feign has made an improvement on the basis of Ribbon, using interface to achieve load balancing.

Use

Import dependency

Org.springframework.cloud spring-cloud-starter-openfeign

Write the corresponding interface

@ FeignClient (value = "PROVIDER-NAME") declares that this is a service id specified by FeignClient,value

@ FeignClient (value = "PROVIDER-NAME") public interface DeptClientService {/ / the path of the request should be the same as that of the service provider @ RequestMapping (value = "/ dev/add") boolean add (Dept dept); @ RequestMapping (value = "/ dev/ {id}") Dept queryByID (@ PathVariable ("id") Long id); @ PostMapping (value = "/ dev/list") List queryAll ();}

Modify Controller

Instead of using RestTemplate to get the objects we need, we get them through the previously defined interfaces

@ RestControllerpublic class ConsumerController {@ Autowired private DeptClientService service; @ RequestMapping ("/ consumer/get/ {id}") public Dept getByID (@ PathVariable ("id") Long id) {return this.service.queryByID (id);} @ RequestMapping ("/ consumer/add") public boolean add (String dname) {Dept dept = new Dept (); dept.setDname (dname); return this.service.add (dept) @ RequestMapping ("/ consumer/list") public List list () {return this.service.queryAll ();}}

Modify startup class

@ EnableFeignClients enables Feign load balancer

@ SpringBootApplication (scanBasePackages = "com") @ EnableEurekaClient@EnableFeignClients (basePackages = "com.service") public class FeignApplication {public static void main (String [] args) {SpringApplication.run (FeignApplication.class,args);}}

Contrast:

Ribbon

Public class ConsumerController {@ Autowired private RestTemplate template; private static final String url= "http://PROVIDER-NAME"; @ RequestMapping (" / consumer/get/ {id} ") public Dept getByID (@ PathVariable long id) {/ / the path of the request, the returned object Dept getEntity = template.getForObject (url +" / dev/ "+ id, Dept.class); return getEntity } @ RequestMapping ("/ consumer/add") public boolean add (String dname) {Dept dept = new Dept (); dept.setDname (dname); System.out.println (dept); / / the path of the request, the parameter passed, the returned object return template.postForObject (url+ "/ dev/add", dept,Boolean.class) } @ RequestMapping ("/ consumer/list") public List list () {/ / path of the request, returned object return template.postForObject (url+ "/ dev/list", void.class,List.class);}}

Feign

@ RestControllerpublic class ConsumerController {@ Autowired private DeptClientService service; @ RequestMapping ("/ consumer/get/ {id}") public Dept getByID (@ PathVariable ("id") Long id) {return this.service.queryByID (id);} @ RequestMapping ("/ consumer/add") public boolean add (String dname) {Dept dept = new Dept (); dept.setDname (dname); return this.service.add (dept) } @ RequestMapping ("/ consumer/list") public List list () {return this.service.queryAll ();}} Thank you for reading. The above is the content of "how to use Feign in Spring Cloud Micro Service Architecture". After the study of this article, I believe you have a deeper understanding of how to use Feign in Spring Cloud Micro Service Architecture, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Services paths architectures equilibrium objects interfaces learning content customers clients consistency between parameters available in basic that is ideas situations providers articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS NVidia Docker Huawei