Dubbo
一款分布式服务框架,作为阿里巴巴SOA服务化治理方案的核心框架,通过高性能和透明化的RPC实现服务的远程调用,对服务的负载均衡以及项目的耦合性提供很强的解决方式;
具体Dubbo的介绍和优缺点请自行搜索,本篇就主要写一下SpringBoot对Dubbo的整合,之前看网上有很多讲解的总感觉不是很清楚,不够详细,然后这篇主要用来边学习边回忆吧;
在以下项目搭建之前需要明白怎么
创建一个SpringBoot项目
,这里不做太详细解说
如果还不太明白怎么创建一个SpringBoot项目
,可以点这里
还需要知道SpringBoot整合Mybatis
,这里也是不做太多详细解说
如果不是太明白可以点这里
如果还不知道zookeeper怎么安装
的兄弟可以点这里,windows兄弟自己找一下哈sorry
本篇主要讲述SpringBoot使用Dubbo和Zookeeper远程调用项目搭建,通过Mybatis和Mysql来获取数据,主要使用maven聚合工程搭建
然后创建完一个项目以后,这时候把它目录里没用的东西都删掉,只留下有用的
然后这时候开始创建他的子模块,就是点他然后就和创建SpringBoot项目一样喽,创建完后目录结构就是我酱紫的(名字随便起)
哎~~这样对于项目的创建就已经完成了最重要的一步了,项目的创建已经完事,然后就开始狂配置吧继续往下走
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zyc</groupId>
<artifactId>zycdubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging><!--打包方式-->
<name>zycdubbo</name>
<description>zycdubbo - f</description>
<!--他的3个子模块-->
<modules>
<module>zyc_common</module>
<module>zyc_controller</module>
<module>zyc_service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo.version>2.5.5</dubbo.version>
<zkclient.version>0.10</zkclient.version>
<lombok.version>1.18.6</lombok.version>
</properties>
<dependencies>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--zookeeper的客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--添加其他依赖-->
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后父模块就可以不用管了,开始配置他的子模块们
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zyc</groupId>
<artifactId>zycdubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>com.zyc</groupId>
<artifactId>zyc_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><!--打包方式-->
<name>zyc_common</name>
<description>zyc_common in</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.zyc.entity;
import java.io.Serializable;
/**
* @description: 需要实现Serializable这个接口,因为dubbo是RPC框架,服务层和控制层通过网络传输,在网络中传输需要将实体对象来进行序列化然后去传输。
*
* @author zhaopeng
* @date 2019/4/11
*/
public class BaseEntity implements Serializable {
}
package com.zyc.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users extends BaseEntity{
private Integer userId;
private String userName;
private String password;
private String phone;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
这样公共common模块也就告一段落了,接下来就是配置服务层模块了
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zyc</groupId>
<artifactId>zycdubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zyc</groupId>
<artifactId>zyc_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><!--打包方式-->
<name>zyc_service</name>
<description>zyc_service in</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<zkclient.version>0.10</zkclient.version>
</properties>
<dependencies>
<!--引入公共包-->
<dependency>
<groupId>com.zyc</groupId>
<artifactId>zyc_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?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="provider"/>
<!-- 注册中心的ip地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="service"/>
<!-- 在20800端口发布服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 使用bean -->
<bean id="userService" class="com.zyc.service.impl.UserServiceImpl"/>
<!-- 声明需要导出的接口 -->
<dubbo:service interface="com.zyc.service.UserService" ref="userService"/>
</beans>
server:
port: 8082
servlet:
context-path: /
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://127.0.0.1:3306/zyc?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: 123456
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
## 该配置节点为独立的节点,有可能容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.zyc.entity # 注意:对应实体类的路径
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
returnPageInfo: check
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zyc.mapper.UserMapper">
<sql id="BASE_TABLE">
users
</sql>
<sql id="BASE_COLUMN">
userId,userName,password,phone
</sql>
<insert id="insert" parameterType="com.zyc.entity.Users">
INSERT INTO
<include refid="BASE_TABLE"/>
<trim prefix="(" suffix=")" suffixOverrides=",">
userName,password,
<if test="phone != null">
phone,
</if>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR},
<if test="phone != null">
#{phone, jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="selectUsers" resultType="com.zyc.entity.Users">
SELECT
<include refid="BASE_COLUMN"/>
FROM
<include refid="BASE_TABLE"/>
</select>
</mapper>
package com.zyc.mapper;
import com.zyc.entity.Users;
import java.util.List;
public interface UserMapper {
int insert(Users users);
List<Users> selectUsers();
}
package com.zyc.service.impl;
import com.zyc.entity.Users;
import com.zyc.mapper.UserMapper;
import com.zyc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @description: common中接口的实现类,写入逻辑
*
* @author zhaopeng
* @date 2019/4/12
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(Users user) {
return userMapper.insert(user);
}
@Override
public List<Users> findAllUser(int pageNum, int pageSize) {
List<Users> userDomains = userMapper.selectUsers();
return userDomains;
}
}
注意这两个注解
@ImportResourc
:引入配置文件
@MapperScan
:指定要扫描的Mapper类的包的路径
package com.zyc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
import java.io.IOException;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ImportResource({"classpath:spring-dubbo.xml"})
@MapperScan("com.zyc.mapper")
public class ZycServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ZycServiceApplication.class, args);
System.out.println("服务端启动成功!!!");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zyc</groupId>
<artifactId>zycdubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zyc</groupId>
<artifactId>zyc_controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zyc_controller</name>
<description>zyc_controller in</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<zkclient.version>0.10</zkclient.version>
</properties>
<dependencies>
<!--引入公共包-->
<dependency>
<groupId>com.zyc</groupId>
<artifactId>zyc_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!---->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?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"/>
<!-- 注册中心的ip地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 在20800端口发布服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 引用服务配置,用于创建一个远程服务代理,一个引用可以指向多个注册中心-->
<dubbo:reference id="userService" check="false" interface="com.zyc.service.UserService"/>
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="controller"/>
</beans>
server:
port: 8080
servlet:
context-path: /
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/zyc
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapping/*.xml #这里是mapper的配置文件
type-aliases-package: com.zyc.entity #这里是实体类的包
package com.zyc.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zyc.entity.Users;
import com.zyc.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author zhaopeng
* @description: 请求controller
* @date 2019/4/12
*/
@RestController
public class UserController {
@Resource
@Reference(version = "1.0.0", check = true)
private UserService userService;
@GetMapping("getUser/{name}")
public List<Users> user(@PathVariable("name") String name) {
//偷个小懒
System.out.println("获取" + name + "用户请求进来了");
System.out.println(userService.hashCode() + "~~~~~~~`");
return userService.findAllUser(1, 10);
}
}
以上代码不要着急不要慌,一步一步的做,只要每步都做ok了就一定会成功的,太顺利了和太不顺利都不是太好,如果那里报错了,或者出现什么问题欢迎评论抛出来
以上项目代码已经放到我的github上面了
链接放在了本篇文章开始
,后期还会对这个项目进行一些技术的扩展,比如Mq,检索引擎,以及一些其他的东西,
会在后续的文章中慢慢写出欢迎大佬指点
好的到这里本篇文章就先到此了,创作不易,如果那里有不合适的地方还请大家多多指教,写这篇博的目的主要就是为了方便自己以后的一个回忆和朋友学习时的一个参考,希望为大家可以带来帮助 ~ ~&
虚心的去学习,自信的去工作~