Spring Cloud之Feign客户端调用工具
feign介绍
Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用。
Spring Cloud 支持 RestTemplate Fetin
Feign客户端实际开发中用的最多 ,易读性比较强。
主要调用部分:
pom:
<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"><modelVersion>4.0.0</modelVersion><groupId>com.toov5</groupId><artifactId>order</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version></parent><!-- 管理依赖 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.M7</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- SpringBoot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot整合eureka客户端 --><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><!-- 注意: 这里必须要添加, 否者各种依赖有问题 --><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>
yml:
###服务启动端口号
server:port: 8002
###服务名称(服务注册到eureka名称)
spring:application:name: app-toov5-order
###服务注册到eureka地址
eureka:client:service-url:defaultZone: http://127.0.0.1:8100/eureka###因为该应用为注册中心,不会注册自己register-with-eureka: true
###是否需要从eureka上获取注册信息fetch-registry: true
Feign
package com.toov5.Feign;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping;@FeignClient(name="app-toov5-member") //name就是服务名称 public interface MemberFeign {//Feign书写方式 以Spring mvc接口形式书写 @RequestMapping("/getMember") //接口形式 实现是交给Member实现的public String getMember(); //底层自动转换成http协议实现rpc远程调用 }
controller
package com.toov5.api.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.toov5.Feign.MemberFeign;@RestController public class FeignController {@Autowiredprivate MemberFeign memberFeign;@RequestMapping("/getFeignMember")public String feignMember() {return memberFeign.getMember();} }
启动类:
package com.toov5;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication(scanBasePackages={"com.toov5.*"}) @EnableEurekaClient @EnableFeignClients //可以开启feign客户端权限 public class AppOrder {public static void main(String[] args) {SpringApplication.run(AppOrder.class, args); }//解决RestTemplate找不到问题 把restTemplate注册到Spring Boot容器中 // @Bean // @LoadBalanced 手写的 不要去实现本地负载均衡效果了 // RestTemplate restTemplate() { // return new RestTemplate(); // } // }
Member(不用变) 服务提供者不变
pom:
<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"><modelVersion>4.0.0</modelVersion><groupId>com.toov5</groupId><artifactId>member</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version></parent><!-- 管理依赖 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.M7</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- SpringBoot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot整合eureka客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><!-- 注意: 这里必须要添加, 否者各种依赖有问题 --><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
yml:
###服务启动端口号
server:port: 8009
###服务名称(服务注册到eureka名称)
spring:application:name: app-toov5-member
###服务注册到eureka地址
eureka:client:service-url:##当前会员注册到eureka服务 地址+端口号 defaultZone: http://127.0.0.1:8100/eureka###因为该应用为注册中心,不会注册自己register-with-eureka: true
###是否需要从eureka上获取注册信息fetch-registry: true
controller
package com.toov5.api.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class MemberApiController {@Value("${server.port}")private String serverPort;@RequestMapping("/getMember")public String getMember() {return "会员服务"+serverPort;} }
启动类
package com.toov5.api;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient //注册到eureka public class AppMember {public static void main(String[] args) {SpringApplication.run(AppMember.class, args);}}
Eureka
pom:
<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"><modelVersion>4.0.0</modelVersion><groupId>com.toov5</groupId><artifactId>SpringCloud-eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version></parent><!-- 管理依赖 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.M7</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--SpringCloud eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
yml:
###eureka 服务端口号
server:port: 8100
###服务注册名称
eureka:instance:##注册中心ip地址hostname: 127.0.0.1
###客户端调用地址client:serviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为该应用为注册中心,不会注册自己 (集群设为true)register-with-eureka: false
###因为自己为注册中心 ,不会去在该应用中的检测服务 fetch-registry: false
启动:
package com.toov5;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer //开启注册中心 @SpringBootApplication public class AppEureka {public static void main(String[] args) {SpringApplication.run(AppEureka.class, args);}}
转载于:https://www.cnblogs.com/toov5/p/9955561.html
Spring Cloud之Feign客户端调用工具相关推荐
- Spring Cloud (Eureka,Feign,Hystrix整合)
Spring Cloud(Eureka,Feign,Hystrix整合) Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代 ...
- Java笔记(3) - 使用Spring Cloud Zookeeper + Feign实现服务发现
配置映射主机名和IP 为每台虚拟主机的/etc/hosts文件加上如下内容,服务发现需要用到默认主机名作为访问地址,这样可以不用为每个服务实例配置IP 192.168.253.30 megumi-30 ...
- Spring Cloud(四):Spring Cloud Alibaba Feign Dubbo
扩展点 RequestInterceptor#apply 扩展点 feign.Client#execute spring cloud dubbo 调用 RPC RPC 全称是 Remote Proce ...
- Spring cloud alibaba--Feign微服务调用组件
目录 1.1Feign优势 2.spring cloud alibaba整合Feign 3.Spring Cloud Feign日志配置 4.Feign契约配置 5.Feign超时时间配置 6.Ope ...
- 脱离spring cloud使用feign
优雅的http接口调用-feign. spring-cloud-feign是spring cloud微服务之间调用封装的功能,由于feign的封装和解耦做的比较好,因此脱离spring cloud也能 ...
- spring cloud利用feign和sentinel进行内部或外部远程调用
一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 /* Copyright 2013 ...
- spring cloud + nacos + feign调用
//by yan 20211119 需求: 简单建个项目,用来测试调用注册在nacos的服务接口. 步骤: idea -> new project-> Spring Initializr ...
- spring cloud微服务之间调用Feign
微服务之间调用Feign 1.在调用方添加feign依赖: <dependency><groupId>org.springframework.cloud</groupId ...
- spring cloud+zookeeper+feign整合 简单实例(一)
一.前言 各位热爱知识的小伙伴们大家好呀!很高兴大家能点开这个博客,这是我个人的第一篇博客,之后也会持续的更新java以及spring项目的相关代码,希望大家持续关注.如果对本篇博客有什么不懂的地方或 ...
- Spring Cloud中Feign如何统一设置验证token
前面我们大致的聊了下如何保证各个微服务之前调用的认证问题 Spring Cloud中如何保证各个微服务之间调用的安全性 Spring Cloud中如何保证各个微服务之间调用的安全性(下篇) 原理是通过 ...
最新文章
- PCL调错:(3)error C2589“(“:“::“右边的非法标记
- http://forensics.idealtest.org CASIA图像篡改数据库
- linux机器启动pg数据库命令,Linux下创建Postgresql数据库的方法步骤
- 矩阵分析与多元统计1 线性空间与线性变换1
- 鲁迅散文——随感录三十五
- numpy函数:[6]arange()详解
- html代码编辑器sp,在线HTML编译,文本关键字高亮显示,富文本编辑实现大概思路...
- spring mvc 工作流程
- php判断是不是纯汉字,php判断输入是否是纯数字,英文,汉字的方法
- Java开发大厂面试资料,让你的面试不再困难!
- ping TCP端口的实用小工具tcping
- 软件设计师中级-UML建模
- linux安装英伟达显卡驱动
- 高质量WordPress下载站模板5play主题源码
- 超越函数e^(-x^2)在(-∞, +∞)上的定积分的两种解法
- 利用Catmull-Rom算法画人脸轮廓
- 防御DDoS攻击的十一种方法
- 【金猿人物展】深演智能黄晓南:在数字化营销的趋势下,智能和算法将重塑营销价值的评估标准...
- web 前端常见英文汇总
- 机械转行程序员怎么样?
热门文章
- 30岁之前创业想成功必看
- 图神经网络:Graph Neural Networks
- 教育部建议采纳:给予导师决定硕博士能否毕业的自主权!
- 【面经】来啦!百度凤巢算法面经
- 【PyTorch】推荐收藏!史上最全的 PyTorch trick 集锦
- python科学计算之Pandas使用(二)
- 【每日算法Day 87】今天我脱单了,所以大家不用做题了!
- 每日算法系列【LeetCode 1250】检查「好数组」
- [论文笔记]MACHINE COMPREHENSION USING MATCH-LSTM AND ANSWER POINTER
- EventThread线程对VSync的接收