1.
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。
使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。
Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果
2.简单理解:
·Feign 采用的是基于接口的注解
·Feign 整合了ribbon
在学习前请确认已创建好一个服务注册中心和两个调用方:
服务注册中心创建:https://blog.csdn.net/quguoxun/article/details/111030810
调用方创建:https://blog.csdn.net/quguoxun/article/details/111034298
复制创建好的调用方client-01创建client-02并修改application.properties中端口为9092并启动
spring.application.name不修改,仍为client-01:
由于服务之间是根据此名称进行相互调用,所以此时表
示
9091,9092对外提供一个服务,
服务名为client-01相当于一个小的集群
创建server-feign工程
在application.properties中添加相关配置,端口号为9094,服务名为server-feign
#Eureka服务注册中心的访问地址
eureka.client.serviceUrl.defaultZone=http://localhost:9090/eureka/
#client服务的端口
server.port=9094
#clinet服务的服务名称
spring.application.name=server-feign
在pom.xml文件中添加feign依赖,这里最好添加两个依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.joyin</groupId>
<artifactId>server-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0-M6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!--高版本feign依赖============================-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
手动添加时需要进行加载,否则会报错,在pom.xml文件中,右击鼠标-->Maven-->Reimport
启动类中加入@EnableFeignClients注解开启Feign的功能(@EnableDiscoveryClient和@EnableFeignClients)
package com.joyin.serverfeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ServerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServerFeignApplication.class, args);
}
}
编写测试类service、controller和serverimp,feign基于接口——创建FeignTest
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
private FeignService feignservice;
@RequestMapping(value = "FeignTest")
public String FeignTest(@RequestParam String message){
return feignservice.FeignTest(message);
}
}
package com.joyin.serverfeign.Service;
import com.joyin.serverfeign.ServiceImp.FeignServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Service
@FeignClient(value = "client-01", fallback = FeignServiceImpl.class)
public interface FeignService {
@RequestMapping("/ClientTest") //此处名称为client-01服务中对应的方法名
String FeignTest(@RequestParam(value = "message") String message);
}
package com.joyin.serverfeign.ServiceImp;
import com.joyin.serverfeign.Service.FeignService;
public class FeignServiceImpl implements FeignService{
@Override
public String FeignTest(String message) {
return "Feign 负载均衡测试"+message;
}
}
配置完成后启动项目,保证服务和端口都启动成功:
在浏览器中刷新地址:http://localhost:9094/FeignTest?message="Feign”,浏览器交替显示:“"Feign"端口号:9092”,“"Feign"端口号:9091”;
出现上图效果说明Feign负载均衡配置完成,需要的小伙伴不妨动动小手进行自主操作!!