7.1分析
7.2操作
7.2.1 common 工程
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
创建远程调用方法的接口:
[Java] 纯文本查看 复制代码 @FeignClient("study-provider")
public interface EmployeeRemoteService {
@RequestMapping("/provider/get/employee/remote")
public Employee getEmployeeRemote();
}
7.2.2 新建 Feign-consumer 工程
加入如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.study.spring.cloud</groupId>
<artifactId>spring-cloud-study-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
主启动类com.study.spring.cloud.AtzytMainType:
[Asm] 纯文本查看 复制代码 // 启用 Feign 客户端功能
@EnableFeignClients
@SpringBootApplication
public class AtzytMainType {
public static void main(String[] args) {
SpringApplication.run(AtzytMainType.class, args);
}
}
application.yml:
handler 类com.study.spring.cloud.hander.EmployeeFeignHander:
[Java] 纯文本查看 复制代码 @RestController
public class EmployeeFeignHander {
@Autowired
private EmployeeRemoteService employeeRemoteService;
@RequestMapping("/feign/consumer/get/emp")
public Employee getEmployeeRemote() {
return employeeRemoteService.getEmployeeRemote();
}
}
测试:
feign-consumer测试
7.3传参测试
接口写法:
[Java] 纯文本查看 复制代码 @RequestMapping("/provider/get/employee/by/id")
public Employee getEmployeeById(@RequestParam("empId") Integer empId);
provider 的 handler 方法必须与接口方法声明一致:
[Java] 纯文本查看 复制代码 @RequestMapping("/provider/get/employee/by/id")
public Employee getEmployeeById(@RequestParam("empId") Integer empId) {
return new Employee(empId, "tom999-", 999.99);
}
测试效果:
测试效果
|