第七章 SpringBoot集成Dubbo
官网下载地址
能源节点springboot材料
视频观看地址
https://www.bilibili.com/vide...
7.1 看 SpringBoot继承Dubbo的文档
https://github.com/apache/dub...
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我的项目
1) pom.xml
<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 { @Override public 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:2181
4)在启动类的下面
@SpringBootApplication@EnableDubbopublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}
7.4消费者
创立SpringBoot我的项目
1) pom.xml
<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:2181
7.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;
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目录中