Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
1、pom.xml,这里有所不同dependencyManagement有变化,之前的跑不通,依赖标红处为修改
<?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、主类:通过@EnableFeignClients
注解开启扫描Spring Cloud Feign客户端的功能
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; //扫描com.example.demo.service下的FeignClient @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() { // 发起REST请求 return iFeign.all(); } }
4、IFeign 一个FeignClient 声明 服务提供者的信息
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; //声明服务提供名 @FeignClient("eurekaClient") public interface IFeign { //指定要消费的接口名称 // @GetMapping("/consumer") @RequestMapping(value="/all",method={RequestMethod.GET}) String all(); }
比上篇使用Ribbon @LoadBalanced,更加具有可配置性
如果使用Dalston版,FeignClient使用GetMapping 会报java.lang.NoClassDefFoundError: feign/Feign$Builder,网上查了很多 暂时没有找到原因, 故而 换成Camden版。
原文地址:https://www.cnblogs.com/zwdx/p/9141367.html
时间: 2024-10-29 10:46:38