springcloud 整合 openfeign的方法

2023-02-23 0 3,532

目录

一、openfeign简介

Feign是Nefix开发的声明式、模块化的Http客户端。Feign可以帮助我们更快捷、优雅地调用Http Api。
在springclouid中使用 feign非常简单  --创建一个接口,并在接口中添加一些注解,代码就完成了,Feign支持多种注解 
openFeign 是springcloud对Feign进行了增强,使得Feign支持了springmvc的注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便

二、使用

1、依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.sofwin</groupId>
<artifactId>springEntity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

2、配置文件 

加入到nacos的服务中
server:
port: 7000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
heart-beat-interval: 1000
application:
name: openFeign

 3、启动类

package com.sofwin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* @author : wentao
* @version : 1.0
*/
@SpringBootApplication
//开启服务注册与发现
@EnableDiscoveryClient
//开启Feign
@EnableFeignClients
public class App {
public static void main(String[]args){
SpringApplication.run(App.class,args);
}
}

 4、接口

package com.sofwin.service;

import com.sofwin.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author : wentao
* @version : 1.0
*/
//写的是服务端的名称
@FeignClient(\"nacos-previder\")
public interface UserService {

@GetMapping(\"/user/data\")
String getUserName();

@GetMapping(\"/user/info/{msg}\")
//这里注意了 这个注意@PathVariable 一定要添加name或者value属性
String infoResful(@PathVariable(name = \"msg\") String msg);

@GetMapping(\"/user/data5\")
//这里注意了 调用远程服务必须使用@RequesParam注解 并且名字与远程服务的参数名相同
String simpleParam(@RequestParam(\"userName11\") String userName,@RequestParam(\"pwd\") String pwd);

//其实openFeign默认就是json的格式 这里是为了规范
@PostMapping(\"user/data6\")
User getUser (@RequestBody User user);
@DeleteMapping(\"user/data7\")
User getUser2 (@RequestBody User user);
//或者我们就可以使用一个注解 将json格式改为form表单的格式发送到远程服务中
//然后远程不用写@RequestBody
@PostMapping(\"user/data8\")
User getUser3 (@SpringQueryMap User user);

//集合
@PostMapping(\"user/data9\")
List<User> getList(@RequestBody User user);
}

接口中定义远程服务中的的请求   
使用 @FeignClien的注解    名称是远程服务的名称

5、controller

package com.sofwin.controller;

import com.sofwin.pojo.User;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author : wentao
* @version : 1.0
*/
@RestController
@RequestMapping(\"/role\")
public class RoleController {

@Autowired
private UserService userService;
@GetMapping(\"/data\")
public String data(){
String userName = userService.getUserName();
return userName;
}
//Resful风格
@GetMapping(\"data1\")
public String infoResful(String msg){
String s = userService.infoResful(msg);
return s;
}
//简单类型的请求参数
@GetMapping(\"/data2\")
public String simpleParam(String userName,String pwd){
return userService.simpleParam(userName,pwd);
}
//自定义 json字符串 或者表单形式
@PostMapping(\"/data3\")
public User getUser(User user){
return userService.getUser(user);
}
@DeleteMapping(\"/data4\")
public User getUser2(User user){
return userService.getUser2(user);
}
@PostMapping(\"/data5\")
public User getUser3(User user){
return userService.getUser3(user);
}
//返回结果为集合类型
@PostMapping(\"/data6\")
public List<User> getList(User user){
return userService.getList(user);
}
}

注意:
//其实openFeign默认就是json的格式

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 PHP教程 springcloud 整合 openfeign的方法 https://www.20zxx.cn/585951/xuexijiaocheng/qes.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务