• 微服务如何实现各个模块之间相互调用


    大家好,我是雄雄,现在是:2022年8月23日21:08:53

    在这里插入图片描述

    前言

    在做微服务项目时,我们可能都会遇到这样的情况,那就是A服务需要调用B服务中的某个接口,那有这样的需求时,我们应该怎么处理呢?

    实现

    使用**@FeignClient** 注解。
    其实吧,网上也有好多关于Feign的相关知识和教程,一搜一大堆,有的看眼就会了,有的看眼就废了。。。

    我来整理下我的方法吧,尽量的简单明了,不为别的,就为了报答你能在茫茫人海中找到我,让你别浪费时间,抓紧CV到工程中就能跑,到时候想起来了给我点个赞,想不起来了就算了,你好我好,大家都好!

    A服务调用B服务的接口:

    第一步:

    检查下B服务中的接口,能不能直接用(主要看看返回值和参数,如果是普通的Stirngint之类的都能直接用)
    如果是实体或者对象集合,我们就使用JSON传递数据。
    如下我写了个接口:

     /**
         * 根据id查询网站的配置
         *
         * @return
         */
        @ApiOperation(value = "网站设置表-通过id查询", notes = "网站设置表-通过id查询")
        @GetMapping(value = "/queryConfigById")
        public String queryConfigById(String id) {
            JSONObject jsonObject = new JSONObject();
            WebConfig webConfig = webConfigService.getById(id);
            if (webConfig == null) {
                jsonObject.put("code", 200);
                jsonObject.put("data", null);
                return jsonObject.toJSONString();
            }
            jsonObject.put("code", 200);
            jsonObject.put("data", webConfig);
            return jsonObject.toJSONString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    我这个就是返回了JSON数据。

    第二步

    在A服务模块中,新建个接口,添加@FeignClient注解,注解的值为B服务的模块名,也就是yml文件中的name

    spring:
      application:
        name: xxx-web
      main:
        allow-bean-definition-overriding: true
    
    • 1
    • 2
    • 3
    • 4
    • 5

    接口代码如下:

    @FeignClient("xxx-web")
    public interface WebClient {
    
        /**
         * 根据编号查询网站的配置信息
         * @param id
         * @return
         */
        @GetMapping(value = "/config/webConfig/queryConfigById")
        String queryConfigById(@RequestParam("id") String id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第三步

    controller控制器中,自动注入B服务的模块。

        @Autowired
        private WebClient webClient;
    
    
    • 1
    • 2
    • 3

    这个名字:WebClient 就是我们创建的那个接口的名字。
    至此,你就可以在控制器中随便用了,通过webClient点里面的方法即可,我的业务代码如下:

     String huifu = "欢迎关注~";
            try {
                //查询网站的配置信息,主要获取的是关注用户回复的内容
                String webConfig = webClient.queryConfigById("1");
                if(Strings.isNotBlank(webConfig)){
                    JSONObject object = JSONObject.parseObject(webConfig);
                    Integer code = object.getInteger("code");
                    if(code==200){
                        String data = object.getString("data");
                        JSONObject dataObject = JSONObject.parseObject(data);
                        if(Strings.isNotBlank(dataObject.getString("xcxReply"))){
                            //拿到数据库中的关注之后回复的信息
                            huifu = dataObject.getString("xcxReply");
                        }
                    }
    
                }
                return new TextBuilder().build(huifu, wxMessage, weixinService);
            } catch (Exception e) {
                this.logger.error(e.getMessage(), e);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    完事儿了!!!

  • 相关阅读:
    [wp][RCTF2015]EasySQL
    MVVMLight的绑定
    灵活部署的CRM系统和传统软件的区别?
    《科学之路》读书笔记
    2022年湖北劳务资质如何办理?劳务资质不分等级
    不知道10年老电脑如何重装系统?其实很简单
    java成员等讲解
    python:最小二乘法拟合原理及代码实现
    Remove the specified nodes in the linked list with dummy header
    人工智能知识全面讲解:让数据更直观的方法
  • 原文地址:https://blog.csdn.net/qq_34137397/article/details/126493073