阿里巴巴提供了dubbo集成springBoot开源项目,可以到GitHub上GitHub - alibaba/dubbo-spring-boot-starter: Dubbo Spring Boot Starter查看入门教程
创建新的数据库springboot,指定数据库字符编码为utf-8
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_student -- ---------------------------- DROP TABLE IF EXISTS `t_student`; CREATE TABLE `t_student` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `age` int NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of t_student -- ---------------------------- INSERT INTO `t_student` VALUES (1, 'zhangsan', 25); INSERT INTO `t_student` VALUES (2, 'lisi', 28); INSERT INTO `t_student` VALUES (3, 'wangwu', 23); INSERT INTO `t_student` VALUES (4, 'Tom', 21); INSERT INTO `t_student` VALUES (5, 'Jck', 55); INSERT INTO `t_student` VALUES (6, 'Lucy', 27); INSERT INTO `t_student` VALUES (7, 'zhaoliu', 75); SET FOREIGN_KEY_CHECKS = 1;
按照Dubbo官方开发建议,创建一个接口项目,该项目只定义接口和model类
项目名称:006-springboot-dubbo-exterface



- public interface UserService {
- String say(String name);
- }
项目名称:007-springboot-dubbo-provider


- <!--Spring Boot集成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>
- <!--Dubbo接口工程依赖-->
- <dependency>
- <groupId>com.suke.springboot</groupId>
- <artifactId>006-springboot-dubbo-exterface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
注意:Dubbo的注解都是自定义的注解,由我们添加的Dubbo依赖中的类进行处理
编写dubbo配置是没有提示的
- #配置内嵌Tomcat端口号
- server.port=9007
- #配置项目上下文根
- server.servlet.context-path=/007-springboot-dubbo-provider
- #配置Dubbo服务提供者配置
- #服务提供者应用名称必须写,且不能重复
- spring.application.name=007-springboot-dubbo-provider
- #表示是服务提供者,可以省略
- spring.dubbo.server=true
- #注册中心地址
- spring.dubbo.registry=zookeeper://192.168.60.130:2181
- @Service是由阿里提供的注解,不是spring的注册
- @Service相当于 <dubbo:service interface="" ref="" version="" timeout=""/>
- 如果提供者指定version,那么消费者也得指定version
- timeout = 20000: 20秒内没连上自动放弃结束
- retries = 3: 额外三次连接机会
- version = "1.0.0": 对以后服务版本的升级进行区分
- /*
- * @Service是由阿里提供的注解,不是spring的注册
- * @Service相当于 <dubbo:service interface="" ref="" version="" timeout=""/>
- * 如果提供者指定version,那么消费者也得指定version
- * timeout = 20000: 20秒内没连上自动放弃结束
- * retries = 3: 额外三次连接机会
- * version = "1.0.0": 对以后服务版本的升级进行区分
- * */
- @Service(interfaceClass = UserService.class, timeout = 20000, retries = 3, version = "1.0.0")
- @Component //将接口实现类交给spring容器进行管理
- public class UserServiceImpl implements UserService {
- @Override
- public String say(String name) {
- return "Hello SpringBoot!";
- }
- }
- @SpringBootApplication
- @EnableDubboConfiguration//开启Dubbo配置支持
- public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
-
- }
(1)启动Linux服务器上的Zookeeper
Zookeeper启动
https://blog.csdn.net/qq_45037155/article/details/124959703
./zkServer.sh start
[root@Suke ~]# cd /usr/local/apache-zookeeper-3.5.5-bin/bin/ [root@Suke bin]# ./zkServer.sh help ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|restart|status|print-cmd} [root@Suke bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.5.5-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
(2)启动服务提供者项目主程序

项目名称:008-springboot-dubbo-consumer


- <!--Spring Boot集成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>
- <!--Dubbo接口工程依赖-->
- <dependency>
- <groupId>com.suke.springboot</groupId>
- <artifactId>006-springboot-dubbo-exterface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- server.port=9008
- server.servlet.context-path=/008-springboot-dubbo-consumer
- #配置dubbo服务提供者配置
- spring.application.name=008-springboot-dubbo-consumer
- #配置注册中心地址
- spring.dubbo.registry=zookeeper://192.168.60.130:2181
- @RestController
- public class UserController {
-
- @Reference(interfaceClass = UserService.class, version = "1.0.0", timeout = 20000, retries = 3, check = false)
- UserService userService;
-
- @GetMapping(value = "/springBoot/hello")
- public Object hello(HttpServletRequest request) {
-
- String sayHello = userService.say("SpringBoot");
-
- return sayHello;
- }
- }
- @SpringBootApplication
- @EnableDubboConfiguration//开启Dubbo配置支持
- public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
-
- }
(1)启动服务消费者项目主程序

(2)浏览器访问测试


- <!--ZooKeeper注册中心依赖-->
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.10</version>
- </dependency>
- <!--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>
