what is feign?
feign是一个http调用的轻量级框架。可以以java接口的方式调用http请求。简化了传统的自己封装构造对象发送http请求调用service。通过使用注解将请求模板化当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。
github源码地址:https://github.com/Jacwo/feign-customer.git
开始
- 使用idea新建一个maven项目 可以使用spring Initializr
- 替换pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yyl</groupId> <artifactId>feign-customer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feign-customer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Dalston.SR5</version> <relativePath/> </parent> <dependencies> <!-- Hystrix,Feign是基于Hystrix的--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!-- Eureka依赖,连接注册中心的都需要有这个依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Feign依赖,声明式开发 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- SpringMVC依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
- 编写启动类
package com.yyl.feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableFeignClients @SpringBootApplication public class FeignCustomerApplication { public static void main(String[] args) { SpringApplication.run(FeignCustomerApplication.class, args); } }
- 编写controller
package com.yyl.feign.controller; import com.yyl.feign.api.HelloApi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * author:yangyuanliang Date:2019-08-20 Time:13:43 **/ @Controller @RequestMapping("feign") public class HelloController { @Autowired private HelloApi helloApi; @GetMapping("/hello") @ResponseBody public String sayHello(){ return helloApi.hello(); } }
- 编写接口 @FeignClient注解当扫描到该注解的时候,会为该接口生成一个动态代理类。在代理类中会根据@RequestMapping组合成请求的url路径。在加上eureka的ribbon自动负载均衡寻找真正的服务地址和url拼接。封装http发送请求。返回相应结构
package com.yyl.feign.api; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * author:yangyuanliang Date:2019-08-20 Time:13:42 * * @author yangyuanliang*/ @FeignClient(value = "hello-service") public interface HelloApi { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello(); }
启动测试:
-
1.启动注册中心eureka-server
-
2.启动服务提供者eureka-server-provider
-
3.启动feign消费端 eureka-customer
-
4.打开浏览器访问 http://localhost:9001/feign/hello
-