SpringBoot+MySQL8+Mybatis+数据库连接池+Dubbo 实现分布式服务管理解析

熊孩纸 阅读:249 2021-03-31 20:58:17 评论:0

 

1、项目结构图:

2、子模块功能说明:

mysql-boot :springboot基础环境依赖
mysql-boot-api :无
mysql-boot-dao :依赖 mysql-boot-api 和mybatis-spring-boot-starter(springboot与mybatis 集成)
mysql-boot-service:依赖api 、dao、数据库连接池(druid)、mysql驱动、dubbo集成
mysql-boot-web:依赖api 、dubbo集成

本文重点讲解mysql-boot-service和mysql-boot-web 子项目

mysql-boot-service的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.zzg</groupId> 
		<artifactId>mysql-boot</artifactId> 
		<version>0.0.1-SNAPSHOT</version> 
	</parent> 
	<artifactId>mysql-boot-service</artifactId> 
 
	<dependencies> 
		<!--api 层依赖  --> 
		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-api</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--dao 层依赖  --> 
		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-dao</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!-- 数据库连接池druid --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>druid-spring-boot-starter</artifactId> 
			<version>1.1.10</version> 
		</dependency> 
		<!--mysql 驱动程序 --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<version>8.0.12</version> 
		</dependency> 
		 
		<!--dubbo 服务集成  --> 
		<dependency> 
            <groupId>com.alibaba</groupId> 
            <artifactId>dubbo</artifactId> 
            <version>2.8.4</version> 
            <exclusions> 
                <exclusion> 
                    <artifactId>spring</artifactId> 
                    <groupId>org.springframework</groupId> 
                </exclusion> 
            </exclusions> 
        </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> 
  
        <dependency> 
            <groupId>com.github.sgroschupf</groupId> 
            <artifactId>zkclient</artifactId> 
            <version>0.1</version> 
        </dependency> 
 
	</dependencies> 
</project>

mysql-boot-web的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.zzg</groupId> 
    <artifactId>mysql-boot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
  </parent> 
  <artifactId>mysql-boot-web</artifactId> 
  <dependencies> 
  		<!--依赖api层  --> 
  		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-api</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--dubbo 服务依赖  --> 
		<dependency> 
            <groupId>com.alibaba</groupId> 
            <artifactId>dubbo</artifactId> 
            <version>2.8.4</version> 
            <exclusions> 
                <exclusion> 
                    <artifactId>spring</artifactId> 
                    <groupId>org.springframework</groupId> 
                </exclusion> 
            </exclusions> 
        </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> 
  
        <dependency> 
            <groupId>com.github.sgroschupf</groupId> 
            <artifactId>zkclient</artifactId> 
            <version>0.1</version> 
        </dependency> 
  </dependencies> 
</project>

mysql-boot-service的核心代码:

package com.zzg; 
 
import org.mybatis.spring.annotation.MapperScan; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ImportResource; 
 
@SpringBootApplication 
@MapperScan("com.zzg.mapper") 
@ImportResource({"classpath:dubbo-provider.xml"}) 
public class Application { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		SpringApplication.run(Application.class, args); 
	} 
 
} 
package com.zzg.service; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
 
import com.zzg.api.BookService; 
import com.zzg.entity.Book; 
import com.zzg.mapper.BookMapper; 
 
@Service 
public class BookServiceImpl implements BookService{ 
	@Autowired 
	private BookMapper mapper; 
 
	@Override 
	public List<Book> getBooks() { 
		// TODO Auto-generated method stub 
		return mapper.getBooks(); 
	} 
 
} 
server.port=8011 
# MyBatis mysql8 配置 
spring.datasource.url=jdbc:mysql://******:3306/blog?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true 
spring.datasource.username=root 
spring.datasource.password=***** 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
 
# Druid 配置 
# 初始化时建立物理连接的个数 
spring.datasource.druid.initial-size=5 
# 最大连接池数量 
spring.datasource.druid.max-active=30 
# 最小连接池数量 
spring.datasource.druid.min-idle=5 
# 获取连接时最大等待时间,单位毫秒 
spring.datasource.druid.max-wait=60000 
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 
spring.datasource.druid.time-between-eviction-runs-millis=60000 
# 连接保持空闲而不被驱逐的最小时间 
spring.datasource.druid.min-evictable-idle-time-millis=300000 
# 用来检测连接是否有效的sql,要求是一个查询语句 
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL 
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 
spring.datasource.druid.test-while-idle=true 
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 
spring.datasource.druid.test-on-borrow=false 
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 
spring.datasource.druid.test-on-return=false 
# 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 
spring.datasource.druid.pool-prepared-statements=true 
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。 
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50 
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计 
spring.datasource.druid.filters=stat,wall 
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录 
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 
# 合并多个DruidDataSource的监控数据 
spring.datasource.druid.use-global-data-source-stat=true

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="common-service"  /> 
  
    <!-- 注册中心服务地址 --> 
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://192.168.1.74:2181" /> 
  
    <!-- 用dubbo协议在30001 --> 
    <dubbo:protocol name="dubbo" port="20882" /> 
  
    <!-- 声明需要暴露的服务接口 --> 
    <dubbo:service interface="com.zzg.api.BookService" ref="bookService" timeout="10000"/> 
  
    <!-- 具体服务接口的实现 --> 
    <bean id="bookService" class="com.zzg.service.BookServiceImpl" /> 
  
</beans>

mysql-boot-web的核心代码:

package com.zzg; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ImportResource; 
 
@SpringBootApplication 
@ImportResource({"classpath:dubbo-consumer.xml"}) 
public class Application { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		SpringApplication.run(Application.class, args); 
	} 
 
} 
package com.zzg.controller; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
import com.zzg.api.BookService; 
import com.zzg.entity.Book; 
 
@Controller 
@RequestMapping 
public class BookController { 
	@Autowired 
	private BookService service; 
	 
	@RequestMapping(value = "/books", method = { RequestMethod.GET }) 
	@ResponseBody 
	public List<Book> getBooks() { 
		List<Book> result = service.getBooks(); 
		return result; 
	} 
} 

dubbo-consumer.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="consumer"  /> 
  
    <!-- 注册中心服务地址 --> 
    <dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://192.168.1.74:2181" /> 
  
    <!-- 引用ComputeService服务--> 
    <dubbo:reference id="bookService" interface="com.zzg.api.BookService"/> 
                      
  
</beans>

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们