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
首次通路:
重新整理