第七章 SpringBoot集成Dubbo
- 视频观看地址
7.1 看 SpringBoot继承Dubbo的文档https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md
7.2 公共项目独立的maven项目: 定义了接口和数据类
public class Student implements Serializable {private static final long serialVersionUID = 1901229007746699151L;private Integer id;private String name;private Integer age;}public interface StudentService {Student queryStudent(Integer id);}7.3 提供者
- 创建SpringBoot项目
<dependencies><!--加入公共项目的gav--><dependency><groupId>com.bjpowernode</groupId><artifactId>022-interface-api</artifactId><version>1.0.0</version></dependency><!--dubbo依赖--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><!--zookeeper依赖--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><version>2.7.8</version><type>pom</type><exclusions><!-- 排除log4j依赖 --><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency></dependencies>2)实现接口
/** * 使用dubbo中的注解暴露服务 * @Component 可以不用加 */@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)public class StudentServiceImpl implements StudentService {@Overridepublic Student queryStudent(Integer id) {Student student= new Student();if( 1001 == id){student.setId(1001);student.setName("------1001-张三");student.setAge(20);} else if(1002== id){student.setId(1002);student.setName("#######1002-李四");student.setAge(22);}return student;}}3)application.properties
#配置服务名称 dubbo:application name="名称"spring.application.name=studentservice-provider#配置扫描的包 , 扫描的@DubboServicedubbo.scan.base-packages=com.bjpowernode.service#配置dubbo协议#dubbo.protocol.name=dubbo#dubbo.protocol.port=20881#注册中心dubbo.registry.address=zookeeper://localhost:21814)在启动类的上面@SpringBootApplication@EnableDubbopublic class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}7.4消费者
- 创建SpringBoot项目
<dependencies><!--加入公共项目的gav--><dependency><groupId>com.bjpowernode</groupId><artifactId>022-interface-api</artifactId><version>1.0.0</version></dependency><!--dubbo依赖--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><!--zookeeper依赖--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><version>2.7.8</version><type>pom</type><exclusions><!-- 排除log4j依赖 --><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency></dependencies>2)创建了Controller 或者 Service都可以
@RestControllerpublic class DubboController {/*** 引用远程服务 , 把创建好的代理对象 , 注入给studentService*///@DubboReference(interfaceClass = StudentService.class,version = "1.0")/*** 没有使用interfaceClass , 默认的就是 引用类型的 数据类型*/@DubboReference(version = "1.0")private StudentService studentService;@GetMapping("/query")public String queryStudent(Integer id){Student student= studentService.queryStudent(id);return "调用远程接口 , 获取对象:"+student;}}3)application.properties
#指定服务名称spring.application.name=consumer-application#指定注册中心dubbo.registry.address=zookeeper://localhost:21817.5 练习使用的技术: SpringBoot ,Dubbo, Redis, MyBatis- Student表:

文章插图
?
CREATE TABLE student (id int(11) NOT NULL AUTO_INCREMENT,name varchar(255) COLLATE utf8_bin DEFAULT NULL,phone varchar(11) COLLATE utf8_bin DEFAULT NULL,age int(11) DEFAULT NULL,PRIMARY KEY (id)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;【动力节点和黑马怎么样 七 动力节点-王妈妈Springboot教程SpringBoot集成Dubbo】1)注册学生? phone必须唯一 , 如果已经存在了手机号 , 注册失败的 。
? int addStudent(Student student);
? 返回值:int
? 1: 注册成功
? 2 : 手机号已经存在
? name至少两个字符 ,
? age 必须 大于 0
2) 查询学生 , 根据id查询 , 此学生 。
? 先到redis查询学生 , 如果redis没有此学生 , 从数据库查询 , 把查询到的学生放入到redis 。
? 后面再次查询这个学生应该从redis就能获取到 。
? Student queryStudent(Integer id);
3)使用Dubbo框架 , addStudent, queryStudent 是有服务提供者实现的 。
? 消费者可以是一个Controller , 调用提供者的两个方法 。实现注册和查询 。
4)页面使用html和ajax , jquery 。
? 在html页面中提供 form 注册学生 , 提供文本框输入id , 进行查询 。
? 注册和查询都使用ajax技术 。
? html , jquery.js都放到resources/static目录中
?
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
