SpringBoot整合Dubbo

x33g5p2x  于2021-11-27 转载在 Spring  
字(3.5k)|赞(0)|评价(0)|浏览(491)

一、接口工程

1.1、创建maven Java工程

1.2、添加实体类和服务接口

  • 实体类
  1. @Data
  2. public class Student implements Serializable {
  3. private Integer id;
  4. private String name;
  5. private String sex;
  6. private Integer age;
  7. }
  • 服务接口
  1. public interface StudentService {
  2. //通过id查询学生
  3. Student queryStudent(Integer id);
  4. }

二、服务提供者

2.1、创建springboot工程

2.2、添加相关依赖

添加dubbo、注册中心、接口工程依赖

  1. <!--接口工程-->
  2. <dependency>
  3. <groupId>com.why</groupId>
  4. <artifactId>ch04-springboot-dubbo-interface</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </dependency>
  7. <!--Dubbo依赖-->
  8. <dependency>
  9. <groupId>com.alibaba.spring.boot</groupId>
  10. <artifactId>dubbo-spring-boot-starter</artifactId>
  11. <version>2.0.0</version>
  12. </dependency>
  13. <!--注册中心依赖-->
  14. <dependency>
  15. <groupId>com.101tec</groupId>
  16. <artifactId>zkclient</artifactId>
  17. <version>0.10</version>
  18. </dependency>

2.3、配置springboot核心配置文件

在application.properties文件中编写

  1. #内嵌tomcat配置
  2. server.port=8081
  3. server.servlet.context-path=/
  4. #dubbo配置
  5. #服务提供者名称
  6. spring.application.name=ch04-dubbo-provide
  7. #声明当前工程是服务提供者
  8. spring.dubbo.server=true
  9. #注册中心 默认端口号2181
  10. spring.dubbo.registry=zookeeper://192.168.140.129:2181

2.4、编写接口实现类

  1. //把接口的实现类(服务)交给springboot容器管理
  2. @Component
  3. //@Service选择alibaba提供的那个 interfaceName接口的全限定名 也可以使用interfaceClass
  4. //@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
  5. //暴露接口服务
  6. @Service(interfaceName = "com.why.service.StudentService",version = "1.0.0",timeout = 15000)
  7. public class StudentServiceImpl implements StudentService {
  8. @Override
  9. public Student queryStudent(Integer id) {
  10. //这里只是通过创建对象来模拟dao层访问数据库返回的结果
  11. //偷个小懒
  12. Student student = new Student();
  13. student.setName("张三");
  14. student.setSex("男");
  15. student.setAge(21);
  16. return student;
  17. }
  18. }

2.5、开启dubbo配置

在引导类上添加@EnableDubboConfiguration注解

  1. @SpringBootApplication
  2. @EnableDubboConfiguration//开启dubbo配置
  3. public class Ch04SpringbootDubboProvideApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Ch04SpringbootDubboProvideApplication.class, args);
  6. }
  7. }

三、服务消费者

3.1、创建spring boot工程

3.2、添加相关依赖

添加dubbo、注册中心、接口工程依赖

  1. <!--接口工程-->
  2. <dependency>
  3. <groupId>com.why</groupId>
  4. <artifactId>ch04-springboot-dubbo-interface</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </dependency>
  7. <!--Dubbo依赖-->
  8. <dependency>
  9. <groupId>com.alibaba.spring.boot</groupId>
  10. <artifactId>dubbo-spring-boot-starter</artifactId>
  11. <version>2.0.0</version>
  12. </dependency>
  13. <!--注册中心依赖-->
  14. <dependency>
  15. <groupId>com.101tec</groupId>
  16. <artifactId>zkclient</artifactId>
  17. <version>0.10</version>
  18. </dependency>

3.3、配置springboot核心配置文件

  1. #内嵌tomcat端口号
  2. server.port=8080
  3. #上下文根
  4. server.servlet.context-path=/
  5. #duoob配置
  6. #服务消费者名称
  7. spring.application.name=ch04-springboot-dubbo-consumer
  8. #注册中心
  9. spring.dubbo.registry=zookeeper://192.168.140.129:2181

3.4、编写Controller

  1. @Controller
  2. public class MyController {
  3. //引用服务提供者提供的服务
  4. @Reference(interfaceName = "com.why.service.StudentService",version = "1.0.0",check = false)
  5. StudentService studentService;
  6. @RequestMapping("/query")
  7. public @ResponseBody Object query(){
  8. Student student = studentService.queryStudent(1);
  9. return student.toString();
  10. }
  11. }

3.5、开启dubbo配置

在引导类上添加@EnableDubboConfiguration注解

  1. @SpringBootApplication
  2. @EnableDubboConfiguration//开启dubbo配置
  3. public class Ch04SpringbootDubboConsumerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Ch04SpringbootDubboConsumerApplication.class, args);
  6. }
  7. }

测试

启动zookeeper注册中心

关闭防火墙

运行结果

相关文章