小总结

springboot,mybatis,dubbo+zookeep,三层架构开发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
大概思路布局

接口
实体类
service接口

提供者
mapper.java(接口)
mapper.xml(mybatisSQL语句)
service实现类

消费者
webController.java
index.html

service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Application启动文件执行以下添加以下

@SpringBootApplication //springboot标签
@MapperScan("io.peng.mapper") //扫描mapper文件夹
@EnableDubboConfiguration //启动dubbo注解

---
application.properties配置文件文件添加以下

#设置服务端口号
server.port=8081
#设置上下文根
server.servlet.context-path=/

#链接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/air
spring.datasource.username=root
spring.datasource.password=123456

#扫描xml文件路径
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
#mapper别名
mybatis.type-aliases-package=io.peng.money.model

#配置 Dubbo 的服务提供者名称
spring.application.name=airservice
#设置工程为服务提供者
spring.dubbo.server=true
#设置 dubbo 注册中心
spring.dubbo.registry=zookeeper://localhost:2181

#redis
spring.redis.host=localhost
spring.redis.port=6379

---
实现接口添加以下
@Component //让spring实例化
// 使用dubbo的service,让他托管
@Service(interfaceClass = xxxxxService.class,version = "1.0.0",timeout = 15000)
public class XXXXXXXX

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Application启动文件执行以下添加以下

@SpringBootApplication //springboot标签
@EnableDubboConfiguration //启动dubbo注解

---
application.properties配置文件添加以下
server.port=8080 //端口
spring.application.name=web //名字
// 注册中心
spring.dubbo.registry=zookeeper://localhost:2181

---
web需要加这些
@RestController
public class xxxxxxController {
@Reference(interfaceClass=AirQualityIndexServer.class,version="1.0.0")
private XXXXService XXXXService;

xxxx
}

开源地址

https://github.com/apache/dubbo

接口工程

使用SpringBoot搭建基于SSM的分布式框架。按照dubbo官方的推荐,一个基于dubbo的微服务项目至少包含三个模块。接口模块,生产者模块,消费者模块。

搭建Maven的java工程——dubbo接口

一.导入工程的核心包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

搭建提供者项目

创建基于SpringBoot的提供者web项目,提供数据服务

导入MyBatis,dubbo,以及zookeeper等核心依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot集成dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--zookeeper客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--myincema服务接口依赖-->
<dependency>
<groupId>com.powernode</groupId>
<artifactId>mycinema-interface</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

配置application.properties/yml,设置数据库连接,dubbo服务等信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#数据库信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mycinema?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=

#设置内嵌 Tomcat 端口号
server.port=8090
#设置上下文根
server.servlet.context-path=/

#配置 dubbo 的服务提供者信息
#服务提供者应用名称(必须写,且不能重复)
spring.application.name=mycinema-provider
#设置当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

使用逆向工程生成实体类

数据访问接口,以及sql-mapper映射文件

  • 在pom的中添加MyBatis逆向工程的插件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!--mybatis代码自动生成插件-->
    <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
    <!--配置文件的位置-->
    <configurationFile>GeneratorMapper.xml</configurationFile>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
    </plugin>
  • 在项目的根目录下面添加插件的配置文件GeneratorMapper.xml,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

    <generatorConfiguration>
    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="C:\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>
    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">

    <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
    <commentGenerator>
    <property name="suppressAllComments" value="true" />
    </commentGenerator>

    <!-- 配置数据库连接信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://127.0.0.1:3306/mycinema"
    userId="root"
    password="">
    </jdbcConnection>

    <!-- 生成model类,targetPackage指定model类的包名,
    targetProject指定生成的model放在接口工程下面-->
    <javaModelGenerator targetPackage="com.powernode.model"
    targetProject="E:\mycinema-interface\src\main\java">
    <property name="enableSubPackages" value="false" />
    <property name="trimStrings" value="false" />
    </javaModelGenerator>

    <!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名,
    targetProject指定生成的mapper.xml放在当前工程src -->
    <sqlMapGenerator targetPackage="com.powernode.mapper" targetProject="src/main/java">
    <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>

    <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名,
    targetProject指定生成的Mapper接口放当前工程src目录 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.powernode.mapper" targetProject="src/main/java">
    <property name="enableSubPackages" value="false" />
    </javaClientGenerator>

    <!-- 数据库表名及对应的Java模型类名 -->
    <table tableName="category" domainObjectName="Category"
    enableCountByExample="false"
    enableUpdateByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false"
    selectByExampleQueryId="false"/>
    <table tableName="movie" domainObjectName="Movie"
    enableCountByExample="false"
    enableUpdateByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false"
    selectByExampleQueryId="false"/>

    <table tableName="user" domainObjectName="User"
    enableCountByExample="false"
    enableUpdateByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false"
    selectByExampleQueryId="false"/>
    </context>
    </generatorConfiguration>
  • 在maven窗口中执行插件,生成代码
    注意:执行插件时,会解析pom文件中的所有依赖,已经导入了的依赖,所以会去本地仓库查找这个包,找不到就会报错。解决方法如下:
    1.可以先注释这个包依赖,等生成代码之后再添加
    2.将mycinema-interface项目打包安装到本地仓库mvn install
    3.将生成的实体类都实现序列化接口,用于远程传输数据

    1
    public class Movie implements Serializable {
  • 插件生成的Mapper.xml文件和Mapper接口在同一个包下,但是idea不会编译,需要手动指定资源文件,修改

提供者mycinema-provider的pom.xml文件,手动指定资源路径

1
2
3
4
5
6
7
8
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
  • 在服务接口工程mycinema-interface工程中添加接口内容

    1
    2
    3
    4
    5
    6
    7
    8
    public interface CategoryService {
    List<Category> getAll();
    Category getById(Integer id);
    }
    public interface MovieService {
    List<Movie> getAll();
    Movie getById(Integer id);
    }
  • 在提供者工程mycinema-provider中创建业务的实现包service.impl实现业务接口,并添加业务注解

@Component,dubbo服务注解@Service

1
2
3
4
5
6
7
8
9
10
11
@Component
@Service(interfaceClass = MovieService.class,version = "1.0.0",timeout = 15000)
public class MovieServiceImpl implements MovieService {
@Autowired private MovieMapper movieMapper;
@Override public List<Movie> getAll() {
return movieMapper.selectAll();
}
@Override public Movie getById(Integer id) {
return movieMapper.selectByPrimaryKey(id);
}
}
1
2
3
4
5
6
7
8
9
10
11
@Component
@Service(interfaceClass = CategoryService.class,timeout = 15000,version = "1.0.0")
public class CategoryServiceImpl implements CategoryService {
@Autowired private CategoryMapper categoryMapper;
@Override public List<Category> getAll() {
return categoryMapper.selectAll();
}
@Override public Category getById(Integer id) {
return categoryMapper.selectByPrimaryKey(id);
}
}
  • 在启动类上开启dubbo配置支持注解

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @SpringBootApplication
    @MapperScan("com.powernode.mapper")
    //扫描数据访问接口
    @EnableDubboConfiguration //开启dubbo注解支持
    public class MycinemaProviderApplication {
    public static void main(String[] args) {
    SpringApplication.run(MycinemaProviderApplication.class, args);
    }
    }
  • 开启zooker服务,启动提供者项目

创建服务消费者项目

mycinema-consumer

创建基于SpringBoot的web项目,添加以下必要的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--zookeeper客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--服务接口依赖-->
<dependency>
<groupId>com.powernode</groupId>
<artifactId>mycinema-interface</artifactId>
<version>1.0.0</version>
</dependency>

修改application.properties添加配置信息

1
2
3
4
5
6
7
8
9
#设置内嵌 Tomcat 端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/
#设置 dubbo 配置
#设置服务消费者名称
spring.application.name=mycinema-consumer
#配置 dubbo 注册中心
spring.dubbo.registry=zookeeper://localhost:2181

添加Controller,依赖服务提供者提供的服务信息

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
public class IndexController {
//组件由dubbo提供,使用@Reference注解
@Reference(interfaceClass = MovieService.class,version = "1.0.0",check = false)
private MovieService movieService;

@RequestMapping("/findAll")
@ResponseBody
public List<Movie> findAll(){
return movieService.getAll();
}
}

测试

集成Thymeleaf实现同步请求

一.添加Thymeleaf坐标

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二.添加视图配置

1
2
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/

三.编写控制器

1
2
3
4
5
@GetMapping("/index")
public String index(Model model){
model.addAttribute("movies",movieService.getAll());
return "index";
}

四.在templates下面添加index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr><td>编号</td><td>名称</td><td>导演</td><td>日期</td></tr>
<tr th:each="movie:${movies}">
<td th:text="${movie.id}"></td>
<td th:text="${movie.title}"></td>
<td th:text="${movie.director}"></td>
<td th:text="${movie.datereleased}"></td>
</tr>
</table>
</body>
</html>

Dubbo监控中心

开源地址:https://github.com/apache/dubbo-admin

Dubbo是一个分布式服务框架,能避免单点故障和支持服务的横向扩容。一个服务通常会部署多个实例。
如何从多个服务 Provider 组成的集群中挑选出一个进行调用,就涉及到一个负载均衡的策略。

新版控制中心前后端分离了!!!
1.下载源代码
2.进入dubbo-admin-server,打包
mvn clean package
3.启动zookeeper
4.启动dubbo-admin-server,端口http:8080
java -jar dubbo-admin-server…jar
5.进入dubbo-admin-ui
下载js包
npm install
6.编译执行 npm run dev
7.http://localhost:8082