一、简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Spring Cloud Feign 基于Netflix Feign 实现的,整合了Spring Cloud Ribbon 与 Spring Cloud Hystrix,并且实现了声明式的Web服务客户端定义方式。
简而言之:
-
Feign 采用的是基于接口的注解
-
Feign 整合了ribbon
二、创建一个feign的服务
创建完pom文件修改如下
<?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.vesus</groupId> <artifactId>springcloud-fegin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-fegin</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.vesus</groupId> <artifactId>springcloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- eureka:微服务注册 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--fegin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、创建application.yml配置文件,增加注册中心地址
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ # 注册中心地址 spring: application: name: spring-cloud-fegin server: port: 8764
3、定义一个接口HelloService,通过@ FeignClient(“服务名”),来指定调用哪个服务。
package com.vesus.springcloudfegin.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "spring-cloud-service") //springcloud-service中定义的服务的名字 public interface HelloService { @RequestMapping(value = "/hello") //springcloud-service中定义的服务方法 public String sayHello() ; }
4、加入控制器HelloWordController
package com.vesus.springcloudfegin.controller; import com.vesus.springcloudfegin.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired HelloService helloService ; @RequestMapping(value = "/hello") public String sayHello(){ return helloService.sayHello() ; } }
5、入口方法加入@EnableFeignClients,增加fegin支持。
package com.vesus.springcloudfegin; 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; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SpringcloudFeginApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudFeginApplication.class, args); } }
启动工程,访问 http://localhost:8764/hello ,显示hello world 。
代码 :https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-fegin