- 1.接口工程
- 2.提供者
- 3. 消费者
- 4. 测试
1.pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.yl boot-interface 0.0.1-SNAPSHOT boot-interface Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com.yl boot-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2.实体类
package com.yl.bootinterface.model;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private Double score;
private Integer sex;
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 Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
3.接口
package com.yl.bootinterface.service;
import com.yl.bootinterface.model.Student;
public interface StudentService {
Student getStudentById(Integer id);
}
2.提供者
1.pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.tl boot-provider 0.0.1-SNAPSHOT boot-provider Demo project for Spring Boot 1.8 com.alibaba.boot dubbo-spring-boot-starter 0.2.0 com.yl boot-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2.application.properties
server.port=8080 # 提供者名称,唯一 dubbo.application.name=boot-provider # 配置注册中心 dubbo.registry.address=127.0.0.1:2181 dubbo.registry.protocol=zookeeper # 配置协议以及端口 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 # 配置超时时间,默认为1000ms dubbo.registry.timeout=5000 # 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接 dubbo.provider.retries=3 # 配置监控中心要监控的地址,监控注册中心的地址 dubbo.monitor.address=registry
3.接口实现类
package com.tl.bootprovider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.yl.bootinterface.model.Student;
import com.yl.bootinterface.service.StudentService;
//暴露服务,注意是com.alibaba这个包下的
@Service
public class StudentServiceImpl implements StudentService {
@Override
public Student getStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setName("tom");
student.setScore(99.0);
student.setSex(0);
return student;
}
}
4.主程序
package com.tl.bootprovider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //开启基于dubbo的注解功能
@SpringBootApplication
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}
3. 消费者
1.pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.yl boot-consumer 0.0.1-SNAPSHOT boot-consumer Demo project for Spring Boot 1.8 com.alibaba.boot dubbo-spring-boot-starter 0.2.0 org.springframework.boot spring-boot-starter-web com.yl boot-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2.application.properties
server.port=8081 # 消费者名称,唯一 dubbo.application.name=boot-consumer # 配置注册中心 dubbo.registry.address=zookeeper://127.0.0.1:2181 # 配置超时时间,默认为1000ms dubbo.registry.timeout=5000 # 配置如果连接断开时,要重连次数,不包含第一次连接,0代表不重尝试连接 dubbo.consumer.retries=3 # 配置监控中心要监控的地址 dubbo.monitor.address=registry
3.controller
package com.yl.bootconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.yl.bootinterface.model.Student;
import com.yl.bootinterface.service.StudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Reference //引用远程服务
StudentService studentService;
@GetMapping("/getStudentById")
public Student getStudentById(Integer id) {
return studentService.getStudentById(id);
}
}
4.主程序
package com.yl.bootconsumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo //开启基于dubbo的注解功能
public class BootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootConsumerApplication.class, args);
}
}
4. 测试
1.启动本地的zookeeper
2.启动提供者和服务器
3.访问测试接口



