一、代码示例
说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
Feign可以理解为声明式调用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、启动类
启动类增加注解@EnableFeignClients启动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对应的接口类
定义一个接口,增加@FeignClient(value = "CLIENT"),@FeignClient对应的VALUE为需要调用的服务名,接口中的方法与需要调用服务的方法一致并增加@GetMapping("/hello/{name}")(根据调用的方式选择)
@FeignClient会将接口注入到容器
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、测试验证
先后启动server、client、feign对应的服务,访问http://localhost:7001/
访问http://localhost:9003/hello/zs
交替出现
说明Feign默认集成了负载均衡,若想修改负载均衡策略,像容器注入IRule即可,具体见上一文章。
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/
还有尚硅谷周阳老师的视频