五:Fiegn 声明式接口调用

什么是Fiegn

Netfix,Fiegn 是一个提供模板式的Web Service客户端,使用Fiegn 可以简化Web Service 客户端的编写,开发者可以通过简单的接口和注解来调用HTTP API

Spring Cloud Fiegn :可插拔、基于注解、负载均衡、服务器熔断

只需要创建接口,同时在接口上添加关注注解即可

Fiegn 和Ribbion的区别:

  • Ribbion是一个HTTP客户端工具,Fiegn 是基于Ribbion,更加灵动
  • 支持Fiegn 注解,spring mvc 注解
  • Fiegn 基于Ribbion实现
  • Fiegn 集成了Hystrix

1.创建模块,配置环境

<?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">
<parent>
<artifactId>springCloud01</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>feign</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>

2.配置文件

server:
port: 8050
spring:
application:
name: fegin
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true

3.启动类

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class feignApplication {
public static void main(String[] args) {
SpringApplication.run(feignApplication.class,args);
}
}

4.Handler

package com.southwind.Handler;

import com.southwind.entity.Student;
import com.southwind.fegin.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.Collection; @RestController
@RequestMapping("/feign")
public class FeignHandler {
@Autowired
private FeignProviderClient feignProviderClient; @GetMapping("/findall")
public Collection<Student> findall(){
return feignProviderClient.findall();
}
@GetMapping("/findbyid/{id}")
public Student findbyid(@PathVariable("id") Integer id){
return feignProviderClient.findbyid(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
feignProviderClient.save(student);
}
@DeleteMapping("/delete")
public void deletebyid(Integer id){
feignProviderClient.delete(id);
}
}

5.声明的接口:

package com.southwind.fegin;

import com.southwind.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; import java.util.Collection; @FeignClient(value = "provider")
public interface FeignProviderClient {
@GetMapping("/provider/findall")
public Collection<Student> findall();
@GetMapping("/provider/findbyid/{id}")
public Student findbyid(@PathVariable("id") Integer id);
@PostMapping("/provider/save")
public void save(@RequestBody Student student);
@DeleteMapping("/provider/delete/{id}")
public void delete(@PathVariable("id") Integer id);
}

最新文章

  1. system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)
  2. Spark入门实战系列--4.Spark运行架构
  3. django admin中保存添加的数据提示need string or buffer, int found
  4. javascrit2.0完全参考手册(第二版) 第2章第1节 基本定义
  5. linux 文本处理
  6. Linux kernel scriptes bin2c &quot;\x&quot;
  7. 9、NFC技术:NDEF文本格式解析
  8. UISearchDisplayController隐藏navigationBar需注意
  9. [功能帮助类] JsHelper--Javascript操作帮助类 (转载)
  10. 比赛--找丢失的数--解题报告T
  11. WPF 实现验证码功能
  12. android-studio-bundle-141.2178183首次执行Hello World的时候出现ADB not responding. If you&#39;d like to retry, then please manually kill &quot;adb.e的错误
  13. struts2.3.23升级到struts2.3.32
  14. 【转】GPS连续运行单参考站解决方案
  15. js处理json js递归
  16. python3 判断数据类型
  17. Linux(Ubuntu)使用日记------tenserflow安装(pip安装法)
  18. Java编程思想--控制执行流程
  19. Unable to load configuration. - action - file:/F:/apache-tomcat-8.0.30/webapps/test1Struts2/WEB-INF/classes/struts.xml:11:71
  20. Python中str()和repr()函数的区别

热门文章

  1. 单节点部署 gpmall 商城
  2. 【云原生 · Kubernetes】Kubernetes 编排部署GPMall(一)
  3. FIXMAP内存管理器
  4. nginx rewrite参数 以及 $1、$2参数解析(附有生产配置实例)
  5. ST表优化区间gcd
  6. MQ系列8:数据存储,消息队列的高可用保障
  7. http 缓存 笔记
  8. angr_ctf——从0学习angr(二):状态操作和约束求解
  9. MassTransit 知多少 | 基于MassTransit Courier实现Saga 编排式分布式事务
  10. linux安装influxdb和chronograf