您的位置:首页 > 编程语言 > Java开发

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

2019-03-04 20:01 387 查看

前言

我的上一个原创博客详细介绍了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-name com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver(Driver的D一定要大写!)
spring.datasource.url useUnicode=true&characterEncoding=utf8 useUnicode=true&characterEncoding=utf8&serverTimezone=Shanghai&useSSL=false

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

序号 pom引用 说明
第一种 groupId io.dubbo.springboot 这种方式集成了springboot+zk+dubbo
第二种 groupId com.alibaba.boot 0.2.x和0.1.x,分别支持springboot2.x和springboot1.x
第三种 groupId com.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"})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: