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

The method of declarative Service calling Feign in Spring Cloud

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the method of declarative service calling Feign in Spring Cloud". In daily operation, I believe many people have doubts about the method of declarative service calling Feign in Spring Cloud. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "declarative service invocation method of Feign in Spring Cloud". Next, please follow the editor to study!

Setting up the entry environment for Spring Cloud Feign

OK, first of all, let's create a Spring Cloud Feign project through the following six steps to experience the convenience that Spring Cloud Feign brings to us.

Step 1: create an ordinary Spring Boot project

First, let's create a normal Spring Boot project, named feign-consumer.

Step 2: add dependencies

The main dependencies to be added here are spring-cloud-starter-eureka and spring-cloud-starter-feign, as follows:

Org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE Dalston.SR3 org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-feign org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} Step 3 of pom import: add comments

Then add @ EnableFeignClients annotation to the entry class of the project to enable Spring Cloud Feign support, as follows:

@ SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class FeignConsumerApplication {public static void main (String [] args) {SpringApplication.run (FeignConsumerApplication.class, args);} step 4: declare the service

Define a HelloService interface, specify the service name through the @ FeignClient annotation to bind the service, and then bind the interface provided by the service provider through the annotation provided in SpringMVC, as follows:

FeignClient ("hello-service") public interface HelloService {@ RequestMapping ("/ hello") String hello ();}

This is equivalent to binding the / hello interface provided by a service provider called hello-service (hello-service case doesn't matter here). Let's take a look at the interface provided by my service provider, as follows:

@ RequestMapping (value = "/ hello", method = RequestMethod.GET) public String hello () {return "hello";} step 5: invoke the service in Controller

Next, create a Controller to invoke the above service, as follows:

@ RestControllerpublic class FeignConsumerController {@ Autowired HelloService helloService; @ RequestMapping ("/ hello") public String hello () {return helloService.hello ();}} step 6: property configuration

Finally, we need to specify the service registry in application.properties and configure the port number, etc., as follows:

Spring.application.name=feign-consumerserver.port=2005eureka.client.service-url.defaultZone= http://localhost:1111/eureka/ Test

After doing the above, start eureka-server, provider and feign-consumer in turn, and then visit the following address: http://localhost:2005/hello. The access result is as follows:

Both Ribbon and Hystrix have functions, but it is easier to implement with Feign. Feign uses a more elegant way to invoke the interface of the service provider, avoiding us from writing templated RestTemplate code.

Parameter transfer

Above we looked at a simple call case, this example does not involve the passing of parameters, so let's take a look at how to pass parameters.

First, I'll add three test interfaces to my service provider, as follows:

@ RequestMapping (value = "/ hello1", method = RequestMethod.GET) public String hello1 (@ RequestParam String name) {return "hello" + name + "!";} @ RequestMapping (value = "/ hello2", method = RequestMethod.GET) public Book hello2 (@ RequestHeader String name, @ RequestHeader String author, @ RequestHeader Integer price) throws UnsupportedEncodingException {Book book = new Book (); book.setName (URLDecoder.decode (name, "UTF-8")); book.setAuthor (URLDecoder.decode (author, "UTF-8") Book.setPrice (price); System.out.println (book); return book;} @ RequestMapping (value = "/ hello3", method = RequestMethod.POST) public String hello3 (@ RequestBody Book book) {return "title:" + book.getName () + "; author:" + book.getAuthor ();}

The hello1 API mainly receives a parameter of type String, which is passed in the form of key-value, and then returns a data of type String; API hello2 receives that the parameter is carried in the request header, and the Chinese character will be garbled in the request header, so it is necessary to encode and decode first (of course, it is not so troublesome if the English is passed), and then return a Book object; hello3 receives a Book object and returns a string.

After the test interface is written, let's take a look at how to write the HelloService interface in the feign-consumer project, as follows:

@ FeignClient ("hello-service") public interface HelloService {@ RequestMapping ("/ hello") String hello (); @ RequestMapping (value = "/ hello1", method = RequestMethod.GET) String hello (@ RequestParam ("name") String name); @ RequestMapping (value = "/ hello2", method = RequestMethod.GET) Book hello (@ RequestHeader ("name") String name, @ RequestHeader ("author") String author, @ RequestHeader ("price") Integer price) @ RequestMapping (value = "/ hello3", method = RequestMethod.POST) String hello (@ RequestBody Book book);}

Here is a detail for friends to pay attention to. In SpringMVC, @ RequestParam and @ RequestHeader are annotated. If we do not specify value, the parameter name is used as its value by default, but in Feign, this value must be specified clearly, otherwise an error will be reported.

Finally, add the test interface as follows:

@ RestControllerpublic class FeignConsumerController {@ Autowired HelloService helloService; @ RequestMapping ("/ hello") public String hello () {return helloService.hello ();} @ RequestMapping ("/ hello1") public String hello1 () {return helloService.hello ("Zhang San") } @ RequestMapping (value = "/ hello2") public Book hello2 () throws UnsupportedEncodingException {Book book = helloService.hello (URLEncoder.encode (Romance of the three Kingdoms, "UTF-8"), URLEncoder.encode ("Luo Guanzhong", "UTF-8"), 33); System.out.println (book); return book;} @ RequestMapping ("/ hello3") public String hello3 () {Book book = new Book () Book.setName (Dream of Red Mansions); book.setPrice (44); book.setAuthor (Cao Xueqin); return helloService.hello (book);}}

The running results are as follows:

Http://localhost:2005/hello1:

Http://localhost:2005/hello2:

Http://localhost:2005/hello3:

At this point, the study on "the method of declarative service invocation of Feign in Spring Cloud" 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.

Share To

Internet Technology

Wechat

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

12
Report