SpringBoot2.x集成Dubbo及Mybatis详细介绍

前言

我的上一个原创博客详细介绍了SpringBoot1.x框架集成Dubbo及Mybatis
https://blog.csdn.net/For_niu/article/details/87875470

本篇文章将进详细介绍SpringBoot2.x集成Dubbo及Mybatis,重点说明不同点和变化。

一:服务提供者pom.xml变化

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

二:服务消费者pom.xml变化

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

三:服务提供者SpringBoot启动类变化

重点是这里:WebApplicationType.NONE

import com.alibaba.dubbo.container.Main;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@MapperScan("com.wangze.sb.orm.test")
@EnableDubboConfiguration
@SpringBootApplication(scanBasePackages = {"com.wangze.sb.test"})
public class TestServerApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder().sources(TestServerApplication.class).web(WebApplicationType.NONE).run(args);
        Main.main(null);
    }
}

四:服务消费者application.yml变化

《1》context-path增加了server节点

server:
  context-path: /sb-web

变为:

server:
  servlet:
    context-path: /sb-web

《2》security增加了spring节点

security:
  basic:
    enabled: false
  user:
    name: admin
    password: admin
    role: ADMIN

变为:

spring:
  security:
    basic:
      enabled: false
    user:
      name: admin
      password: admin
      role: ADMIN

五:Mybatis数据库连接配置变化

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pms

变为(禁用SSL):

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pms?useSSL=false

六:踩过的坑及总结

《1》:由于spring-boot-starter-logging日志冲突,排除jar引用

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.spring.boot.starter.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-logging</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

《2》:由于dubbo-spring-boot-starter中已有dubbo集成,不需要再单独引用dubbo

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

《3》:对于引用mysql-connector-java的引用

事项5.x版本(如:5.1.27)6.x版本(如:8.0.12)
spring.datasource.driver-class-namecom.mysql.jdbc.Drivercom.mysql.cj.jdbc.Driver(Driver的D一定要大写!)
spring.datasource.urluseUnicode=true&characterEncoding=utf8useUnicode=true&characterEncoding=utf8&serverTimezone=Shanghai&useSSL=false

《4》:SpringBoot整合Dubbo的几种方式

序号pom引用说明
第一种 groupIdio.dubbo.springboot这种方式集成了springboot+zk+dubbo
第二种 groupIdcom.alibaba.boot0.2.x和0.1.x,分别支持springboot2.x和springboot1.x
第三种 groupIdcom.alibaba.spring.boot阿里提供给apache并开源

《5》:启动Application.java服务时报错,如下:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

出现错误的原因是SpringBoot 应用程序启动时,根据自动装配机制去配置文件中找相关的数据库配置信息,如果找不到则抛异常,解决办法是:排除对JDBC 的自动装配。

//其中办法之一就是在启动类中增加下面配置。

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}, scanBasePackages = {"com.wangze.facade"})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值