您现在的位置是:群英 > 开发技术 > 编程语言
基于SpringCloud的OpenFeign模板化远程通信如何实现
Admin发表于 2022-10-09 18:13:08512 次浏览
在这篇文章中我们会学习到关于“基于SpringCloud的OpenFeign模板化远程通信如何实现”的知识,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望对大家学习或工作能有帮助。下面就请大家跟着小编的思路一起来学习一下吧。


1. openFeign实现

基于spring-boot-starter-parent 2.6.8,spring-cloud-dependencies 2021.0.3,一个order服务一个user服务

1.1 pom依赖

<!--nacos服务注册与发现-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	<version>2021.0.1.0</version>
</dependency>
<!--远程服务调用-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!--服务调用feign-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

1.2 yaml配置

order调用端,配置的超时设置注释掉了只为记录

spring:
  application:
    name: orderservice
  cloud:
    #找对应网段的网卡 不配置内部服务就走外网
    inetutils:
      preferred-networks: 192.168.0
    nacos:
      discovery:
        server-addr: 192.168.0.221:8848
#feign:
#  client:
#    config:
#      #default设置的是全局超时时间,对所有的openFeign接口服务都生效 默认60s超时
#      default:
#        connectTimeout: 5000
#        readTimeout: 5000
#      #为某个服务设置超时时间 优先于全局
#      userservice:
#        connectTimeout: 5000
#        readTimeout: 5000

user服务仅需要注册

spring:
  application:
    name: userservice
  cloud:
    #找对应网段的网卡 不配置内部服务就走外网
    inetutils:
      preferred-networks: 192.168.0
    nacos:
      discovery:
        server-addr: 192.168.0.221:8848

远程调用依赖于注册中心,这里用的是nacos,其他的eureka也可以的

1.3 客户端调用代码

  • 启动类上添加@EnableFeignClients注解
  • api接口,可以单独放在api包
@FeignClient(value = "userservice")
//没有注册中心的服务调用使用  testFeign/随便写
//@FeignClient(value = "testFeign",url = "http://192.168.0.199:7540")
public interface UserService {
    //默认是@RequestBody注解参数 
    //如果使用其他注解一定要带上value 否者会报错 RequestParam.value() was empty on parameter 1
    @GetMapping("/getTime/{uuid}")
    String getTime(@PathVariable("uuid") String uuid, @RequestParam("name") String name);
    @PostMapping("/postTime")
    Map<String, Object> getTime(@RequestBody Map<String, Object> params);
}

客户端代码

	@Resource
    UserService userService;
    @GetMapping("/test")
    public String test() throws Exception {
        log.info("openFeign -- start");
        Map<String, Object> time = userService.getTime(resMap);
        log.info("openFeign -- {}", time);
        return template + ":" + time;
    }

1.4.服务端暴露接口

    @PostMapping("/postTime")
    public Map<String, Object> getTime(@RequestBody Map<String, Object> params) {
        params.put("time", new Date().getTime());
        return params;
    }

1.5.测试日志

c.e.order.controller.OrderController     : openFeign -- start
c.e.order.controller.OrderController     : openFeign -- {aaaa=bbbb, time=1657187048104}



感谢各位的阅读,以上就是“基于SpringCloud的OpenFeign模板化远程通信如何实现”的内容了,通过以上内容的阐述,相信大家对基于SpringCloud的OpenFeign模板化远程通信如何实现已经有了进一步的了解,如果想要了解更多相关的内容,欢迎关注群英网络,群英网络将为大家推送更多相关知识点的文章。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: SpringCloud
相关信息推荐
2022-08-25 17:46:11 
摘要:留言板是接触WEB开发的基础,写一个留言板需要知道前端的一些基础标签,对数据库有一个了解会基础SQL语言,PHP基础知识,前段基础+数据库基础+PHP基础=>留言板
2022-07-27 17:56:22 
摘要:这篇文章主要为大家详细介绍了AJAX实现指定部分页面刷新效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
2022-07-25 17:55:02 
摘要:bootstrap icon不显示的解决办法:首先找到fonts文件夹下的指定文件;然后保证该文件和bootstrap.css文件的相对路径正确即可。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部