創(chuàng)建一個接口項目,12-springboot-dubbo-interface,該項目只定義接口和model類
1、創(chuàng)建普通的Maven項目,dubbo服務接口工程
2、創(chuàng)建 UserService 接口
創(chuàng)建service包,在這個包下寫
package service;/** * @author MD * @create 2020-08-22 6:31 */public interface StudentService { /** * 獲取學生總人數(shù) * @return */ Integer queryAllStudentCount();}
創(chuàng)建 SpringBoot 框架的 WEB 項目,13-springboot-dubbo-provider
2、依賴
加入 Dubbo 集成 SpringBoot 的起步依賴
由于使用 zookeeper 作為注冊中心,需加入 zookeeper 的客戶端
加入 Dubbo 接口依賴
<!--Dubbo 集成 SpringBoot 框架起步依賴--><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><!--dubbo接口依賴--> <dependency> <groupId>com.md.springboot</groupId> <artifactId>12-springboot-dubbo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3、在Springboot 的核心配置文件
application.properties中配置 dubbo
#設置內嵌Tomcatserver.port=8081#設置上下文根server.servlet.context-path=/#配置 dubbo 的服務提供者信息#服務提供者應用名稱(必須寫,且不能重復)spring.application.name=springboot-dubbo-provider#設置當前工程為服務提供者spring.dubbo.server=true#設置注冊中心spring.dubbo.registry=zookeeper://localhost:2181
注意:Dubbo 的 的注解都是自定義的注解,由我們添加的 Dubbo 依賴中的類 進行 處理 編寫dubbo 配置是沒有提示的
4、編寫 Dubbo 的接口實現(xiàn)類,并暴露接口
在com.md.springboot.service.impl下面
package com.md.springboot.service.impl;import com.alibaba.dubbo.config.annotation.Service;import org.springframework.stereotype.Component;import service.StudentService;/** * @author MD * @create 2020-08-22 6:46 */@Component// 也可以這樣寫,寫接口的權限定類名//@Service(interfaceName ="com.md.springboot.service.StudentService",version = "1.0.0",timeout= 15000)//暴露出接口的類名.class,版本號,@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)public class StudentServiceImpl implements StudentService { @Override public Integer queryAllStudentCount() { //調用數(shù)據(jù)持久層,經過一系列操之后得到學生總人數(shù) return 200; }}
注意使用service注解的時候:使用alibaba的這個
5、SpringBoot 入口 程序啟 類上加開啟 Dubbo 配置支持注解
package com.md.springboot;import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubboConfiguration // 開啟Dubbo配置public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
創(chuàng)建 SpringBoot 框架的 WEB 項目,14-springboot-dubbo-comsumer
2、依賴pom.xml
加入 Dubbo 集成 SpringBoot 的起步依賴
由于使用 zookeeper 作為注冊中心,需加入 zookeeper 的客戶端
加入 Dubbo 接口依賴
<!--Dubbo 集成 SpringBoot 框架起步依賴--><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><!--dubbo接口依賴--> <dependency> <groupId>com.md.springboot</groupId> <artifactId>12-springboot-dubbo-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3、在Springboot 的核心配置文件
application.properties中配置 dubbo
#設置內嵌Tomcatserver.port=8080#設置上下文根server.servlet.context-path=/#配置 dubbo 的服務提供者信息#服務提供者應用名稱(必須寫,且不能重復)spring.application.name=springboot-dubbo-consumer#設置注冊中心spring.dubbo.registry=zookeeper://localhost:2181
4、編寫 Controller 類,調用遠程的 Dubbo 服務
在com.md.springboot.web下面
package com.md.springboot.web;import com.alibaba.dubbo.config.annotation.Reference;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import service.StudentService;/** * @author MD * @create 2020-08-22 7:15 */@Controllerpublic class StudentController { // 調用暴露的接口 @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false) private StudentService studentService; @GetMapping(value = "/student/count") @ResponseBody public Object studentCount(){ Integer count = studentService.queryAllStudentCount(); return "學生的總人數(shù):"+count; }}
注意使用Reference注解的時候:使用alibaba的這個
5、SpringBoot 入口 程序啟動類上加開啟 Dubbo 配置支持注解
package com.md.springboot;import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubboConfiguration // 開啟Dubbo配置public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
接口工程:存放實體bean和業(yè)務接口
服務提供者:
服務消費者:
16-springboot-ssm-dubbo-provider
在16-springboot-ssm-dubbo-provider項目中
1、添加插件,pom.xml中
<!--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>
2、將配置文件存放到項目根據(jù)目錄
GeneratorMapper.xml 內容如下
注意:生成model類時,指定位置,生成到接口項目中
<?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 驅動包所在位置,指定到你本機的完整路徑 --> <classPathEntry location="E:\Java\tool\maven_repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar"/> <!-- 配置 table 表信息內容體,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://localhost:3306/springboot" userId="root" password="123456"> </jdbcConnection><!--此時model生成在接口中,targetProject指定位置--> <!-- 生成 model 類,targetPackage 指定 model 類的包名, targetProject 指定 生成的 model 放在 IDEA 的哪個工程下面--> <javaModelGenerator targetPackage="com.md.springboot.model" targetProject="E:\Java\code\springboot\15-springboot-ssm-dubbo-interface\src\main\java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="false"/> </javaModelGenerator> <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的 包名, targetProject 指定生成的 mapper.xml 放在 IDEA 的哪個工程下面 --> <sqlMapGenerator targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成 MyBatis 的 Mapper 接口類文件,targetPackage 指定 Mapper 接口類的包 名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪個工程下面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 數(shù)據(jù)庫表名及對應的 Java 模型類名,有幾個表寫幾個table --> <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context></generatorConfiguration>
數(shù)據(jù)表名:t_student
4. 雙擊生成
生成的如下:
5、實體 bean 必須實現(xiàn)序列化
public class Student implements Serializable {
在pom.xml中
注意:
先生成完逆向工程,在寫接口依賴
<!--Dubbo 集成 SpringBoot 框架起步依賴--> <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.1</version> </dependency> <!--MySQL 數(shù)據(jù)庫驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <!--生成完逆向工程再寫這個--> <!--接口工程--> <dependency> <groupId>com.md.springboot</groupId> <artifactId>15-springboot-ssm-dubbo-interface</artifactId> <version>1.0.0</version> </dependency>
在 pom 文件中的 build 標簽中添加
<!--手動指定資源配置文件路徑--> <!--目的:將數(shù)據(jù)持久層映射文件編譯到 classpath 中--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
application.properties
#配置內嵌 Tomcat 端口號server.port=8081#設置上下文根server.servlet.context-path=/provider#配置數(shù)據(jù)源spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/springbootspring.datasource.username=rootspring.datasource.password=123456#配置 dubbo 服務提供者spring.application.name=springboot-ssm-dubbo-provider#表示是服務提供者spring.dubbo.server=true#注冊中心地址spring.dubbo.registry=zookeeper://localhost:2181
package com.md.springboot;import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubboConfiguration@MapperScan(basePackages = "com.md.springboot.mapper") // 掃描數(shù)據(jù)持久層映射文件public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
17-springboot-ssm-dubbo-consumer
完整的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.md.springboot</groupId> <artifactId>17-springboot-ssm-dubbo-consumer</artifactId> <version>1.0.0</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbbo 集成 SpringBoot 框架起步依賴--> <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.md.springboot</groupId> <artifactId>15-springboot-ssm-dubbo-interface</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
application.properties
#配置內嵌 Tomcat 端口號server.port=8080#配置項目上下文根server.servlet.context-path=/consumer#配置 zookeeper 注冊中心spring.application.name=springboot-ssm-dubbo-consumerspring.dubbo.registry=zookeeper://localhost:2181
@SpringBootApplication@EnableDubboConfiguration // 開啟dubbo支持配置public class Application {
在15-springboot-ssm-dubbo-interface中
package com.md.springboot.service;import com.md.springboot.model.Student;/** * @author MD * @create 2020-08-22 9:35 */public interface StudentService { /** * 通過id查詢學生信息 * @param id * @return */ Student queryStudentById(Integer id);}
在16-springboot-ssm-dubbo-provider中
package com.md.springboot.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.md.springboot.mapper.StudentMapper;import com.md.springboot.model.Student;import com.md.springboot.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * @author MD * @create 2020-08-22 9:37 */@Component// 暴露接口@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)public class StudentServiceImpl implements StudentService { // 逆向工程自動生成的StudentMapper @Autowired private StudentMapper studentMapper; @Override public Student queryStudentById(Integer id) { return studentMapper.selectByPrimaryKey(id); }}
在17-springboot-ssm-dubbo-consumer中
package com.md.springboot.web;import com.alibaba.dubbo.config.annotation.Reference;import com.md.springboot.model.Student;import com.md.springboot.service.StudentService;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author MD * @create 2020-08-22 9:43 */@RestControllerpublic class StudentController {// 使用暴露的接口 @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false) private StudentService studentService; @RequestMapping(value = "/student") public Object queryStudent(Integer id){ Student student = studentService.queryStudentById(id); return student; } @RequestMapping(value = "/student/detail/{id}") public Object queryStudent1(@PathVariable("id") Integer id){ Student student = studentService.queryStudentById(id); return "RESTful:"+student; }}
聯(lián)系客服