In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Feign in Spring Cloud". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Feign in Spring Cloud.
Create a public interface
First, let's create a common maven project called hello-service-api. Since we are going to use SpringMVC annotations in this project, we need to add spring-boot-starter-web dependencies after the creation is successful, as follows:
Org.springframework.boot spring-boot-starter-web org.sang commons 1.0-SNAPSHOT
Commons is my other maven project, which provides the Book class, because my hello-service-api will use the Book class later, of course, you can also define a Book class directly in the hello-service-api.
After the project is created, we provide a HelloSerivce interface in the project, as follows:
@ RequestMapping ("/ hs2") public interface HelloService {@ 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) throws UnsupportedEncodingException @ RequestMapping (value = "/ hello3", method = RequestMethod.POST) String hello (@ RequestBody Book book);}
Do the friends look familiar? This is what we defined in HelloService above. To distinguish it from the HelloService above, I narrowed the request this time, defining the prefix / hs2 for the request.
Implement the interface in the service provider
After the hello-service-api project is written, we add the dependency on the hello-service-api project in the service provider, as follows:
Org.sang hello-service-api 0.0.1-SNAPSHOT
Then create the BookController2 implementation HelloService interface in the service provider, as follows:
RestControllerpublic class BookController2 implements HelloService {@ Override public String hello (@ RequestParam ("name") String name) {return "hello" + name + "!";} @ Override public Book hello (@ RequestHeader ("name") String name, @ RequestHeader ("author") String author, @ RequestHeader ("price") Integer price) throws UnsupportedEncodingException {org.sang.Book book = new org.sang.Book (); book.setName (URLDecoder.decode (name, "UTF-8")) Book.setAuthor (URLDecoder.decode (author, "UTF-8"); book.setPrice (price); System.out.println (book); return book;} @ Override public String hello (@ RequestBody Book book) {return "title:" + book.getName () + "; author:" + book.getAuthor ();}} "
Of course, if you implement the HelloService interface, you have to implement the methods in the HelloService interface, and the implementation of the method is the same as above. The difference is that I don't need to add @ RequestMapping annotations to the method here, all of which are in the parent interface, but you still need to add @ RestController annotations on Controller. In addition, you need to note that the parameters @ RequestHeader and @ RequestBody annotations in the method still need to be added, while the @ RequestParam annotations may not be added.
Inherit interface in service consumer
After writing about the service provider, let's take a look at the service consumer. First, add the dependency on hello-service-api in the service consumer, and then create a new HelloService2 class that inherits the HelloService interface in hello-service-api, as follows:
@ FeignClient ("hello-service") public interface HelloService2 extends HelloService {}
There is no need to add any methods to this interface, the methods are all in the parent interface, and here you just need to add @ FeignClient ("hello-service") annotation to the class to bind the service.
Then provide the corresponding test interface in Controller, as follows:
RestControllerpublic class FeignConsumerController {@ Autowired HelloService2 helloService2; @ RequestMapping ("/ hello1") public String hello1 () {return helloService2.hello ("Zhang San");} @ RequestMapping (value = "/ hello2") public Book hello2 () throws UnsupportedEncodingException {Book book = helloService2.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 (A Dream of Red Mansions); book.setPrice (44); book.setAuthor (Cao Xueqin); return helloService2.hello (book) }} at this point, I believe you have a deeper understanding of "how to use Feign in Spring Cloud". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.