一、代碼示例
說(shuō)明:此處使用的SpringBoot版本為2.1.13.RELEASE,SpringCloud版本為Greenwich.SR5
Feign可以理解為聲明式調(diào)用REST方式船惨。
此處就不再貼server與client的代碼
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2缕陕、application.yml
server:
port: 9003
spring:
application:
name: feign
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: feign-9003
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
app:
name: feign-9003
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3粱锐、啟動(dòng)類
啟動(dòng)類增加注解@EnableFeignClients啟動(dòng)Feign
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class Feign9003Application {
public static void main(String[] args) {
SpringApplication.run(Feign9003Application.class,args);
}
}
4、其他java類
Feign對(duì)應(yīng)的接口類
定義一個(gè)接口扛邑,增加@FeignClient(value = "CLIENT")怜浅,@FeignClient對(duì)應(yīng)的VALUE為需要調(diào)用的服務(wù)名,接口中的方法與需要調(diào)用服務(wù)的方法一致并增加@GetMapping("/hello/{name}")(根據(jù)調(diào)用的方式選擇)
@FeignClient會(huì)將接口注入到容器
package org.example.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "CLIENT")
public interface FeignService {
@GetMapping("/hello/{name}")
public String hello(@PathVariable(value = "name") String name);
}
Controller類
注入前面定義的Feign接口類
package org.example.controller;
import org.example.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private FeignService feignService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return feignService.hello(name);
}
}
2鹿榜、測(cè)試驗(yàn)證
先后啟動(dòng)server海雪、client、feign對(duì)應(yīng)的服務(wù)舱殿,訪問(wèn)http://localhost:7001/
image.png
訪問(wèn)http://localhost:9003/hello/zs
交替出現(xiàn)
image.png
image.png
說(shuō)明Feign默認(rèn)集成了負(fù)載均衡奥裸,若想修改負(fù)載均衡策略,像容器注入IRule即可沪袭,具體見(jiàn)上一文章湾宙。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽(yáng)老師的視頻