網(wǎng)站權(quán)重高+做別的關(guān)鍵詞百度應(yīng)用下載安裝
目錄
1.說(shuō)明
2.示例
3.總結(jié)
1.說(shuō)明
dubbo官網(wǎng):https://cn.dubbo.apache.org/zh-cn/
Apache Dubbo 是一款 RPC 服務(wù)開發(fā)框架,用于解決微服務(wù)架構(gòu)下的服務(wù)治理與通信問(wèn)題,支持多種語(yǔ)言,官方提供了 Java、Golang 等多語(yǔ)言 SDK 實(shí)現(xiàn)。使用 Dubbo 開發(fā)的微服務(wù)原生具備相互之間的遠(yuǎn)程地址發(fā)現(xiàn)與通信能力, 利用 Dubbo 提供的豐富服務(wù)治理特性,可以實(shí)現(xiàn)諸如服務(wù)發(fā)現(xiàn)、負(fù)載均衡、流量調(diào)度等服務(wù)治理訴求。Dubbo 被設(shè)計(jì)為高度可擴(kuò)展,用戶可以方便的實(shí)現(xiàn)流量攔截、選址的各種定制邏輯。
2.示例
實(shí)現(xiàn)說(shuō)明:
????????創(chuàng)建一個(gè)空項(xiàng)目,在空項(xiàng)目中創(chuàng)建3個(gè)模塊,分別定義接口工程,生產(chǎn)者工程及消費(fèi)者工程。并在生產(chǎn)者工程及消費(fèi)者工程中引入接口工程。
????????接口工程存放表的實(shí)體類及服務(wù)接口。
????????生產(chǎn)者工程提供服務(wù)接口的實(shí)現(xiàn)。
????????消費(fèi)者工程調(diào)用服務(wù)接口。
實(shí)現(xiàn)步驟:
①引入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>
?②在接口工程中創(chuàng)建接口
package com.example.service;public interface PrivoderService {String getInfo();
}
③在生產(chǎn)者工程中實(shí)現(xiàn)接口,并進(jìn)行dubbo的配置
接口實(shí)現(xiàn):使用dbboservice注解,將服務(wù)的實(shí)現(xiàn)暴露給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 "執(zhí)行成功";}
}
?配置文件:
server:port: 8881
dubbo:application:name: provider-service //dubbo的應(yīng)用名registry:protocol: zookeeper //使用zookeeper作為服務(wù)的注冊(cè)中心address: 127.0.0.1:2181 //zookeeper地址protocol:name: dubbo //使用dubbo協(xié)議port: 20885consumer:timeout: 60000 //調(diào)用接口的超時(shí)時(shí)間check: false //啟動(dòng)時(shí)不校驗(yàn)消費(fèi)者是否已啟動(dòng)
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
啟動(dòng)類配置:添加@EnableDubbo,用于將dubbo相關(guān)的配置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);}
}
?④生產(chǎn)者工程中調(diào)用接口
調(diào)用:使用DubboReference指定調(diào)用的服務(wù)
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));}
}
配置文件及啟動(dòng)類配置和生產(chǎn)者工程一致
啟動(dòng)zookeeper服務(wù)及生產(chǎn)者工程及消費(fèi)者工程,就可以進(jìn)行服務(wù)的調(diào)用。
3.總結(jié)
可以通過(guò)dubbo-admin進(jìn)行服務(wù)的管理及查看。
dubbo.consumer.timeout:調(diào)用超時(shí)時(shí)間(毫秒),默認(rèn)為 1000。debug模式下會(huì)導(dǎo)致調(diào)用失敗,所以需要調(diào)大。
dubbo.consumer.check:為true時(shí),開啟服務(wù)啟動(dòng)時(shí)檢查依賴的服務(wù)是否可用,默認(rèn)為 true。
也就是說(shuō),生產(chǎn)者沒(méi)有啟動(dòng)時(shí),消費(fèi)者無(wú)法啟動(dòng),需要設(shè)置為false
參照:SpringBoot整合dubbo+zooker搭建分布式服務(wù)(超詳細(xì))_springboot+dubbo分布式項(xiàng)目-CSDN博客
SpringBoot項(xiàng)目集成Dubbo_springboot集成dubbo-CSDN博客