Learn a little springcloud every day. (iv): Use of feign and custom configuration

Source: Internet
Author: User

Feign:springcloud's official website defines it like this:

is a declarative Web service client. It supports annotations of the feign itself, jax-rs annotations, and SPRINGMVC annotations. Spring Cloud integrates the Ribbon and Eureka to provide load-balanced HTTP clients when using feign.

Use of feign

Let's start with a look at how to use feign in a project

    1. First of all, we put the original Cloud-demo-consumer project copy, Artifactid to Cloud-demo-consumer-feign, and then add the dependency (warm reminder, have not seen the previous articles of friends, It is recommended to start with the first article to learn better. Learn Springcloud every day (a): Integrate Springcloud with SpringBoot2.0.3)

      <dependency>

      <groupId>org.springframework.cloud</groupId>

      Spring-cloud-starter-openfeign

The 2.CloudDemoConsumerApplication startup class is renamed Clouddemoconsumerfeignapplication, deleting the last used Resttemplate, and add annotation @enablefeignclients to the class to indicate that this item is feign client

@SpringBootApplicationbr/> @EnableEurekaClient

public class Clouddemoconsumerfeignapplication {

public static void Main (string[] args) {
Springapplication.run (Clouddemoconsumerfeignapplication.class, args);
}
}

3. Add the feign package under the Cn.org.zhixiang package and then the Userfeignclient interface:

@FeignClient ("Provider-demo")
Public interface Userfeignclient {

@GetMapping (value = "/user/getuser/{id}")
Public User GetUser (@PathVariable ("id") Long ID);
}

This interface, is the key to our use of feign, we first look at the @feignclient ("Provider-demo") annotations, it is in the parentheses are not familiar with the content, this is not the name of our service provider, Yes, it's actually the spring.application.name of the service we need to invoke.

Followed by @requestmapping (value = "/user/getuser/{id}", method = Requestmethod.get)

@PathVariable ("id"), this note is also written on the value of it, I remember not written Ah, in fact, this is a hole, if not write the words are not up drip.

4. Then we look at the Usercontroller class, just now we have resttemple beans to kill, then in this flatly did not use it in the truth.

What we're going to use now is the Userfeignclient interface we just defined.

@RestControllerbr/> @RequestMapping ("/user")
< p="">

@Autowired
Private Userfeignclient userfeignclient;

@GetMapping ("/getuser/{id}")
Public User GetUser (@PathVariable Long ID) {
return Userfeignclient.getuser (ID);
}
}

5 Now we can test, start the Eureka service, and then randomly launch one of the two service providers we have defined, and finally start our project.

Browser access HTTP://LOCALHOST:8089/USER/GETUSER/1, do not know that you have access to the data?

Modify the default configuration for feign

Modify the default configuration of feign in fact, there is also the problem of packet scanning, because the previous article (custom Eureka cluster load Balancing strategy) has been said, here is not a waste of time, have not seen the students can first refer to.

Here we have a simple and convenient, put the configuration file to Componentscan scanning place: Cn.org.config, here, we have new configuration class

@org. springframework.context.annotation.Configuration
public class Configuration {br/> @Bean

return new Contract.default ();
}
}

Then specify the configuration in the Userfeignclient class

@FeignClient (value = "Provider-demo", configuration = Configuration.class)
Note that we have modified the feign contract in this class, so what is contract? It's called a contract. Because feign first used the contract is SPRINGMVC, so we springmvc the annotations of the time the direct success, but if you start the project now you will find that has not been started. Because the contract used by Contract.default () is feign own, that is to say, we need to modify SPRINGMVC annotations to feign annotations.

SPRINGMVC version

@GetMapping (value = "/user/getuser/{id}")
Public User GetUser (@PathVariable ("id") Long ID);
Feign version

@RequestLine ("Get/user/getuser/{id}")
Public User GetUser (@Param ("id") Long ID);

When you learn to modify the feign's contract, the other default is the same.

Github:https://github.com/2388386839/spring-cloud-demo

Code Cloud: Https://gitee.com/zhixiang_blog/spring-cloud-demo

If you have any help, please remember to help a star oh.

This article is from HTTPS://ZHIXIANG.ORG.CN/#/BLOG/READ/9886ACC8-96FA-4AF0-9AB4-A99721474F51, reproduced please retain.

Learn a little springcloud every day. (iv): Use of feign and custom configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.