题记
接着feign的热乎劲,就顺便把dubbo一起复习一下吧
上代码
准备提供接口类的服务
- 整好服务
- 写好接口类
public interface StudentDubboApi {
DubboUserInfo getInfo();
}
teacher作为消费者调用dubbo
- pom引入dubbo-api的接口类
<!--引入dubbo的统一api-->
```xml
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
- application.yml增加dubbo的配置
dubbo:
application:
name: teacher-dubbo
registry:
address: nacos://${spring.cloud.nacos.discovery.server-addr}
protocol:
name: dubbo # 指定通信协议
port: 20880 # 通信端口 这里指的是与消费者间的通信协议与端口
provider:
timeout: 10000 # 配置全局调用服务超时时间
retries: 3 # 重试3次
delay: -1
- 启动类添加 @EnableDubbo
- 编写消费者,新版dubbo的reference已经过时,采用DubboReference
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class TeacherDubboService {
@DubboReference(check = false)
private StudentDubboApi studentDubboApi;
@GetMapping("getDubbo")
public DubboUserInfo testGetInfo(){
return studentDubboApi.getInfo();
}
}
student作为生产者
- 快速引入相同pom
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<!--引入dubbo的统一api-->
<dependency>
<groupId>ccom.example.com</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
- 启动类添加@EnableDubbo
- 新建生产者的类
import com.example.demo.handdle.StudentDubboApi;
import com.example.demo.model.DubboUserInfo;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class StudentProduct implements StudentDubboApi {
@Override
public DubboUserInfo getInfo() {
DubboUserInfo userInfo = new DubboUserInfo();
userInfo.setAge(250);
userInfo.setLike("I LIKE JAVA");
userInfo.setName("my name is bongbong");
return userInfo;
}
}
- application.yml 添加配置
server:
port: 8081
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
cluster-name: sh
namespace: 20c0e599-919c-483a-a3d7-c73eaf3b4e7c
ephemeral: false
config:
file-extension: yaml
server-addr: 127.0.0.1:8848
namespace: 20c0e599-919c-483a-a3d7-c73eaf3b4e7c
shared-configs:
- dataId: common.yaml
application:
name: sutdent
profiles:
active: test
dubbo:
application:
name: student-dubbo
registry:
address: nacos://${spring.cloud.nacos.discovery.server-addr}
protocol:
name: dubbo # 指定通信协议
port: 20881 # 通信端口 这里指的是与消费者间的通信协议与端口
provider:
timeout: 10000 # 配置全局调用服务超时时间
retries: 3 # 重试3次
delay: -1
访问teacher的接口http://127.0.0.1:8082/getDubbo
为了方便大家不晕,我再截个图
版本对应表
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
|---|---|---|---|---|---|
| 2021.0.1.0* | 1.8.3 | 1.4.2 | 4.9.2 | 2.7.15 | 1.4.2 |
| 2.2.7.RELEASE | 1.8.1 | 2.0.3 | 4.6.1 | 2.7.13 | 1.3.0 |
| 2.2.6.RELEASE | 1.8.1 | 1.4.2 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 |
| 2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 |
| 2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 |
| 2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 |
| 2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 |
技术债务
1.bootstrap.yml突然失效了,肯定是依赖问题,后续找到了我把解决贴过来
2.配置全局不检查依赖,否则服务之间启动的先后顺序会影响是否成功
写作不易,看到这里还请点个赞~