目錄:Spring Cloud Alibaba 系列教程
上一篇:5.Spring Cloud Alibaba 創(chuàng)建服務(wù)消費(fèi)者
下一篇:7.Spring Cloud Alibaba 熔斷(Sentinel)
概述
Feign 是一個(gè)聲明式的偽 Http 客戶端虐拓,它使得寫 Http 客戶端變得更簡(jiǎn)單笼痛。使用 Feign仇箱,只需要?jiǎng)?chuàng)建一個(gè)接口并注解。它具有可插拔的注解特性偶翅,可使用 Feign 注解和 JAX-RS 注解贯底。Feign 支持可插拔的編碼器和解碼器棘幸。Feign 默認(rèn)集成了 Ribbon,Nacos 也很好的兼容了 Feign挽荡,默認(rèn)實(shí)現(xiàn)了負(fù)載均衡的效果
- Feign 采用的是基于接口的注解
- Feign 整合了 ribbon
POM
創(chuàng)建一個(gè)工程名為 hello-spring-cloud-alibaba-nacos-consumer-feign
的服務(wù)消費(fèi)者項(xiàng)目藐石,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.wsl</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-consumer-feign</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-alibaba-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.wsl.consumer.ConsumerFeignApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了 org.springframework.cloud:spring-cloud-starter-openfeign
依賴
Application
通過 @EnableFeignClients
注解開啟 Feign 功能
package com.wsl.hello.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)用哪個(gè)服務(wù)。代碼如下:
package com.wsl.hello.consumer.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "nacos-provider")
public interface FeignService {
@GetMapping(value = "/test/{message}")
String test(@PathVariable("message") String message);
}
Controller
package com.wsl.hello.consumer.feign.controller;
import com.wsl.hello.consumer.feign.service.FeignService;
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 FeignService FeignService;
@GetMapping(value = "/test/hi")
public String test() {
return FeignService.test("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: "*"
目錄結(jié)構(gòu):
啟動(dòng)工程
通過瀏覽器訪問 http://localhost:8848/nacos
定拟,即 Nacos Server 網(wǎng)址
你會(huì)發(fā)現(xiàn)多了一個(gè)名為 nacos-consumer-feign
的服務(wù)
這時(shí)打開 http://localhost:9092/test/hi
于微,你會(huì)在瀏覽器上看到:
Hello Nacos Discovery Hi Feign
測(cè)試負(fù)載均衡
-
idea啟動(dòng)多個(gè)實(shí)例
在這里選√
然后修改yml文件里的端口號(hào)再啟動(dòng)一個(gè)就可以了。 - 啟動(dòng)多個(gè)
consumer-provider
實(shí)例青自,效果圖如下:
- 修改
consumer-provider
項(xiàng)目中的Controller
代碼株依,用于確定負(fù)載均衡生效
package com.wsl.hello.provider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NacosProviderController {
@Value("${server.port}")
private String port;
@GetMapping(value = "/test/{message}")
public String test(@PathVariable String message) {
return "Hello Nacos Discovery " + message + " i am from port " + port;
}
}
在瀏覽器上多次訪問 http://localhost:9092/test/hi
,瀏覽器交替顯示:
Hello Nacos Discovery Hi Feign i am from port 8081
Hello Nacos Discovery Hi Feign i am from port 8082
目錄:Spring Cloud Alibaba 系列教程
上一篇:5.Spring Cloud Alibaba 創(chuàng)建服務(wù)消費(fèi)者
下一篇:7.Spring Cloud Alibaba 熔斷(Sentinel)