Dubbo遇到的问题以及整合淘淘商城

Dubbo学习

Dubbo配置了解

遇到的问题:

  1. idea运行的时候找不到某一个模块的类(之前我的模块名字乱了,重构了下,最后发现模块下的.iml有两个)

    在这里插入图片描述

  2. No provider available for the service com.dubbo.api.service.ICalcService

    常见问题

    问题一:找不到服务提供者

    问题描述:

    ​ 使用multicast注册中心时,一直报 No provider available for the service xxxx Service1

    问题原因:

    ​ dubbo的服务消费方在服务注册中心中无法找到匹配的服务提供者,导致服务无法调用

    问题解决:

    1. 检查服务提供者、消费者配置是否和快速开始或者官方multicast示例中一致

    2. 如果服务提供者和消费者处于同一台服务器上,或者一个服务提供者有多个服务消费者,在消费者这方需要关闭multicast的单播模式,改为广播模式,否则消费者可能无法接收到服务提供者发出的服务注册消息。

      <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />
      

      <dubbo:registry protocol="multicast" address="224.5.6.7:1234">
          <dubbo:parameter key="unicast" value="false" />
      </dubbo:registry>
      

      参见:官方说明

    3. 如果上面两步都排除掉了,依然无法解决。 使用终极大法,将你的网线拔掉,或者网络断掉,然后重启服务提供者、消费者,你会发现已经解决了。之后你再插上网线也不会再有问题,就是这么神奇~

    问题二:启动消费者时,有qos server无法启动的警告

    问题描述:

    警告:  [DUBBO] Fail to start qos server: , dubbo version: 2.7.3, current host: 10.10.13.127
    java.net.BindException: Address already in use: bind
    

    问题原因:

    通常这种情况发生在消费者和提供者运行在同一台服务器上,因为dubbo应用在启动时默认会启动一个心跳服务运行在22222端口,当一台服务器运行多个dubbo应用时,就会端口冲突。
    

    问题解决:

    ​ 在开发过程中可以直接将qos心跳服务停掉

    <dubbo:application name="hello-dubbo">
        <!-- 关闭qos心跳服务 -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>
    
  3. cp dd.txt ee.txt :复制并改名,并存放在当前目录下 (cp源文件名 新文件名)

  4. zookeer下载(bin)

    在这里插入图片描述

  5. zookeeper地址要改成自己的

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://dubbo.apache.org/schema/dubbo
            http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 给dubbo应用命名 -->
        <dubbo:application name="hello-dubbo">
            <dubbo:parameter key="qos.enable" value="false" />
        </dubbo:application>
    
        <!-- 配置Dubbo服务注册中心(这里使用的是基于multicast协议的注册中心) -->
        <!--<dubbo:registry address="multicast://224.5.6.8:1234?unicast=false" />-->
        <!-- 配置基于zookeeper的服务注册中心  -->
        <dubbo:registry address="zookeeper://10.10.13.105:2181?client=curator" />
        <!-- 配置基于redis的服务注册中心 -->
        <!--<dubbo:registry address="redis://localhost:6379" />-->
    
        <!-- 指定Dubbo底层在远程调用服务时通过什么协议,哪个端口调用 -->
        <dubbo:protocol name="dubbo" />
    
        <dubbo:reference id="calcService" interface="com.lanou3g.dubbo.service.ICalcService" />
        <!--<dubbo:reference id="calcService" interface="com.lanou3g.dubbo.service.ICalcService" url="dubbo://10.10.110.223:20880" />-->
    
    </beans>
    
  6. mvn clean package cmd不生效

  7. ctr+shift+u(idea 中代码大小写切换快捷键)

  8. 修改idea启动方式启动多个服务提供者(平行run)

    在这里插入图片描述

dubbo整合SpringBoot

所需pom.xml 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lanou3g</groupId>
    <artifactId>dubbo-springboot-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 引入SpringBoot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- dubbo springboot依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>

            <!-- redis注册中心依赖 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>3.1.0</version>
            </dependency>

            <!-- zookeeper注册中心依赖 -->
            <dependency>
                <groupId>com.netflix.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>1.1.10</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.0.1</version>
            </dependency>

            <dependency>
                <groupId>com.lanou3g</groupId>
                <artifactId>dubbo-springboot-api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

使用dubbo重构淘淘商城:

目录结构:

在这里插入图片描述

部分模块展示:

一:taotao-common-api(接口服务)

在这里插入图片描述

二:taotao-order(即是消费者又是提供者)

原因:跳到rest查询了订单中的商品信息

在这里插入图片描述

三:taotao-portal(消费者)

在这里插入图片描述

四:taotao-rest(提供者)

在这里插入图片描述

五:taotao-solr(提供者)

在这里插入图片描述

遇到的问题:

  1. dubbo的扫描路径把我搞晕了(扫描的是实现类)

    在这里插入图片描述

  2. 版本问题(降低版本)

    在这里插入图片描述

  3. httputil—dubbo(整合的关键就是改变后台网路请求方式。)

    dobbo会自动给前台实现序列化,我们只需要在所需的类上边实现序列化接口即可!

    即直接返回对象即可

在这里插入图片描述

​ 优化后的:

在这里插入图片描述

  1. jsonnode类了解

    在这里插入图片描述

    她是一个抽象类

    public static TaotaoResult formatToPojo(String jsonData, Class<?> clazz) {
            try {
                if (clazz == null) {
                    return MAPPER.readValue(jsonData, TaotaoResult.class);
                }
                // Json字符串转换成JsonNode对象
                JsonNode jsonNode = MAPPER.readTree(jsonData);
                //应该获得TaotaoResult.class某个字段的数据(Ctrl + alt + b:查看实现的子类)
                JsonNode data = jsonNode.get("data");
                Object obj = null;
                if (clazz != null) {
                    if (data.isObject()) {
                        obj = MAPPER.readValue(data.traverse(), clazz);
                    } else if (data.isTextual()) {
                        obj = MAPPER.readValue(data.asText(), clazz);
                    }
                }
                return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return null;
            }
        }
    
  2. dubbo注意点:

    public SearchResult search(String keywords, Integer pageNum, Integer pageSize) {
          Map<String, String> params = new HashMap<>();
          params.put("q", keywords);
          params.put("pageNum", pageNum+"");
          params.put("pageSize", pageSize+"");
          //String searchResultJson = HttpClientUtil.doPost(searchUrl + "/search", params);
          SearchResult search = search02(keywords, pageNum, pageSize);
          //以下是在dubbo中进行序列化(只需要给此类实现序列化就OK!)
          /*String searchResultJson = JsonUtils.objectToJson(search);
    
          *//*//后来加的
          TaotaoResult taotaoResult1 = TaotaoResult.formatToList(searchResultJson, TaotaoResult.class);
          String result = JsonUtils.objectToJson(taotaoResult1);*//*
          *//******************************************************************//*
          System.out.println(searchResultJson + "这里的隐藏着一个报错信息!");
          TaotaoResult taotaoResult = TaotaoResult.formatToPojo(searchResultJson, SearchResult.class);
          System.out.println(taotaoResult + "fdsg");
          if(taotaoResult.getStatus().equals(SystemConstants.TAOTAO_RESULT_STATUS_OK)) {
             return (SearchResult) taotaoResult.getData();
          }*/
          return search;
       }
    
  3. Serialized class org.apache.catalina.connector.RequestFacade

    HttpServletRequest无法被序列化,所以如果需要,就把request里面的参数取出来

    dubbo操作cookie用到了request

  4. 另一种解决办法:改为redis存储

  5. redis中的key——value如何处理最好

    redis的key——value不一定会被覆盖,故使用下边的建议:

    粗暴解决(先删除后添加)

学习

在github中搜索开源项目筛选关键字

in:name example 名字中有“example”
in:readme example readme中有“example”
in:description example 描述中有“example”

stars:>1000 star>1000
forks:>1000 fork>1000
pushed:>2019-09-01 2019年9月1日后有更新的

language:java 用Java编写的项目

终极搜索

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值