关于Feign是什么的问题可以参考以下:
https://www.jianshu.com/p/94177e224ef8
https://www.cnblogs.com/756623607-zhang/p/8661571.html
后边介绍Fein中有依赖Eureka和Ribbon可以看这些博客
1.版本:
SpringBoot:2.2.5.RELEASE
Spring Cloud:Hoxton.SR1
2.项目结构
共4个服务:feign-web-api、feign-service-a、feign-service-b、feign-service-c
3.parent依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.yarm</groupId>
<artifactId>feign</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>service-a</module>
<module>service-b</module>
<module>service-c</module>
<module>web-api</module>
</modules>
<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.boot</groupId>
<artifactId>spring-boot-starter</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3.其余模块依赖类似、如下:
<?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">
<parent>
<artifactId>feign</artifactId>
<groupId>com.yarm</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>web-api</artifactId>
<name>web-api</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. 启动函数
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class WebApiApplication {
public static void main(String[] args) {
SpringApplication.run(WebApiApplication.class, args);
}
}
说明:
@EnableFeignClients // 表示feign客户端
5.配置文件,几个服务都类似
server.port=7000
spring.application.name=feign-web-api
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 开启健康检查
eureka.client.healthcheck.enabled=true
# eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance
eureka.instance.lease-renewal-interval-in-seconds=3
# eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance
eureka.instance.lease-expiration-duration-in-seconds=6
6.服务调用链
feign-web-api-->feign-service-a-->feign-service-b-->feign-service-c
7. 以feign-web-api调用feign-service-a举例
7.1 feign-service-a服务提供服务,和springmvc方式一样,加controller:
package com.yarm.controller;
import com.yarm.service.FeignServiceB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping
@RestController
public class OpneController {
@Autowired
FeignServiceB feignServiceB;
/**
* @Description :对外提供服务接口
* @Author : yarm.yang
* @Date : 2020/3/25 14:19
* @Return :
*/
@RequestMapping("toA")
public String open(){
String result = "feign-service-a";
String s = feignServiceB.toB();
return result + "-->" + s;
}
}
7.2 feign-web-api作为消费者需新建接口,加上注解@FeignClient("feign-service-a"),其中feign-service-a表示要调用的服务。
package com.yarm.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description :调用服务feign-service-a
* @Author : yarm.yang
* @Date : 2020/3/25 14:15
*/
@FeignClient("feign-service-a")
public interface FeignServiceA {
@RequestMapping("toA")
String toA();
}
7.3 注入FeignServiceA接口就可以直接调用feign-service-a的接口了
package com.yarm.controller;
import com.yarm.service.FeignServiceA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("api")
public class ConsumerController {
@Autowired
FeignServiceA feignServiceA;
@RequestMapping("getLink")
public String get(){
String result = "feign-web-api";
// 调用服务feign-service-a
String s = feignServiceA.toA();
return result + "-->" + s;
}
}
其他的服务类似,调用feign-web-api接口
http://127.0.0.1:7000/api/getLink
输出:
8.代码地址