概述
什么是Feign豁翎?與 Ribbon ?樣角骤,F(xiàn)eign 也是由 Netflflix 提供的,F(xiàn)eign 是?個(gè)聲明式心剥、模版化的 Web Service 客戶端启搂,它簡化了開發(fā)者編寫 Web 服務(wù)客戶端的操作,開發(fā)者可以通過簡單的接?和注解來調(diào)? HTTP API刘陶, Spring Cloud Feign胳赌,它整合了 Ribbon 和 Hystrix,具有可插拔匙隔、基于注解疑苫、負(fù)載均衡、服務(wù)熔斷等?系列便捷功能。
特點(diǎn)
(1)Feign 是?個(gè)聲明式的 Web Service 客戶端捍掺;
(2)?持 Feign 注解撼短、Spring MVC 注解、JAX-RS 注解挺勿;
(3)Feign 基于 Ribbon 實(shí)現(xiàn)曲横,使?起來更加簡單;
(4)Feign 集成了 Hystrix不瓶,具備服務(wù)熔斷降級(jí)的功能禾嫉。
示例
1.首先創(chuàng)建服務(wù)端項(xiàng)目,提供數(shù)據(jù)接口蚊丐。
1.jpg
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
application.yaml
server:
port: 8081
servlet:
context-path: /api/server
spring:
application:
name: userService
User
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
UserController
@RestController
public class UserController {
@GetMapping("/getUserInfo")
public User getUserInfo(){
User user = new User();
user.setId(1L);
user.setName("BanQ");
user.setAge(21);
return user;
}
}
啟動(dòng)類
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
}
}
瀏覽器訪問:http://localhost:8081/api/server/getUserInfo
2.jpg
2.創(chuàng)建客戶端項(xiàng)目熙参,調(diào)用服務(wù)端接口请毛。
3.jpg
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
application.yaml
server:
port: 8082
servlet:
context-path: /api/client
UserFeignClient
@FeignClient(
name = "userService",
url = "http://localhost:8081/api/server"
)
public interface UserFeignClient {
@RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
@ResponseBody
String getUserInfo();
}
TestController
@RestController
public class TestController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/get")
public String get(){
return userFeignClient.getUserInfo();
}
}
啟動(dòng)類
@EnableFeignClients
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
}
瀏覽器訪問:http://localhost:8082/api/client/get
4.jpg
這樣就實(shí)現(xiàn)了通過feign client來調(diào)用遠(yuǎn)程(第三方)接口了澜术。