1.dubbo分布式框架理解
分布式系统:若干独立计算机的集合,这些计算机对于用户来说就像单个系统。
理解:一个系统分为若干个服务来部署,通过服务之间的调用来处理负载均衡,这些服务之间还有相互的联系,dubbo这种思想对于这种服务之间的治理、管理就有了很大的作用(consumer消费者和provider提供者)。
单一的应用架构 将系统统一打包部署在服务器上,这样有一个弊端,服务多了,服务器承载不了。另一点就是系统的扩展比较麻烦,改一个模块,都要将整个项目重新部署。所以做法是拆分成互相独立的子应用,独立的放在各自的服务器上。将页面和业务逻辑相分离。
RPC:远程过程调用(renmote procedure call)。(分布式的核心)
2.springboot理解
主要简化配置,ssm框架至少需要spring的配置文件和mvc的配置文件及mybatis的配置文件。springboot集成了这些,优化了配置工作。
3.mybatis
有注解方式和xml方式。
开始整合项目:(eclipse)
1.创建父项目springboot-dubbox-parent。父项目创建的是maven_project
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>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
<dubbo.version>0.2.0</dubbo.version>
<mybatis.version>1.3.2</mybatis.version>
<mysql.version>5.1.42</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springboot相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<!-- 静态资源访问 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${springboot.version}</version>
<scope>test</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- mybatis相关 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>SpringBoot-dubbox-pojo</module>
<module>SpringBoot-dubbox-dao</module>
<module>SpringBoot-dubbox-interface</module>
<module>SpringBoot-dubbox-serviceimpl</module>
<module>SpringBoot-dubbox-web</module>
</modules>
</project>
2.在父工程上右键>选中Maven Module->Next->选中Craete a simple project.....->输入Module Name为SpringBoot-dubbox-pojo工程
实体类也要的单独创建一个工程,便于前台后后台都依赖它。创建的实体类必须实现serializable接口。
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.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-dubbox-pojo</artifactId>
</project>
3.创建一个访问dao的工程。这个工程就访问数据库,应用mybatis.写一些mapper接口。
<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.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-dubbox-dao</artifactId>
<dependencies>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!-- 依赖于pojo -->
<dependency>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
4.由于Dubbo要求,服务的提供者与服务的消费者使用的接口必须在同一个包中,所以将服务接口单独抽成一个jar包,服务的提供者与服务的消费者均需要依赖于这个jar包。
主要写一些接口,消费者去调用它,它去调用服务提供者。
<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.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-dubbox-interface</artifactId>
<dependencies>
<dependency>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5.然后就是消费者和提供者。该demo只有一个消费者和一个提供者。
--服务的提供者配置:application.properties
#\u914D\u7F6Edubbo
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
dubbo.application.name=SpringBoot-dubbox-service
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.basePackages=com.peng.demo.service.impl
#\u52A0\u8F7DDao\u4E2D\u7684\u914D\u7F6E\u6587\u4EF6
spring.profiles.include=db
--配置 application.properties服务的消费者
dubbo.application.name=SpringBoot-dubbox-web
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.basePackages=com.peng.demo.controller
server.port=8081
--增加mvc配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
提供者pom.xml:依赖dao层和接口层
<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.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-dubbox-serviceimpl</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 配置打包后,运行的入口 -->
<configuration>
<mainClass>com.peng.demo.SpringBootDubboxService</mainClass>
</configuration>
<!-- 工程依赖了parent是需要添加 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
--消费者的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.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-dubbox-web</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.peng.demo</groupId>
<artifactId>SpringBoot-dubbox-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 配置打包后,运行的入口 -->
<configuration>
<mainClass>com.peng.demo.SpringBootDubboxWeb</mainClass>
</configuration>
<!-- 工程依赖了parent是需要添加 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.peng.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.peng.demo.pojo.TbUser;
import com.peng.demo.service.UserService;
@Controller
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/categories/{id}")
@ResponseBody
public List<TbUser> findById(Integer id){
List<TbUser> list = new ArrayList<TbUser>();
list.add(userService.findById(id));
return list;
}
@GetMapping("/categories")
public String listCategory(Model m) {
List<TbUser> page = userService.findAll();
m.addAttribute("page", page);
return "listCategory";
}
}
6.测试
将整体工程先安装到maven仓库,父工程右键->Run as->Maven install。。先启动SpringBoot-dubbox-service工程,运行SpringBootDubboxService的main方法。。在启动SpringBoot-dubbox-web工程,运行SpringBootDubboxWeb的main方法
然后访问:http://localhost:8081/categories/
7.遇到的问题:
1.jdk问题,各个项目要使用一致的版本。
2.@Controller和@Recontroller的区别,后者无法返回到前台页面。
3.警告:Establishing SSL connection without server’s identity verification is not recommended
解决方法:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
4.找不到mapper文件,添加mapperscan注解
5.服务者及接口消费者都依赖pojo。消费者依赖服务者的接口。服务者依赖接口和dao。
参考博客: