Dubbo
发表于|更新于
|字数总计:2.8k|阅读时长:13分钟|阅读量:
小总结
springboot,mybatis,dubbo+zookeep,三层架构开发
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 大概思路布局
 接口
 实体类
 service接口
 
 提供者
 mapper.java(接口)
 mapper.xml(mybatisSQL语句)
 service实现类
 
 消费者
 webController.java
 index.html
 
 | 
service层
| 12
 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层
| 12
 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接口
一.导入工程的核心包
| 12
 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>
 <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等核心依赖
| 12
 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
 
 | <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>
 
 <dependency>
 <groupId>com.alibaba.spring.boot</groupId>
 <artifactId>dubbo-spring-boot-starter</artifactId>
 <version>2.0.0</version>
 </dependency>
 
 <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>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>
 
 
 | 
配置application.properties/yml,设置数据库连接,dubbo服务等信息
| 12
 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=
 
 
 server.port=8090
 
 server.servlet.context-path=/
 
 
 
 spring.application.name=mycinema-provider
 
 spring.dubbo.server=true
 
 spring.dubbo.registry=zookeeper://localhost:2181
 
 | 
使用逆向工程生成实体类
数据访问接口,以及sql-mapper映射文件
- 在pom的中添加MyBatis逆向工程的插件 | 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | <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,内容如下: | 12
 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>
 
 <classPathEntry location="C:\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>
 
 <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>
 
 
 
 <javaModelGenerator targetPackage="com.powernode.model"
 targetProject="E:\mycinema-interface\src\main\java">
 <property name="enableSubPackages" value="false" />
 <property name="trimStrings" value="false" />
 </javaModelGenerator>
 
 
 
 <sqlMapGenerator targetPackage="com.powernode.mapper"                targetProject="src/main/java">
 <property name="enableSubPackages" value="false" />
 </sqlMapGenerator>
 
 
 
 <javaClientGenerator type="XMLMAPPER" targetPackage="com.powernode.mapper"    targetProject="src/main/java">
 <property name="enableSubPackages" value="false" />
 </javaClientGenerator>
 
 
 <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文件,手动指定资源路径
| 12
 3
 4
 5
 6
 7
 8
 
 | <resources><resource>
 <directory>src/main/java</directory>
 <includes>
 <include>**/*.xml</include>
 </includes>
 </resource>
 </resources>
 
 | 
@Component,dubbo服务注解@Service
| 12
 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);
 }
 }
 
 | 
| 12
 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配置支持注解 | 12
 3
 4
 5
 6
 7
 8
 9
 
 | @SpringBootApplication@MapperScan("com.powernode.mapper")
 
 @EnableDubboConfiguration
 public class MycinemaProviderApplication {
 public static void main(String[] args) {
 SpringApplication.run(MycinemaProviderApplication.class, args);
 }
 }
 
 |  
 
- 开启zooker服务,启动提供者项目 
创建服务消费者项目
mycinema-consumer
创建基于SpringBoot的web项目,添加以下必要的依赖
| 12
 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>
 
 <dependency>
 <groupId>com.alibaba.spring.boot</groupId>
 <artifactId>dubbo-spring-boot-starter</artifactId>
 <version>2.0.0</version>
 </dependency>
 
 <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添加配置信息
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | server.port=8080
 
 server.servlet.context-path=/
 
 
 spring.application.name=mycinema-consumer
 
 spring.dubbo.registry=zookeeper://localhost:2181
 
 | 
添加Controller,依赖服务提供者提供的服务信息
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | @Controllerpublic class IndexController {
 
 @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坐标
| 12
 3
 4
 
 | <dependency><groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 
 | 
二.添加视图配置
| 12
 
 | spring.thymeleaf.suffix=.htmlspring.thymeleaf.prefix=classpath:/templates/
 
 | 
三.编写控制器
| 12
 3
 4
 5
 
 | @GetMapping("/index")public String index(Model model){
 model.addAttribute("movies",movieService.getAll());
 return "index";
 }
 
 | 
四.在templates下面添加index.html
| 12
 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