1.开发Dubbo服务接口
1.1 创建普通maven工程
014-springboot-dubbo-interface
1.2 创建StudentService接口
package com.zzy.springboot.service;
public interface StudentService {
//获取学生总人数
Integer queryStudentCount();
}
2. 开发Dubbo服务提供者
2.1 创建SpringBoot-WEB工程
015-springboot-dubbo-provider
2.2 引入Dubbo集成springboot的依赖
<!--Dubbo 集成 SpringBoot 框架起步依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.3 引入 zookeeper 的客户端依赖
由于使用 zookeeper 作为注册中心,需加入 zookeeper 的客户端依赖
<!--引入zookeeper客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
2.4 加入Dubbo接口依赖
<!--引入dubbo接口依赖-->
<dependency>
<groupId>com.zzy.springboot</groupId>
<artifactId>014-springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2.5 核心配置文件中配置dubbo
application.properties 格式-配置 dubbo
#设置内嵌tomcat端口号
server.port=8014
#设置上下文根
server.servlet.context-path=/
#设置#服务提供者应用名称(必须写,且不能重复)
spring.application.name=015-springboot-dubbo-provider
#设置当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://192.168.126.146:2181
2.6 编写dubbo接口的实现类
package com.zzy.springboot.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.zzy.springboot.StudentService;
import org.springframework.stereotype.Component;
@Component//交给spring容器管理
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 55000)
public class StudentServiceImpl implements StudentService {
@Override
public Integer quertStudentCounr() {
return 1111;
}
}
2.7主启动类开启 Dubbo 配置支持注解
2.8 启动zookeeper服务
3.开发Dubbo服务消费者
3.1 创建SpringBoot-WEB工程
016-springboot-dubbo-consumer
3.2 引入 Dubbo 集成 SpringBoot起步依赖
<!--Dubbo 集成 SpringBoot 框架起步依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
3.3 引入 zookeeper 的客户端依赖
<!--Zookeeper 客户端依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
3.4 引入 Dubbo 接口依赖
<!--引入dubbo接口依赖-->
<dependency>
<groupId>com.zzy.springboot</groupId>
<artifactId>014-springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3.5 核心配置文件中配置dubbo
#设置内嵌tomcat端口号
server.port=8016
#设置上下文根
server.servlet.context-path=/
#设置#服务提供者应用名称(必须写,且不能重复)
spring.application.name=016-springboot-dubbo-consumer
#设置注册中心
spring.dubbo.registry=zookeeper://192.168.126.146:2181
3.6 编写Controller类调用远程Dubbo服务
package com.zzy.springboot.web;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zzy.springboot.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Reference(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
private StudentService studentService;
@RequestMapping(value = "/student/count")
public Object studentCount(){
Integer count = studentService.queryStudentCount();
return "学生人数为"+count;
}
}
3.7 主启动类开启 Dubbo 配置支持注解
4. 运行访问
4.1 运行提供者
4.2 运行消费者
4.3 测试结果
localhost:8016/student/count