spring cloud(四) feign
spring cloud 使用feign进行服务间调用
1. 新建boot工程 pom引入依赖
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.启动类添加注解@EnableEurekaClient @EnableFeignClients
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class WebWapApplication {
public static void main(String[] args) {
SpringApplication.run(WebWapApplication.class, args);
}
}
3. application.properties添加配置
server.port=8080
spring.application.name=web-wap
spring.main.allow-bean-definition-overriding=true
#eureka
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
4. 编写调用程序
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserFeignCilent userFeignCilent;
@GetMapping("/getUserInfoList")
public GlobalResult<List<UserInfoRespVo>> getUserInfoList(){
return userFeignCilent.getUserInfoList();
}
@GetMapping("/getUserInfo")
public GlobalResult<UserInfoRespVo> getUserInfo(Integer id){
return userFeignCilent.getUserInfo(id);
}
}
/**
* 调用网关服务
*/
@FeignClient(name = "zuul-server")
@RequestMapping("/userService")
public interface UserFeignCilent {
@GetMapping("/getUserInfoList")
GlobalResult<List<UserInfoRespVo>> getUserInfoList();
@GetMapping("/getUserInfo")
GlobalResult<UserInfoRespVo> getUserInfo(@RequestParam("id") Integer id);
}
服务提供者 user-service
@RestController
public class UserController {
@Autowired
private UserInfoService userInfoService;
@GetMapping("/getUserInfoList")
public GlobalResult<List<UserInfoRespVo>> getUserInfoList() {
return userInfoService.getUserInfoList();
}
@GetMapping("/getUserInfo")
public GlobalResult<UserInfoRespVo> getUserInfo(Integer id) {
return userInfoService.getUserInfo(id);
}
}
5.调用结果

项目github 地址 https://github.com/yongxiangliu123/SpringCloud
spring cloud(四) feign的更多相关文章
- spring cloud 使用feign 遇到问题
spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...
- spring cloud关于feign client的调用对象列表参数、设置header参数、多环境动态参数试配
spring cloud关于feign client的调用 1.有些场景接口参数需要传对象列表参数 2.有些场景接口设置设置权限等约定header参数 3.有些场景虽然用的是feign调用,但并不会走 ...
- Spring Cloud 整合 Feign 的原理
前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...
- spring cloud 之 Feign 使用HTTP请求远程服务
一.Feign 简介 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLCo ...
- Spring Cloud 之 Feign
新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- Spring Cloud中Feign如何统一设置验证token
代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...
- Spring Cloud 组件 —— feign
feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...
- spring cloud中feign的使用
我们在进行微服务项目的开发的时候,经常会遇到一个问题,比如A服务是一个针对用户的服务,里面有用户的增删改查的接口和方法,而现在我有一个针对产品的服务B服务中有一个查找用户的需求,这个时候我们可以在B服 ...
- spring cloud 之 Feign的使用
1.添加依赖 2.创建FeignClient 原理:Spring Cloud应用在启动时,Feign会扫描标有@FeignClient注解的接口,生成代理,并注册到Spring容器中.生成代理时Fei ...
随机推荐
- STM32 HAL库的定时器中断回调函数跟串口中断回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { //添加回调后的程序逻辑 if (htim->Instance == ...
- daemon not running; starting now at tcp:5037 adb: CreateFileW 'nul' failed: 系统找不到指定的文件
1. INFO - roid.tools.idea.adb.AdbService - Initializing adb using: C:\Users\Administrator\AppData\Lo ...
- hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
#include<stdio.h> #include<string.h> #define N 21000 struct node { int v,next; }bian[510 ...
- LightOJ - 1231 - Coin Change (I)
先上题目: 1231 - Coin Change (I) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- Spring Boot实例Hello World Demo
Spring Boot要求Maven的版本达到3.2或以上. 实例: POM: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- Windows 10不能正常打开开始菜单问题修复
1.可以尝试通过命令重新注注册Windows Store app: powershell -ExecutionPolicy Unrestricted Add-AppxPackage -DisableD ...
- installed jre指向jdk而非jre位置&
1.eclipse菜单 - Window - Preferences- Java - Installed JREs 将配置的JRE定位到JDK,例如JRE home:D:\Program Files ...
- HDOJ 题目5097 Page Rank(矩阵运算,模拟)
Page Rank Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Tota ...
- CLLocationManagerDelegate的解说
1.//新的方法.登陆成功之后(旧的方法就无论了) - (void)locationManager:(CLLocationManager *)manager didUpdateLocatio ...
- Linux中的默认权限与隐藏权限(文件、文件夹)
一个文件(或文件夹)拥有若干个属性.包含(r/w/x)等基本属性,以及是否为文件夹(d)与文件(-)或连接文件(l)等属性.此外,Linux还能够设置其它系统安全属性.使用chattr来设置.以lsa ...