Spring Cloud Introductory Eureka-consumer service consumption (declarative feign) (iii)

Source: Internet
Author: User

Spring Cloud Feign is a set of declarative service invocation clients based on the Netflix feign implementation. It makes writing Web service clients easier. We only need to configure it by creating an interface and using annotations to complete the binding to the Web service interface. It features pluggable annotation support, including feign annotations, Jax-rs annotations. It also supports pluggable encoders and decoders. Spring Cloud Feign also extends support for spring MVC annotations, while also consolidating the ribbon and Eureka to provide an HTTP client implementation of a balanced load.

1, Pom.xml, here are different dependencymanagement there are changes, before the run does not pass, relying on the red place for modification

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelversion>4.0.0</modelversion>    <groupId>Com.example</groupId>    <Artifactid>Eurekaribbon</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Eurekaribbon</name>    <Description>Demo Project for Spring Boot</Description>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.4.0.RELEASE</version>        <RelativePath/> <!--Lookup parent from repository -    </Parent>    <Properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </Properties>    <Dependencies>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-eureka</Artifactid>        </Dependency>  <dependency> <groupId>org.springframework.cloud</groupId> <artifacti D>spring-cloud-starter-feign</artifactid> </dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>    </Dependencies>    <dependencymanagement>        <Dependencies>            <Dependency>                <groupId>Org.springframework.cloud</groupId>                <Artifactid>Spring-cloud-dependencies</Artifactid><!--<version>Dalston.SR3</version> -                <version>Camden.SR4</version>                <type>Pom</type>                <Scope>Import</Scope>            </Dependency>        </Dependencies>    </dependencymanagement>    <Build>        <Plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <Artifactid>Spring-boot-maven-plugin</Artifactid>            </plugin>        </Plugins>    </Build></Project>

2. Main class: @EnableFeignClients enable scanning of spring Cloud feign client via annotations

Package Com.example.demo;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.client.discovery.enablediscoveryclient;import org.springframework.cloud.netflix.feign.EnableFeignClients;
Scan Com.example.demo.service under Feignclient@enablefeignclients (basepackages={"Com.example.demo.service"}) @ Enablediscoveryclient@springbootapplicationpublic class Eurekafeignapplication {public static void main (string[ ] args) { springapplication.run (eurekafeignapplication.class, args);} }

3, Clientfeigncontroller

 PackageCom.example.demo.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController;Importcom.example.demo.service.IFeign; @RestController Public classClientfeigncontroller {@Autowired ifeign ifeign; @GetMapping ("/consumer")     PublicString All () {//initiate a REST request        returnIfeign.all (); }    }

4, ifeign a feignclient to declare the service provider's information

 PackageCom.example.demo.service;Importorg.springframework.cloud.netflix.feign.FeignClient;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;
Claims service provider name @feignclient ("Eurekaclient") Public InterfaceIfeign {
Specify the name of the interface to consume//@GetMapping ("/consumer") @RequestMapping (value= "/all", method= {requestmethod.get}) String all ();}

More configurable than the previous article using the Ribbon @LoadBalanced

If the use of Dalston version, feignclient use getmapping will be reported java.lang.noclassdeffounderror:feign/feign$builder, online check a lot of temporarily did not find the reason, therefore Change to version Camden.

Spring Cloud Getting Started Eureka-consumer service consumption (declarative feign) (c)

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.