• 学习dubbo项目架构时,consumer 报错class not found


    使用测试类方式启动dubbo  指定此项目为a项目

    一、ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");

    context.start();
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.zd.service.DemoService" ref="demoService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.zd.service.impl.DemoServiceImpl" />

    这时候项目有两种功能情况:
    1、 <dubbo:service interface="com.zd.service.DemoService" ref="demoService" />
    这里interface 指定的接口可以是a项目本身,也可以是b项目(b项目仅仅是一个普通的java项目,a项目中加入了b项目的依赖)
    使用配置类是可以直接 这样获取interface ,就算这个interface在其他java项目中还是能顾拿到
    2、如果使用web.xml 方式启动的话,如果该接口没有注入到Spring中,那么就会报错,报can not found class exception
    因为通过b项目区获取这个接口,xml会去ioc容器中去找,而a项目中的这个接口仅仅是一个普通的java项目接口,没有注入到ioc中。

    下面附上完成Springmvc 集成dubbo

    Provider
      1、新建一个Demoservice
    package com.zd.service;
    
    /**
     * @Author: EnzoLuo
     * @Date: 2018/7/12 16:14
     */
    public interface DemoService {
        String sayHello(String name);
    }

      2、新建一个DemoserviceImpl

      

    package com.zd.service.impl;
    import com.zd.service.DemoService;
    import org.springframework.stereotype.Service;
    
    @Service("demoService") //这里要注意,不要使用dubbo的service注解,不然怎么死的都不知道
    public class DemoServiceImpl implements DemoService {
    
        public String sayHello(String name) {
            // TODO Auto-generated method stub
            System.out.println("hello:"+name);
            return "hello,"+name;
        }
        
    }

    3、dubbo-provider.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd        
         http://code.alibabatech.com/schema/dubbo        
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     
     
         <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="app-provider"  />
     
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
     
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.zd.service.DemoService" ref="demoService" />
     
        <!-- 和本地bean一样实现服务 -->
        <bean id="demoService" class="com.zd.service.impl.DemoServiceImpl" />
     
         
         
     </beans>

    4、spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
       
    <context:component-scan base-package="com.zd.controller"></context:component-scan>
       
    <mvc:annotation-driven />
       
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    
    </bean>               
    <import resource="dubbo-provider.xml"/>
    </beans>

    5、web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    
        <display-name>Archetype Created Web Application</display-name>
    
    
        <!-- Spring MVC servlet -->
        <servlet>
            <servlet-name>SpringMVC</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>
        <servlet-mapping>
            <servlet-name>SpringMVC</servlet-name>
            <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>

    这里可以测试一下启动,记得打开zookeeper

    附上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">
        <parent>
            <artifactId>dubbo</artifactId>
            <groupId>com.zd</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>order-service</artifactId>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <springframework.version>3.2.4.RELEASE</springframework.version>
            <!-- Logging -->
            <slf4j.version>1.7.5</slf4j.version>
            <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version>
            <log4j.version>1.2.17</log4j.version>
        </properties>
    
        <dependencies>
            <!-- Logging with SLF4J -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j-log4j12.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.5</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <scope>compile</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.0.2.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.0.2.RELEASE</version>
            </dependency>
    
            <!--servlet api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.0.2.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>com.zd</groupId>
                <artifactId>api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
    </project>

    Consumer

    1、新建DemoController

    package com.cwj.controller;
    
    
    import com.zd.service.DemoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @Author: EnzoLuo
     * @Date: 2018/7/12 16:48
     */
    @Controller
    public class DemoController {
        @Autowired
        private DemoService demoService;
    
        @RequestMapping("/index")
        public String index(){
            String name = demoService.sayHello("enzoluo");
            return "test";
        }
    }

    2、dubbo.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd        
         http://code.alibabatech.com/schema/dubbo        
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="app-customer"  />
     
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
     
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="demoService" interface="com.zd.service.DemoService" />
     
         
         </beans>

    3、spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans    
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                            http://www.springframework.org/schema/context    
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                            http://www.springframework.org/schema/mvc    
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
       
    <context:component-scan base-package="com.cwj.controller"></context:component-scan> 
       
    <mvc:annotation-driven />
       
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    
    </bean>               
    <import resource="dubbo.xml"/>
    </beans>

    4、web.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns="http://java.sun.com/xml/ns/javaee"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
        version="3.0">  
     
      <display-name>Archetype Created Web Application</display-name>
    
    
        <!-- Spring MVC servlet -->  
        <servlet>  
            <servlet-name>SpringMVC</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath:spring-mvc.xml</param-value>  
            </init-param>  
            <load-on-startup>1</load-on-startup>  
            <async-supported>true</async-supported>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>SpringMVC</servlet-name>  
            <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->  
            <url-pattern>/</url-pattern>  
        </servlet-mapping>  
        <welcome-file-list>  
            <welcome-file>/index.jsp</welcome-file>  
        </welcome-file-list>  
    
    </web-app>

    pom.xml

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.zd</groupId>
        <artifactId>protal</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>protal</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.0.2.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.0.2.RELEASE</version>
            </dependency>
    
            <!--servlet api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
                <scope>provided</scope>
            </dependency>
                
           <!-- dubbo 基础包 -->
           
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.5</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <scope>compile</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>spring</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency> 
    
            <dependency>
                <groupId>com.zd</groupId>
                <artifactId>order-service</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>protal</finalName>
        </build>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    </project>

    可以启动成功了

    不懂可以评论问我,及时回复。

    不懂可以评论问我,及时回复。

    不懂可以评论问我,及时回复。




  • 相关阅读:
    Java多线程详解——一篇文章搞懂Java多线程
    解析spring事务管理@Transactional为什么要添加rollbackFor=Exception.class(事务失效)
    有参构造器(方法)和无参构造器(方法)的作用(区别)
    JAVA:将类、抽象类、接口当成方法的参数传入
    为什么要有无参构造方法,无参构造的运行原理
    java 如何在listener(监听器) 中使用Spring容器管理bean
    java为什么有些异常throw出去需要在函数头用throws声明,一些就不用?
    用mmap 考os不用考虑, 利用 cache 向量化计算 内存对齐 叶大小 都得考虑 还有原子写入
    位运算 异或 数组中数字出现的次数 II
    程序中有个常量,空间超过了1页,有哪些优化思路
  • 原文地址:https://www.cnblogs.com/enzoluo-blog/p/9300254.html
Copyright © 2020-2023  润新知