springBoot中的dubbox与普通项目的中的dubbox之间调用

2 篇文章 0 订阅

后期发现 dubbox与@Transactional 会发生冲突,解决方案
参考:https://github.com/teaey/spring-boot-starter-dubbo/issues/226

具体解决方法:
1.pom.xml文件修改

<!--  旧版本dubbo 替换成新的dubbo  -->  
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <!-- 旧版本@Service 注解和@Transactional有冲突 -->
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
            </exclusion>
        </exclusions>
     </dependency>
     <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.8.4</version>
    </dependency>
    <!-- dubbo 相关的 -->
    <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
    </dependency>

    <dependency>
           <groupId>org.apache.zookeeper</groupId>
           <artifactId>zookeeper</artifactId>
           <version>3.4.6</version>
           <exclusions>
               <exclusion>
                   <groupId>org.slf4j</groupId>
                   <artifactId>slf4j-log4j12</artifactId>
               </exclusion>
               <exclusion>
                   <groupId>log4j</groupId>
                   <artifactId>log4j</artifactId>
               </exclusion>
           </exclusions>
      </dependency>
    <!--  aop 注入 -->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2.application.properties 中配置


#dubbo提供者的别名,只是个标识
spring.dubbo.application.name=pinyougou-sellergoods-service
#zk地址
spring.dubbo.registry.address=zookeeper://192.168.25.130:2181
#dubbo协议
spring.dubbo.protocol.name=dubbo
#duboo端口号,每一个都不一样
spring.dubbo.protocol.port=20881
#这是你要发布到dubbo的接口所在包位置
spring.dubbo.scan = com.pinyougou.sellergoods.service.impl

# 启动springBoot的zop
spring.aop.proxy-target-class=true

3.在启动类中加注释


@SpringBootApplication
@MapperScan("com.pinyougou.mapper")//将项目中对应的mapper类的路径加进来就可以了
//@ImportResource(locations = {"classpath:dubbo-pro.xml"}) 使用xml文件
//aop注解
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class Application {

    public static void main(String[] args) {
        //启动程序
        SpringApplication.run(Application.class, args);
    }

}

1.springBoot项目中配置dubbox
其重点是:

传统项目中的dubbo

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>            
        </dependency>

对应的是 springBoot项目的dubbo

    <dependency>
             <groupId>io.dubbo.springboot</groupId>
             <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency> 

(1) 可以参考https://blog.csdn.net/wohaqiyi/article/details/72967805来配置
(2) spribgBoot项目中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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.pinyougou</groupId>
    <artifactId>pinyougou-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>pinyougou-sellergoods-service</artifactId>

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 <dependencies>
        <!-- web启动项 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
             <groupId>io.dubbo.springboot</groupId>
             <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency> 
         <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  </dependencies>
</project>

(3)项目目录结构
这里写图片描述

(4)application.properties 配置

server.port=9100

#dubbo提供者的别名,只是个标识
spring.dubbo.application.name=dubbox-service
#zk地址
spring.dubbo.registry.address=zookeeper://192.168.25.130:2181
#dubbo协议
spring.dubbo.protocol.name=dubbo
#duboo端口号,每一个都不一样
#spring.dubbo.protocol.port=20881
#这是你要发布到dubbo的接口所在包位置
spring.dubbo.scan = com.pinyougou.sellergoods.service

(5) 接口以及实现类
这里写图片描述

(6)项目地址 https://gitee.com/yuhaifei/pinyougou-springBoot.git


传统项目
(1) 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>dubbox-web</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

   <properties>     
        <spring.version>4.2.4.RELEASE</spring.version>
   </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>            
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.11.0.GA</version>
        </dependency>

    </dependencies>


  <build>
     <plugins>
          <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-compiler-plugin</artifactId>  
              <version>2.3.2</version>  
              <configuration>  
                  <source>1.7</source>  
                  <target>1.7</target>  
              </configuration>  
          </plugin> 
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <version>3.0</version>
                </configuration>
            </plugin>

          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8082</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
          </plugin>
  </plugins>
</build>
</project>

(2) springmvc.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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <mvc:annotation-driven >
        <!-- 转换器,过来的数据,转成utf-8 字符 -->
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />
            </bean>  
        </mvc:message-converters>   
    </mvc:annotation-driven>

       <!-- 注册中心注册 -->
       <dubbo:application name="dubbox-web"/>
       <!-- 注册中心,地址 -->
       <dubbo:registry address="zookeeper://192.168.25.130:2181"/>
       <!-- 扫描的包地址 -->
       <dubbo:annotation package="com.pinyougou.sellergoods.service.controller" />
</beans>

(3)代码实现
这里写图片描述
(4)代码地址 https://gitee.com/yuhaifei/dubboxdemo.git


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值