Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Feign in SpringCloud micro-service architecture

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report