目錄
1.說明
2.示例
3.總結
1.說明
dubbo官網:https://cn.dubbo.apache.org/zh-cn/
Apache Dubbo 是一款 RPC 服務開發框架,用于解決微服務架構下的服務治理與通信問題,支持多種語言,官方提供了 Java、Golang 等多語言 SDK 實現。使用 Dubbo 開發的微服務原生具備相互之間的遠程地址發現與通信能力, 利用 Dubbo 提供的豐富服務治理特性,可以實現諸如服務發現、負載均衡、流量調度等服務治理訴求。Dubbo 被設計為高度可擴展,用戶可以方便的實現流量攔截、選址的各種定制邏輯。
2.示例
實現說明:
????????創建一個空項目,在空項目中創建3個模塊,分別定義接口工程,生產者工程及消費者工程。并在生產者工程及消費者工程中引入接口工程。
????????接口工程存放表的實體類及服務接口。
????????生產者工程提供服務接口的實現。
????????消費者工程調用服務接口。
實現步驟:
①引入dubbo依賴
<!-- Dubbo Spring Boot Starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><!--zookerper版本一定要匹配! --><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-zookeeper</artifactId><version>2.7.8</version></dependency>
?②在接口工程中創建接口
package com.example.service;public interface PrivoderService {String getInfo();
}
③在生產者工程中實現接口,并進行dubbo的配置
接口實現:使用dbboservice注解,將服務的實現暴露給dubbo
package com.example.provider.service.impl;import com.example.service.PrivoderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;/*** @Author linaibo* @Date 2023/11/18 15:28* @Version 1.0*/
@Service
@DubboService
public class PrividerServiceImpl implements PrivoderService {@Overridepublic String getInfo() {return "執行成功";}
}
?配置文件:
server:port: 8881
dubbo:application:name: provider-service //dubbo的應用名registry:protocol: zookeeper //使用zookeeper作為服務的注冊中心address: 127.0.0.1:2181 //zookeeper地址protocol:name: dubbo //使用dubbo協議port: 20885consumer:timeout: 60000 //調用接口的超時時間check: false //啟動時不校驗消費者是否已啟動
spring:datasource:url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: 123456
mybatis:mapper-locations: classpath*:mapper/*Mapper.xmltype-aliases-package: com.**.domain
啟動類配置:添加@EnableDubbo,用于將dubbo相關的配置bean加載到spring容器
package com.example.provider;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @Author linaibo* @Date 2023/11/18 15:32* @Version 1.0*/
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}
?④生產者工程中調用接口
調用:使用DubboReference指定調用的服務
package com.example.consumer.service.impl;import com.example.consumer.service.ConsumerService;
import com.example.domain.AjaxResult;
import com.example.service.ISysConfigService;
import com.example.service.PrivoderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;import static com.example.domain.AjaxResult.success;/*** @Author linaibo* @Date 2023/11/18 15:56* @Version 1.0*/
@Service
public class ConsumerServiceImpl implements ConsumerService {@DubboReferenceprivate PrivoderService privoderService;@DubboReferenceprivate ISysConfigService sysConfigService;@Overridepublic String getInfo() {String info = privoderService.getInfo();return info;}@Overridepublic AjaxResult getConfig(Long configId) {return success(sysConfigService.selectConfigById(configId));}
}
配置文件及啟動類配置和生產者工程一致
啟動zookeeper服務及生產者工程及消費者工程,就可以進行服務的調用。
3.總結
可以通過dubbo-admin進行服務的管理及查看。
dubbo.consumer.timeout:調用超時時間(毫秒),默認為 1000。debug模式下會導致調用失敗,所以需要調大。
dubbo.consumer.check:為true時,開啟服務啟動時檢查依賴的服務是否可用,默認為 true。
也就是說,生產者沒有啟動時,消費者無法啟動,需要設置為false
參照:SpringBoot整合dubbo+zooker搭建分布式服務(超詳細)_springboot+dubbo分布式項目-CSDN博客
SpringBoot項目集成Dubbo_springboot集成dubbo-CSDN博客