Spring Cloud Feign is a declarative service invocation client based on Netflix Feign implementation. It makes it easier to write Web service clients. We only need to create an interface and configure it with annotations to bind to the Web service interface. It has pluggable annotation support, including Feign annotations and JAX-RS annotations. It also supports pluggable encoders and decoders. Spring Cloud Feign also extends support for Spring MVC annotations and integrates Ribbon and Eureka to provide load balancing HTTP client implementations.
1. pom.xml, there are different dependency management changes, the previous run is not smooth, rely on the red mark for modification.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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> <artifactId>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: Through @EnableFeignClients Annotation opens the function of scanning Spring Cloud Feign client
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 FeignClient under com.example.demo.service
@EnableFeignClients(basePackages={"com.example.demo.service"})
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
3,ClientFeignController
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.IFeign; @RestController public class ClientFeignController { @Autowired IFeign iFeign; @GetMapping("/consumer") public String all() { // Launch REST request return iFeign.all(); } }
4. IFeign A FeignClient Declares Information for Service Providers
package com.example.demo.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
//Declare the service provider name @FeignClient("eurekaClient") public interface IFeign {
//Specify the name of the interface to consume // @GetMapping("/consumer") @RequestMapping(value="/all",method={RequestMethod.GET}) String all(); }
More configurable than the previous one using Ribbon@LoadBalanced
If you use the Dalston version, FeignClient will report java.lang.NoClassDefFoundError: feign/Feign$Builder using GetMapping. Many online searches have not found the reason yet, so it will change to Camden version.