1.1 公共项目
创建Maven项目:ch14-interface-api
1.1.1 项目坐标
<groupId>com.suyv</groupId>
<artifactId>ch14-interface-api</artifactId>
<version>1.0.0</version>
1.1.2 创建 Student 实体类
package com.suyv.model;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1318357199475675242L;
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;
}
}
1.1.3 创建服务接口
package com.suyv.service;
import com.suyv.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
1.2 服务提供者
创建SpringBoot项目:ch15-service-provider
1.2.1 Maven依赖
<dependencies>
<!--加入公共依赖-->
<dependency>
<groupId>com.suyv</groupId>
<artifactId>ch14-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>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2.2 编写 application.properties 文件
#服务名称
spring.application.name=studentservice-provider
#zookeeper 注册中心
dubbo.registry.address=zookeeper://localhost:2181
#dubbo 注解所在的包名
dubbo.scan.base-packages=com.suyv.service
# 配置Dubbo协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
1.2.3 创建接口的实体类
package com.suyv.service.impl;
import com.suyv.model.Student;
import com.suyv.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
/**
* 使用Dubbo中的注解暴露服务
*/
@Component
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 500)
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(25);
}
return student;
}
}
1.2.4 修改主启动类 Application
package com.suyv;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启用Dubbo
*/
@SpringBootApplication
@EnableDubbo
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.3 创建消费者
创建SpringBoot项目:ch16-service-consumer
1.3.1 Maven依赖
<dependencies>
<!--加入公共依赖-->
<dependency>
<groupId>com.suyv</groupId>
<artifactId>ch14-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>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.3.2 编写 application.properties 文件
#服务名称
spring.application.name=studentservice-consumer
#zookeeper注册中心
dubbo.registry.address=zookeeper://localhost:2181
1.3.3 创建 Controller
package cm.suyv.controller;
import com.suyv.model.Student;
import com.suyv.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 DubboController {
@DubboReference(interfaceClass = StudentService.class,version = "1.0")
private StudentService studentService;
@GetMapping("/query")
public String queryStudent(){
Student student = studentService.queryStudent(1001);
return "调用远程接口:获取对象:" + student;
}
}
1.3.4 修改主启动类 Application
package cm.suyv;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}