Feign的介绍:
Feign在微服务中主要用来做一个服务调用与负载均衡,它是在Ribbon的基础上进行的又一次封装。
Feign的远程调用非常的便捷,使用需要服务中心,这里使用简单点的Eureka就行。
直接进入正题
Feign的使用:
创建个mvn父工程:
<?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.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>spring_cloud_feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_feign</name>
<description>Feign入门使用 父工程</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</project>
用spring自带的项目构建创建的父工程
接下来依赖父工程 通过maven创建个Eureka服务中心 server-eureka

创建完项目导下依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Eureka 服务端配置application.yml:
server:
port: 8000
spring:
application:
name: server-eureka
eureka:
client:
fetch-registry: false #服务端不需要去检索服务
register-with-eureka: false #不向服务中心注册自己
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://localhost:8000/eureka/
启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
创建商品微服务 server-goods
导入依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
启动类:
@SpringBootApplication
@EnableEurekaClient
public class GoodsServerApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsServerApplication.class,args);
}
}
设置一下配置文件 application.yml
server:
port: 7001
spring:
application:
name: server-goods
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
创建个假数据 使用订单微服务去调用这个方法:
@RestController
@RequestMapping("/goods")
public class GoodsController {
@GetMapping("/{id}")
public String findById(@PathVariable("id")Integer id) {
return "查询商品业务成功,商品id是:" + id;
}
}
创建个订单微服务 server-order使用feign去调用商品微服务
导入feign的依赖 feign使用需要导入这个 openfeign 的依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
设置配置类 application.yml
server:
port: 7002
spring:
application:
name: server-order
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
设置启动类,添加注解EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class OrderServerApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServerApplication.class,args);
}
}
再创建个feign的接口 调用商品微服务 加上注解 @FeignClient(“server-goods”)
server-goods是商品微服务yml配置中的服务名
方法名称与url以及返回值和商品微服务的方法保持一致
@FeignClient("server-goods")
public interface GoodsFeignClient {
@GetMapping("/goods/{id}")
public String findById(@PathVariable("id")Integer id);
}
接着使用订单微服务方法去调用服务
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private GoodsFeignClient goodsFeignClient;
@GetMapping("/{id}")
public String findGoodsById(@PathVariable("id") Integer id) {
return goodsFeignClient.findById(id);
}
}
@Autowired 导入feign的接口进行调用
开启所有微服务
输入: http://localhost:7002/order/2
这样就实现了微服务的一个远程调用
测试feign的负载均衡:
feign是在ribbon的基础进行的封装, 所有本身就有了负载均衡的功能。默认是轮询机制
商品微服务中application.yml中添加:
testUrl: #获取当前的ip及端口
url: ${spring.cloud.client.ip-address}:${server.port}
获取当前的ip和端口
修改goodsController:
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Value("${testUrl.url}")
private String url;
@GetMapping("/{id}")
public String findById(@PathVariable("id")Integer id) {
return "查询商品业务成功,商品id是:" + id + "商品微服务的url: " + url;
}
}
然后启动分配两个端口 7001 、7011分别进行启动 (复制一个启动类即可)
配置好后启动所有微服务
继续访问: localhost:7002/order/2
首次访问:
刷新