目录
- 一、Zookeeper
- 1.1 下载
- 1.2 安装
- 1.3 修改配置文件
- 1.4 启动
- 二、Dubbo插件搭建
- 三、手动创建
- 3.1 创建项目
- 3.1.1 pom依赖
- 3.2 api模块
- 3.2.1 pom依赖
- 3.2.2 实体类
- 3.2.3 service接口
- 3.3 provider
- 3.3.1 pom依赖
- 3.3.2 配置文件
- 3.3.3 mapper
- 3.3.4 service实现类
- 3.3.5 启动类
- 3.4 consumer
- 3.4.1 pom依赖
- 3.4.2 配置文件
- 3.4.3 控制层
- 3.4.4 启动类
- 四、测试
一、Zookeeper
我选择的是3.6.3版本的zookeeper
1.1 下载
官网:https://zookeeper.apache.org/releases.html
下载
1.2 安装
将下载的压缩包进行解压
将zookeeper配置至环境变量
变量名 | 变量值 |
---|---|
ZOOKEEPER_HOME | D:\Program Files\apache-zookeeper-3.6.3-bin |
path中引入zookeeper
%ZOOKEEPER_HOME%\bin
%ZOOKEEPER_HOME%\conf
1.3 修改配置文件
将conf下的zoo_sample.cfg复制并命名为zoo.cfg
并添加配置内容:
dataDir=D:\\azookerpersstudy\\data
dataLogDir=D:\\azookerpersstudy\\log
1.4 启动
在zookeeper下的bin目录下
点击zkservice.cmd
二、Dubbo插件搭建
idea已经有dubbo插件IntellijDubboPlugin,可快速构建一个 dubbo 项目
Configure→Plugins
输入Dubbo,点击installed
安装后,在创建项目界面就会多一个Dubbo的选项,点击Next
点击Next
设置项目路径和名称
点击finish
创建成功后,结构如下:
三、手动创建
以下示例为Dubbo+Zookeeper+SpringBoot+Mybatis集成项目
3.1 创建项目
选择maven,创建父项目dubbo_demo
3.1.1 pom依赖
<groupId>com.tung</groupId>
<artifactId>dubbo_demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 此处是springboot的父工程,用于指定springboot 版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 统一版本管理 -->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<zk.version>3.6.2</zk.version>
<dubbo.version>2.7.7</dubbo.version>
<curator.framework.version>5.1.0</curator.framework.version>
<curator.recipes.version>5.1.0</curator.recipes.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.framework.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.recipes.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
3.2 api模块
创建dubbo_api模块
api模块提供接口和实体类
3.2.1 pom依赖
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.tung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_api</artifactId>
<name>dubbo_api</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
3.2.2 实体类
package com.tung.entity;
import java.io.Serializable;
/**
* @author tung
* @ClassName User
* @Description 类的说明
* @date 2022/12/29
*/
public class User implements Serializable {
private String id;
private String name;
private String password;
public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.2.3 service接口
package com.tung.service;
import com.tung.entity.User;
import java.util.List;
/**
* @author tung
* @ClassName UserService
* @Description 类的说明
* @date 2022/12/29
*/
public interface UserService {
List<User> findAll();
}
3.3 provider
创建dubbo_provider,操作跟dubbo_api一样。
3.3.1 pom依赖
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.tung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_provider</artifactId>
<name>dubbo_provider</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<!-- zookeeper和dubbo的集成包 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<!-- zookeeper和dubbo的集成包 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!-- 刚刚创建的api接口模块 -->
<dependency>
<groupId>com.tung</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo启动-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
3.3.2 配置文件
application.yml
server:
port: 8082
# 注册中心 zookeeper的地址
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
timeout: 20000
application:
name: dubbo-provider
scan:
base-packages: com.tung
protocol:
name: dubbo
port: 20880
mybatis:
config-location: classpath:mybatis-config.xml
spring:
datasource:
name : test
url : jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username : root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="true"/>
<!-- Sets the number of seconds the driver will wait for a response from the database -->
<setting name="defaultStatementTimeout" value="3000"/>
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<!-- Continue going here -->
</configuration>
3.3.3 mapper
package com.tung.mapper;
import com.tung.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author tung
* @ClassName UserMapper
* @Description 类的说明
* @date 2022/12/29
*/
@Mapper
public interface UserMapper {
@Select("select id,name,password from t_user")
List<User> findAll();
}
3.3.4 service实现类
package com.tung.service.impl;
import com.tung.entity.User;
import com.tung.mapper.UserMapper;
import com.tung.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author tung
* @ClassName UserServiceImpl
* @Description 类的说明
* @date 2022/12/29
*/
@Component
@DubboService(loadbalance = "roundrobin")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
3.3.5 启动类
package com.tung.service;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author tung
* @ClassName ProviderApplication
* @Description 类的说明
* @date 2022/12/29
*/
@SpringBootApplication
@MapperScan("com.tung.mapper")
public class ProviderApplication {
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class,args);
}
}
3.4 consumer
创建dubbo_consumer
3.4.1 pom依赖
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.tung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_consumer</artifactId>
<packaging>war</packaging>
<name>dubbo_consumer Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.tung</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.4.2 配置文件
application.yml
server:
port: 8083
# 注册中心 zookeeper的地址
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
timeout: 20000
application:
name: dubbo-consumer
scan:
base-packages: com.tung.controller
protocol:
name: dubbo-consumer
port: 20881
3.4.3 控制层
package com.tung.controller;
import com.tung.entity.User;
import com.tung.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author tung
* @ClassName UserController
* @Description 类的说明
* @date 2022/12/29
*/
@RestController
@RequestMapping("/user")
public class UserController {
@DubboReference
private UserService userService;
@GetMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
3.4.4 启动类
package com.tung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author tung
* @ClassName ConsumerApplication
* @Description 类的说明
* @date 2022/12/28
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class,args);
}
}
四、测试
依次启动provider、consumer
访问consumer暴露的接口
成功