公共接口项目
-
独立的maven项目:定义了接口和数据类
-
数据类
kage com.example.dubbo.model;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = -2375196479092045782L;
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
}
}
- 接口
package com.example.dubbo.service;
import com.example.dubbo.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
提供者
- springboot项目的pom.xml
<!--加入公共接口项目的gav-->
<dependency>
<groupId>com.example.dubbo</groupId>
<artifactId>oo14-springboot-dubbo-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>
- 实现接口
package com.example.service.impl;
import com.example.dubbo.model.Student;
import com.example.dubbo.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* 使用dubbo中的注解,对外暴露服务接口
*/
@DubboService(interfaceClass = StudentService.class, version = "1.0.0", timeout = 5000)
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(Integer id) {
Student student = new Student();
student.setId(id);
student.setName("橘子");
student.setAge(21);
return student;
}
}
- 在springboot的主启动类上添加注解@EnableDubbo,支持dubbo
package com.example;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
- 在application.properties中配置服务提供者
#配置springboot中的dubbo服务者
#配置提供的服务名称
dubbo.application.name=student-service-provider
#配置需要扫描的包
dubbo.scan.base-packages=com.example.service
#配置注册中心
dubbo.registry.address=zookeeper://localhost:2181
消费者
-
springboot项目的pom.xml, 和服务提供者的依赖相同
-
创建controller, 调用服务者提供的服务
package com.example.controller;
import com.example.dubbo.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@DubboReference(interfaceClass = StudentService.class, version = "1.0.0")
private StudentService studentService;
@GetMapping("/student/query")
public String queryStudent(Integer id){
return studentService.queryStudent(id).toString();
}
}
- 同样在springboot的主启动类上添加注解@EnableDubbo,支持dubbo
- 在application.properties中配置服务消费者
#springboot中配置dubbo消费者
#消费者名称
dubbo.application.name=student-service-consumer
#配置注册中心
dubbo.registry.address=zookeeper://localhost:2181
测试
- 先启动zookeeper注册中心,再启动服务的提供者和消费者