In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to consume services through Feign". In daily operation, I believe many people have doubts about how to consume services through Feign. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to consume services through Feign". Next, please follow the editor to study!
A brief 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 annotations and can use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign default integration
With Ribbon, you can add requests to understand the springcloud architecture: 3536247259, and combined with Eureka, the load balancing effect is achieved by default.
In short:
Feign uses interface-based annotations
Feign integrates ribbon and has the ability of load balancing.
Integrated with Hystrix, with the ability of fusing
II. Preparatory work
Continue to use the project in the previous section, start eureka-server with port 8761, and start service-hi twice with ports 8762 and 8773, respectively.
Third, create a feign service
Create a new spring-boot project, named serice-feign, and the start of introducing Feign into its pom file depends on spring-cloud-starter-feign,
The start of Eureka depends on spring-cloud-starter-netflix-eureka-client and the start of Web depends on spring-boot-starter-web. The code is as follows:
4.0.0 com.forezp service-feign 0.0.1-SNAPSHOT jar service-feign Demo project for Spring Boot com.forezp sc-f-chapter3 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot- Starter-web org.springframework.cloud spring-cloud-starter-openfeign
In the project configuration file application.yml file, specify that the program name is service-feign, the port number is 8765, and the service registration address is http://localhost:8761/eureka/. The code is as follows:
Eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8765spring: application: name: service-feign
In the startup class ServiceFeignApplication of the program, add the @ EnableFeignClients annotation to enable the Feign function:
@ SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@EnableFeignClientspublic class ServiceFeignApplication {public static void main (String [] args) {SpringApplication.run (ServiceFeignApplication.class, args);}}
Define a feign interface that specifies which service to invoke through @ FeignClient ("service name"). For example, the "/ hi" interface of the service-hi service is called in the code 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, expose a "/ hi" API interface to consume services through the Feign client SchedualServiceHi defined above. The code is as follows:
@ RestControllerpublic class HiController {/ / compiler reported an error, ignoring it. Because this Bean is injected when the program starts, the compiler is not aware of it, so an error is reported. @ Autowired SchedualServiceHi schedualServiceHi; @ GetMapping (value = "/ hi") public String sayHi (@ RequestParam String name) {return schedualServiceHi.sayHiFromClientOne (name);}}
Start the program and visit the http://localhost:8765/hi?name=forezp, browser many times to display alternately:
Hi forezp,i am from port:8762
Hi forezp,i am from port:8763
At this point, the study on "how to consume services through Feign" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.