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 implement Service Consumer Feign by Spring Cloud

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Xiaobian to share with you how Spring Cloud to achieve service consumer Feign, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

I. Introduction to Feign

Feign is a declarative pseudo-Http client that makes it easier to write Http clients. With Feign, you only need to create an interface and annotate it. It has pluggable annotation features that allow the use of Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates Ribbon by default and combines with Eureka to achieve the effect of Load Balancer by default.

In short:

Feign uses interface-based annotations

Feign integrates the ribbon

II. Preparations

Continue with the project in the previous section, start eureka-server on port 8761; start service-hi twice on ports 8762 and 8773.

Create a feign service

Create a new spring-boot project, named serice-feign, and introduce Feign's start-up dependency spring-cloud-starter-feign, Eureka's start-up dependency spring-cloud-starter-eureka, and Web's start-up dependency spring-boot-starter-web into its pom file. The code is as follows:

org.springframework.cloudspring-cloud-starter-eureka

org.springframework.cloudspring-cloud-starter-feign

org.springframework.bootspring-boot-starter-web

In the project configuration file application.yml file, specify the program name as service-feign, port number as 8765, service registration address as http://localhost:8761/eureka/, and the code is as follows:

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8765spring: application: name: service-feign

In the program startup class ServiceFeigApplication, add the @ EnableFeigClients annotation to enable Feign's functionality:

@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); }}

Define a feign interface to specify which service to invoke via @ FeignClient ("service name"). For example, the "/hi" interface of the service-hi service is called in the code, and the code is as follows:

@FeignClient(value = "service-hi")public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name);}

In the controller layer of the Web layer, an API interface of "/hi" is exposed to the outside world, and services are consumed through the Feign client SchedualServiceHi defined above. The code is as follows:

@RestControllerpublic class HiController { @Autowired SchedualServiceHi schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); }}

Start the program and visit http://localhost:8765/hi? several times. name=forezp, browser alternates:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

The above is all the content of "Spring Cloud how to achieve service consumer Feign" this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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

Internet Technology

Wechat

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

12
Report