当前位置:   article > 正文

springcloud 整合 openfeign_springcloud整合openfeign

springcloud整合openfeign

openfeign目录

一、openfeign简介

Feign是Nefix开发的声明式、模块化的Http客户端。Feign可以帮助我们更快捷、优雅地调用Http Api。

在springclouid中使用 feign非常简单  --创建一个接口,并在接口中添加一些注解,代码就完成了,Feign支持多种注解 

openFeign 是springcloud对Feign进行了增强,使得Feign支持了springmvc的注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便

二、使用

1、依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!--服务注册与发现-->
  7. <dependency>
  8. <groupId>com.alibaba.cloud</groupId>
  9. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-openfeign</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>com.sofwin</groupId>
  17. <artifactId>springEntity</artifactId>
  18. <version>1.0-SNAPSHOT</version>
  19. </dependency>
  20. </dependencies>

2、配置文件 

加入到nacos的服务中

  1. server:
  2. port: 7000
  3. spring:
  4. cloud:
  5. nacos:
  6. discovery:
  7. server-addr: 127.0.0.1:8848
  8. heart-beat-interval: 1000
  9. application:
  10. name: openFeign

 3、启动类

  1. package com.sofwin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. /**
  7. * @author : wentao
  8. * @version : 1.0
  9. */
  10. @SpringBootApplication
  11. //开启服务注册与发现
  12. @EnableDiscoveryClient
  13. //开启Feign
  14. @EnableFeignClients
  15. public class App {
  16. public static void main(String[]args){
  17. SpringApplication.run(App.class,args);
  18. }
  19. }

 4、接口

  1. package com.sofwin.service;
  2. import com.sofwin.pojo.User;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.cloud.openfeign.SpringQueryMap;
  5. import org.springframework.web.bind.annotation.*;
  6. import java.util.List;
  7. /**
  8. * @author : wentao
  9. * @version : 1.0
  10. */
  11. //写的是服务端的名称
  12. @FeignClient("nacos-previder")
  13. public interface UserService {
  14. @GetMapping("/user/data")
  15. String getUserName();
  16. @GetMapping("/user/info/{msg}")
  17. //这里注意了 这个注意@PathVariable 一定要添加name或者value属性
  18. String infoResful(@PathVariable(name = "msg") String msg);
  19. @GetMapping("/user/data5")
  20. //这里注意了 调用远程服务必须使用@RequesParam注解 并且名字与远程服务的参数名相同
  21. String simpleParam(@RequestParam("userName11") String userName,@RequestParam("pwd") String pwd);
  22. //其实openFeign默认就是json的格式 这里是为了规范
  23. @PostMapping("user/data6")
  24. User getUser (@RequestBody User user);
  25. @DeleteMapping("user/data7")
  26. User getUser2 (@RequestBody User user);
  27. //或者我们就可以使用一个注解 将json格式改为form表单的格式发送到远程服务中
  28. //然后远程不用写@RequestBody
  29. @PostMapping("user/data8")
  30. User getUser3 (@SpringQueryMap User user);
  31. //集合
  32. @PostMapping("user/data9")
  33. List<User> getList(@RequestBody User user);
  34. }

接口中定义远程服务中的的请求   

使用 @FeignClien的注解    名称是远程服务的名称

5、controller

  1. package com.sofwin.controller;
  2. import com.sofwin.pojo.User;
  3. import com.sofwin.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.*;
  6. import java.util.List;
  7. /**
  8. * @author : wentao
  9. * @version : 1.0
  10. */
  11. @RestController
  12. @RequestMapping("/role")
  13. public class RoleController {
  14. @Autowired
  15. private UserService userService;
  16. @GetMapping("/data")
  17. public String data(){
  18. String userName = userService.getUserName();
  19. return userName;
  20. }
  21. //Resful风格
  22. @GetMapping("data1")
  23. public String infoResful(String msg){
  24. String s = userService.infoResful(msg);
  25. return s;
  26. }
  27. //简单类型的请求参数
  28. @GetMapping("/data2")
  29. public String simpleParam(String userName,String pwd){
  30. return userService.simpleParam(userName,pwd);
  31. }
  32. //自定义 json字符串 或者表单形式
  33. @PostMapping("/data3")
  34. public User getUser(User user){
  35. return userService.getUser(user);
  36. }
  37. @DeleteMapping("/data4")
  38. public User getUser2(User user){
  39. return userService.getUser2(user);
  40. }
  41. @PostMapping("/data5")
  42. public User getUser3(User user){
  43. return userService.getUser3(user);
  44. }
  45. //返回结果为集合类型
  46. @PostMapping("/data6")
  47. public List<User> getList(User user){
  48. return userService.getList(user);
  49. }
  50. }

注意:

//其实openFeign默认就是json的格式
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/428825
推荐阅读
相关标签
  

闽ICP备14008679号