概述
Feign 是一個聲明式的偽 Http 客戶端秤朗,它使得寫 Http 客戶端變得更簡單煤蹭。使用 Feign,只需要創(chuàng)建一個接口并注解取视。它具有可插拔的注解特性硝皂,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的編碼器和解碼器作谭。Feign 默認(rèn)集成了 Ribbon,Nacos 也很好的兼容了 Feign丢早,默認(rèn)實現(xiàn)了負(fù)載均衡的效果
- Feign 采用的是基于接口的注解
- Feign 整合了 ribbon
POM
創(chuàng)建一個工程名為 hello-spring-cloud-alibaba-nacos-consumer-feign
的服務(wù)消費者項目姨裸,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wj</groupId>
<artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-alibaba-nacos-consumer-feign</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-alibaba-nacs-consumer-feign</name>
<description>Demo project for Spring Cloud Alibaba</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wj.hello.spring.cloud.alibaba.nacos.consumer.feign.NacosConsumerFeignApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了 org.springframework.cloud:spring-cloud-starter-openfeign
依賴
Application
通過 @EnableFeignClients
注解開啟 Feign 功能
package com.wj.hello.spring.cloud.alibaba.nacos.consumer.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerFeignApplication.class, args);
}
}
創(chuàng)建 Feign 接口
通過 @FeignClient("服務(wù)名")
注解來指定調(diào)用哪個服務(wù)秧倾。代碼如下:
package com.wj.hello.spring.cloud.alibaba.nacos.consumer.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
// 根據(jù)名稱來通過Nacos查找生產(chǎn)者
@FeignClient(value = "nacos-provider")
public interface NacosProviderServise {
// 生產(chǎn)者中對應(yīng)的接口
@GetMapping(value = "/echo/{message}")
String echo(@PathVariable("message") String message);
}
Controller
package com.wj.hello.spring.cloud.alibaba.nacos.consumer.feign.controller;
import com.wj.hello.spring.cloud.alibaba.nacos.consumer.feign.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NacosConsumerFeignController {
@Autowired
private NacosProviderServise echoService;
@GetMapping(value = "/echo/hi")
public String echo() {
return echoService.echo("Hi Feign");
}
}
application.yml
spring:
application:
name: nacos-consumer-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 9092
management:
endpoints:
web:
exposure:
include: "*"
啟動工程
通過瀏覽器訪問 http://localhost:8848/nacos
,即 Nacos Server 網(wǎng)址
你會發(fā)現(xiàn)多了一個名為 nacos-consumer-feign
的服務(wù)
這時打開 http://localhost:9092/echo/hi
傀缩,你會在瀏覽器上看到:
Hello Nacos Discovery Hi Feign
測試負(fù)載均衡
- idea啟動多個實例
在這里選√
然后修改yml文件里的端口號再啟動一個就可以了那先。
- 啟動多個
consumer-provider
實例,效果圖如下:
- 修改
consumer-provider
項目中的Controller
代碼赡艰,用于確定負(fù)載均衡生效
package com.wj.hello.spring.cloud.alibaba.nacos.provider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@Value("${server.port}")
private String port;
@RestController
public class EchoController {
@GetMapping(value = "/echo/{message}")
public String echo(@PathVariable String message) {
return "Hello Nacos Discovery " + message + " i am from port " + port;
}
}
}
- 在瀏覽器上多次訪問
http://localhost:9092/echo/hi
售淡,瀏覽器交替顯示:
Hello Nacos Discovery Hi Feign i am from port 8081
Hello Nacos Discovery Hi Feign i am from port 8082