1.pom,xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>boot-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-interface</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.yl</groupId>
<artifactId>boot-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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);
}
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tl</groupId>
<artifactId>boot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.yl</groupId>
<artifactId>boot-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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.接口实现类(Version0.0.1)
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(version = "0.0.1")
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.接口实现类(Version0.0.2)
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;
@Service(version = "0.0.2")
public class UserServiceImpl implements StudentService {
@Override
public Student getStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setName("小兰");
student.setScore(100.0);
student.setSex(1);
return student;
}
}
5.主程序
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);
}
}
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>boot-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.yl</groupId>
<artifactId>boot-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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(version = "0.0.1") //引用远程服务
StudentService studentService;
@Reference(version = "0.0.2") //引用远程服务
StudentService userService;
@GetMapping("/getStudentById")
public Student getStudentById(Integer id) {
return studentService.getStudentById(id);
}
@GetMapping("/getUserById")
public Student getUserById(Integer id) {
return userService.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);
}
}
1.本地启动zookeeper
2.启动提供者和消费者
3.访问version为0.0.1的接口
4.访问version为0.0.2的接口
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_41359273/article/details/124642680
内容来源于网络,如有侵权,请联系作者删除!