阿里巴巴提供了dubbo集成springBoot開源項(xiàng)目,可以到GitHub上https://github.com/alibaba/dubbo-spring-boot-starter查看入門教程。
1.開發(fā)Dubbo服務(wù)接口
按照Dubbo官方開發(fā)建議,創(chuàng)建一個接口項(xiàng)目,該項(xiàng)目只定義接口和model類。
項(xiàng)目名稱:019-springboot-dubbo-exterface
① 創(chuàng)建普通Maven項(xiàng)目,dubbo服務(wù)接口工程
② 創(chuàng)建UserService接口
public interface UserService {
String say(String name);
}
2.開發(fā)Dubbo服務(wù)提供者
項(xiàng)目名稱:021-springboot-dubbo-provider
① 創(chuàng)建SpringBoot WEB項(xiàng)目
② 加入springboot與dubbo集成的起步依賴
<!--Spring Boot集成Dubbo的起步依賴-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
③ 由于使用zookeeper作為注冊中心,需加入zookeeper的客戶端依賴
<!--ZooKeeper注冊中心依賴-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
④ 加入019-springboot-dubbo-exterface接口依賴
<!--Dubbo接口工程依賴-->
<dependency>
<groupId>com.bjpowernode.springboot</groupId>
<artifactId>019-springboot-dubbo-exterface</artifactId>
<version>1.0.0</version>
</dependency>
⑤ 在Springboot的核心配置文件application.properties中配置dubbo的信息
#配置內(nèi)嵌Tomcat端口號
server.port=9090
#配置項(xiàng)目上下文根
server.servlet.context-path=/020-springboot-dubbo-provider
#配置Dubbo服務(wù)提供者配置
#服務(wù)提供者應(yīng)用名稱必須寫,且不能重復(fù)
spring.application.name=020-springboot-dubbo-provider
#表示是服務(wù)提供者,可以省略
spring.dubbo.server=true
#注冊中心地址
spring.dubbo.registry=zookeeper://192.168.92.134:2181
注意:Dubbo的注解都是自定義的注解,由我們添加的Dubbo依賴中的類進(jìn)行處理編寫dubbo配置是沒有提示的
⑥ 編寫Dubbo的接口實(shí)現(xiàn)類
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
//@Service是由阿里提供的注解,不是spring的注冊
//@Service相當(dāng)于
//如果提供者指定version,那么消費(fèi)者也得指定version
@Service(interfaceClass = UserService.class,version = "1.0.0",timeout = 15000)
@Component //將接口實(shí)現(xiàn)類交給spring容器進(jìn)行管理
public class UserServiceImpl implements UserService {
@Override
public String say(String name) {
return "Hello SpringBoot!";
}
}
⑦ 在SpringBoot入口程序類上加開啟Dubbo配置支持注解
@SpringBootApplication
@EnableDubboConfiguration//開啟Dubbo配置支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
⑧ 啟動Zookeeper服務(wù)
• 啟動Linux服務(wù)器上的Zookeeper
• 啟動服務(wù)提供者項(xiàng)目主程序
3.開發(fā)Dubbo服務(wù)消費(fèi)者
項(xiàng)目名稱:021-springboot-dubbo-consumer
① 創(chuàng)建SpringBoot WEB項(xiàng)目
② 加入springboot與dubbo集成的起步依賴
<!--Spring Boot集成Dubbo的起步依賴-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
③ 由于使用zookeeper作為注冊中心,需加入zookeeper的客戶端依賴
<!--ZooKeeper注冊中心依賴-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
④ 加入019-springboot-dubbo-exterface接口依賴
<!--Dubbo接口工程依賴-->
<dependency>
<groupId>com.bjpowernode.springboot</groupId>
<artifactId>019-springboot-dubbo-exterface</artifactId>
<version>1.0.0</version>
</dependency>
⑤ 在Springboot的核心配置文件application.properties中配置dubbo的信息
#配置內(nèi)嵌Tomcat端口號
server.port=8080
#配置項(xiàng)目上下文根
server.servlet.context-path=/021-springboot-dubbo-consumer
#配置dubbo服務(wù)提供者配置
spring.application.name=021-springboot-dubbo-consumer
#配置注冊中心地址
spring.dubbo.registry=zookeeper://192.168.92.134:2181
⑥ 編寫一個Controller類,調(diào)用遠(yuǎn)程的Dubbo服務(wù)
import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.springboot.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class UserController {
//@Reference注冊是由阿里所提供
//@Reference注解 相當(dāng)于
//如果服務(wù)提供者
@Reference(version = "1.0.0")
private UserService userService;
@RequestMapping(value = "/springBoot/hello")
public Object hello(HttpServletRequest request) {
String sayHello = userService.say("SpringBoot");
return sayHello;
}
}
⑦ 在SpringBoot入口程序類上加開啟Dubbo配置支持注解
@SpringBootApplication
@EnableDubboConfiguration //開啟Dubbo配置支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
⑧ 測試
• 啟動服務(wù)消費(fèi)者項(xiàng)目主程序
• 瀏覽器訪問測試
該實(shí)例目的是為了讓同學(xué)們快速學(xué)會使用SpringBoot集成搭建SpringMVC、Spring、MyBatis及Redis
1.創(chuàng)建Maven Java工程,Dubbo接口工程
項(xiàng)目名稱:022-springboot-ssm-dubbo-exterface
2.創(chuàng)建StudentService業(yè)務(wù)接口類
package com.abc.springboot.service;
import com.abc.springboot.model.Student;
import java.util.List;
/**
* ClassName:StudentService
* Package:com.abc.springboot.service
* Description:
*/
public interface StudentService {
/**
* 查詢所有學(xué)生
* @return
*/
List queryAllStudent();
/**
* 獲取學(xué)生總?cè)藬?shù)
* @return
*/
Long queryAllStudentCount();
}
3.創(chuàng)建Dubbo服務(wù)提供者
項(xiàng)目名稱:023-springboot-ssm-dubbo-provider
4.給Dubbo服務(wù)提供者添加依賴
<!--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>
<!--MyBatis集成SpringBoot框架起步依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--連接MySQL數(shù)據(jù)庫驅(qū)動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--SpringBoot集成Redis起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!—dubbo接口工程 -->
<dependency>
<groupId>com.abc.springboot</groupId>
<artifactId>022-springboot-ssm-dubbo-exterface</artifactId>
<version>1.0.0</version>
</dependency>
5.手動指定資源配置文件路徑在pom文件中的build標(biāo)簽中添加
<!--手動指定資源配置文件路徑-->
<!--目的:將數(shù)據(jù)持久層映射文件編譯到classpath中-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
6.配置MyBatis逆向工程
① 添加插件
<!--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>
② 將配置文件存放到項(xiàng)目根據(jù)目錄
③ GeneratorMapper.xml內(nèi)容如下
<?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>
<!-- 指定連接數(shù)據(jù)庫的JDBC驅(qū)動包所在位置,指定到你本機(jī)的完整路徑 -->
<classPathEntry location="E:\mysql-connector-java-5.1.38.jar"/>
<!-- 配置table表信息內(nèi)容體,targetRuntime指定采用MyBatis3的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注釋,由于生成的注釋都是英文的,可以不讓它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置數(shù)據(jù)庫連接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.92.134:3306/springboot"
userId="root"
password="123456">
</jdbcConnection>
<!-- 生成model類,targetPackage指定model類的包名, targetProject指定生成的model放在eclipse的哪個工程下面-->
<javaModelGenerator targetPackage="com.abc.springboot.model"
targetProject="E:\course\031-SpringBoot\000-springboot-projects\022-springboot-ssm-dubbo-exterface\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪個工程下面 -->
<sqlMapGenerator targetPackage="com.abc.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成MyBatis的Mapper接口類文件,targetPackage指定Mapper接口類的包名, targetProject指定生成的Mapper接口放在eclipse的哪個工程下面 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.abc.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 數(shù)據(jù)庫表名及對應(yīng)的Java模型類名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
④ 點(diǎn)擊插件生成
⑤ 實(shí)體beann必須實(shí)現(xiàn)序列化
7.配置023-springboot-ssm-dubbo-provider核心配置文件
application.properties
#配置內(nèi)嵌Tomcat端口號
server.port=8081
#配置項(xiàng)目上下文根
server.servlet.context-path=023-springboot-ssm-dubbo-provider
#配置連接MySQL數(shù)據(jù)庫信息
spring.datasource.url=jdbc:mysql://192.168.92.134:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#配置dubbo服務(wù)提供者
spring.application.name=023-springboot-ssm-dubbo-provider
#表示是服務(wù)提供者
spring.dubbo.server=true
#注冊中心地址
spring.dubbo.registry=zookeeper://192.168.92.134:2181
#配置redis連接信息
spring.redis.host=192.168.92.134
spring.redis.port=6379
spring.redis.password=123456
8.StudentServiceImpl業(yè)務(wù)接口實(shí)現(xiàn)類
package com.abc.springboot.service;
import com.abc.springboot.mapper.StudentMapper;
import com.abc.springboot.model.Student;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* ClassName:StudentServiceImpl
* Package:com.abc.springboot.service
* Description:<br/>
*/
@Service(interfaceName = "com.abc.springboot.service.StudentService",version = "1.0.0",timeout = 15000)
@Component
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public List<Student> queryAllStudent() {
return studentMapper.selectAllStudent();
}
@Override
public Long queryAllStudentCount() {
//設(shè)置redisTemplate的key序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
//從redis中獲取學(xué)生總?cè)藬?shù)
Long allStudentCount = (Long) redisTemplate.opsForValue().get("allStudentCount");
//判斷是否有值
if (null == allStudentCount) {
//去數(shù)據(jù)庫查詢
allStudentCount = studentMapper.selectAllStudentCount();
//將學(xué)生總?cè)藬?shù)存放到redis緩存中
redisTemplate.opsForValue().set("allStudentCount",allStudentCount,15, TimeUnit.MINUTES);
}
return allStudentCount;
}
}
9.配置023-springboot-ssm-dubbo-provider啟動類
@SpringBootApplication
@EnableDubboConfiguration //開啟dubbo支持配置
@MapperScan(basePackages = "com.abc.springboot.mapper") //掃描數(shù)據(jù)持久層映射文件
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
10.創(chuàng)建Dubbo服務(wù)消費(fèi)者
項(xiàng)目名稱:024-springboot-ssm-dubbo-consumer
11.給Dubbo服務(wù)消費(fèi)者添加依賴
<!--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>
<!--MyBatis集成SpringBoot框架起步依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--連接MySQL數(shù)據(jù)庫驅(qū)動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--SpringBoot集成Redis起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--dubbo接口工程-->
<dependency>
<groupId>com.abc.springboot</groupId>
<artifactId>022-springboot-ssm-dubbo-exterface</artifactId>
<version>1.0.0</version>
</dependency>
12.配置024-springboot-ssm-dubbo-consumer核心配置文件
配置內(nèi)嵌Tomcat端口號
server.port=8080
#配置項(xiàng)目上下文根
server.servlet.context-path=024-springboot-ssm-dubbo-consumer
#配置zookeeper注冊中心
spring.application.name=024-springboot-ssm-dubbo-consumer
spring.dubbo.registry=zookeeper://192.168.92.134:2181
#配置SpringMVC的視圖解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
13.StudentController控制層
package com.abc.springboot.web;
import com.abc.springboot.model.Student;
import com.abc.springboot.service.StudentService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ClassName:StudentController
* Package:com.abc.springboot.web
* Description:<br/>
*/
@RestController
public class StudentController {
@Reference(interfaceName = "com.abc.springboot.service.StudentService",version = "1.0.0",check = false)
private StudentService studentService;
@RequestMapping(value = "/springBoot/students")
public Object students() {
List<Student> studentList = studentService.queryAllStudent();
return studentList;
}
@RequestMapping(value = "/springBoot/student/count")
public Object studentCount() {
Long allStudentCount = studentService.queryAllStudentCount();
return "學(xué)生總?cè)藬?shù)為:" + allStudentCount;
}
}
14.配置024-springboot-ssm-dubbo-consumer啟動類
@SpringBootApplication
@EnableDubboConfiguration //開啟dubbo支持配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
15.啟動023-springboot-ssm-dubbo-provider
16.啟動024-springboot-ssm-dubbo-consumer
17.啟動瀏覽器進(jìn)行測試
在啟動項(xiàng)目的時候,控制臺出現(xiàn)如下警告信息
主要是因?yàn)閆ookeeper包中,slf4j-log4j12和log4j沖突了,需要處理一下;
在服務(wù)提供者和消費(fèi)中的pom.xml文件的ZooKeeper依賴中添加如下內(nèi)容。
<!--zookeeper注冊中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
替換后,重啟服務(wù)進(jìn)行測試。