第七章SpringBoot集成Dubbo官方下载地址PowerNodespringboot资料视频查看地址https://www.bilibili.com/video...7.1看SpringBoot继承Dubbo的文档https://github.com/apache/dub...7.2公共项目独立maven项目:定义接口和数据类publicclassStudentimplementsSerializable{privatestaticfinallongserialVersionUID=1901229007746699151L;私有整数id;私有字符串名称;privateIntegerage;}publicinterfaceStudentService{StudentqueryStudent(Integerid);}7.3Provider创建SpringBoot项目1)pom.xmlcom.bjpowernode022-interface-api1.0.0org.apache.dubbodubbo-spring-boot-starter2.7.8org.apache.dubbodubbo-dependencies-zookeeper2.7.8pomslf4j-log4j12org.slf4j2)实现接口/***使用dubbo中的注解暴露服务*@Component不能添加*/@DubboService(interfaceClass=StudentService.class,version="1.0",超时=5000)publicclassStudentServiceImplimplementsStudentService{@OverridepublicStudentqueryStudent(Integerid){Studentstudent=newStudent();如果(1001==id){student.setId(1001);student.setName("------1001-张三&q不;);学生.setAge(20);}elseif(1002==id){student.setId(1002);student.setName("#######1002-李四");student.setAge(22);}返校生;}}3)application.properties#configure服务名dubbo:applicationname="name"spring.application.name=studentservice-provider#configure扫描包,扫描@DubboServicedubbo.scan.base-packages=com.bjpowernode.service#Configuredubbo协议#dubbo.protocol.name=dubbo#dubbo.protocol.port=20881#注册中心dubbo.registry.address=zookeeper://localhost:21814)上面的启动类@SpringBootApplication@EnableDubbopublicclassProviderApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ProviderApplication.class,args);}}7.4消费者创建SpringBoot项目1)pom.xmlcom.bjpowernode022-interface-api1.0.0org.apache.dubbodubbo-spring-boot-starter2.7.8org.apache.dubbodubbo-dependencies-zookeeper2.7.8pomslf4j-log4j12org.slf4jexclusion>2)可以创建一个Controller或者Service@RestControllerpublicclassDubboController{/***引用远程服务,将创建的代理对象注入studentService*///@DubboReference(接口Class=StudentService.class,version="1.0")/***没有使用interfaceClass,默认是引用类型的数据类型*/@DubboReference(version="1.0")privateStudentServicestudentService;@GetMapping("/query")publicStringqueryStudent(Integerid){Studentstudent=studentService.queryStudent(id);return"调用远程接口并获取对象:"+student;}}3)application.properties#指定服务名spring.application.name=consumer-application#指定注册中心dubbo.registry.address=zookeeper://localhost:21817.5实践中用到的技术:SpringBoot、Dubbo、Redis、MyBatisStudent表:CREATETABLEstudent(idint(11)NOTNULLAUTO_INCREMENT,namevarchar(255)COLLATEutf8_binDEFAULTNULL,phonevarchar(11)COLLATEutf8_binDEFAULTNULL,ageint(11)DEFAULTNULL,PRIMARYKEY(id))ENGINE=InnoDBAUTO_INCREMENT=6默认字符集=utf8COLLATE=utf8_bin;1)注册学生手机必须Only,如果手机号已经存在,则注册失败intaddStudent(学生学生);返回值:int1:注册成功2:手机号已存在,姓名至少两个字符,年龄必须大于02)查询学生,根据id查询,该学生。先去redis查询student。如果redis中没有这个学生,就从数据库中查询,把查询到的学生放到redis中,然后再查询这个学生。学生应该从redis中获得。学生queryStudent(Integerid);3)使用Dubbo框架,addStudent和queryStudent由服务提供者实现。消费者可以是调用提供者的两个方法的控制器。实现注册和查询。4)页面使用html和ajax、jquery。在html页面提供学生注册的表单,提供文本框输入id进行查询。注册和查询都使用了ajax技术。html、jquery.js放在resources/static目录下