Amateur Grass Springcloud Tutorial | Article Three: Service Consumers (feign) (Finchley version)

Source: Internet
Author: User

The previous article, about how to go through the Resttemplate+ribbon to consumer services, this article mainly on how to go through the feign to consumer services.

I. Introduction of 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 can be used with feign annotations and jax-rs annotations. The feign supports pluggable encoders and decoders. The feign integrates the Ribbon by default and combines with Eureka to achieve load balancing by default.

Nutshell:

    • Feign uses an interface-based annotation
    • Feign integrates the ribbon, with load balancing capability
    • Integrated Hystrix, with the ability to fuse
Ii. preparatory work

Continue with the previous section of the project, start the Eureka-server, the port is 8761; Start Service-hi two times with ports of 8762, 8773, respectively.

Third, create a feign service

Create a new Spring-boot project, named Serice-feign, in its pom file introduced feign start dependent spring-cloud-starter-feign, Eureka's start relies on spring-cloud-starter-netflix-eureka-client, the web's start-dependent Spring-boot-starter-web, the code is as follows:

1<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"2xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >3<modelVersion>4.0.0</modelVersion>4  5<groupId>com.forezp</groupId>6<artifactId>service-feign</artifactId>7<version>0.0.1-SNAPSHOT</version>8<packaging>jar</packaging>9  Ten<name>service-feign</name> One<description>demo Project forSpring boot</description> A   -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter3</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.cloud</groupId> at<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> in<dependency> -<groupId>org.springframework.cloud</groupId> to<artifactId>spring-cloud-starter-openfeign</artifactId> +</dependency> -</dependencies> the   *</project>

In the project configuration file Application.yml file, specify the program named Service-feign, the port number is 8765, the service registered address is http://localhost:8761/eureka/, the code is as follows:

1 Eureka: 2   Client:3    serviceurl:4       defaultzone:http://  localhost:8761/eureka/5server:6   port:87657Spring:  8  application:9     name:service-feign

In the program's startup class Servicefeignapplication, plus the @enablefeignclients annotation opens the feign function:

1 @SpringBootApplication2 @EnableEurekaClient3 @EnableDiscoveryClient4 @EnableFeignClients5  Public classservicefeignapplication {6  7      Public Static voidMain (string[] args) {8Springapplication.run (servicefeignapplication.class, args);9     }Ten}

Defines a feign interface that specifies which service to invoke through @ feignclient ("service name"). For example, in the code called the Service-hi Service "/hi" interface, the code is as follows:

1  2 @FeignClient (value = "Service-hi")3publicinterface  Schedualservicehi {4     @RequestMapping (value = "/hi", method = requestmethod.get)5      string Sayhifromclientone (@RequestParam (value = "Name") string name); 6 }

At the controller layer of the Web layer, exposes a "/hi" API interface, consumes the service through the feign client Schedualservicehi defined above. The code is as follows:

1 @RestController2  Public classHicontroller {3  4  5     //compiler error, ignore. Because this bean is injected when the program starts, the compiler does not perceive it, so the error is incorrect. 6 @Autowired7 Schedualservicehi Schedualservicehi;8  9@GetMapping (value = "/hi")Ten      Publicstring Sayhi (@RequestParam string name) { One         returnSchedualservicehi.sayhifromclientone (name); A     } -}

Launch program, multiple access to Http://localhost:8765/hi?name=forezp, browser alternate display:

Hi forezp,i am from port:8762

Hi forezp,i am from port:8763

Feign source code Analysis: http://blog.csdn.net/forezp/article/details/73480304

This article source code download:
Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter3

V. References

This article references the following:

Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Article Three: Service Consumers (feign) (Finchley version)

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.