服務(wù)測(cè)試環(huán)境
1.引入Eureka所需的依賴(lài)
2.修改配置文件
3.啟動(dòng)服務(wù)
4.測(cè)試
消費(fèi)者
1.發(fā)現(xiàn)客戶(hù)端方式
2.Ribbon方式功能的Spring RestTemplate
3.feign客戶(hù)端方式
本文主要介紹SpringCloud中調(diào)用服務(wù)的方式:
Spring DiscoveryClient
支持 Ribbon 的 RestTemplate
Feign客戶(hù)端
服務(wù)測(cè)試環(huán)境
測(cè)試中,發(fā)現(xiàn)Netflix的Eureka服務(wù)層采用。
主要步驟如下:
1.引入Eureka所需的依賴(lài)
org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.cloud spring-cloud-starter-eureka
2.修改配置文件
服務(wù)端:
eureka: instance: hostname:eureka9001.com#eureka服務(wù)端的實(shí)例名稱(chēng) instance-id:eureka9001 client: register-with-eureka:false#false表示不向注冊(cè)中心注冊(cè)自己 fetch-registry:false##false表示自己就是注冊(cè)中心,職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù) service-url: defaulteZone:http://127.0.0.1:9001
客戶(hù)端1:
server: port:8002 spring: application: name:licensingservice eureka: instance: instance-id:licensing-service-8002 prefer-ip-address:true client: register-with-eureka:true fetch-registry:true service-url: defaultZone:http://127.0.0.1:9001/eureka/,
客戶(hù)端2:
server: port:8002 spring: application: name:licensingservice eureka: instance: instance-id:licensing-service-8002 prefer-ip-address:true client: register-with-eureka:true fetch-registry:true service-url: defaultZone:http://127.0.0.1:9001/eureka/,
一組微服務(wù)的不同實(shí)例采辦服務(wù)名稱(chēng)不同,不同的實(shí)例ID不同,不同,spring.application.name和eureka.instance.instance-id。
3.啟動(dòng)服務(wù)
服務(wù)端:
@SpringBootApplication
@EnableEurekaServer
publicclassEurekaServerPort9001_App{
publicstaticvoidmain(String[]args){
SpringApplication.run(EurekaServerPort9001_App.class,args);
}
}
客戶(hù)端:
@SpringBootApplication @RefreshScope @EnableEurekaClient publicclassLicenseApplication_8002{ publicstaticvoidmain(String[]args){ SpringApplication.run(LicenseApplication_8002.class,args); } }
4.測(cè)試
進(jìn)入到Eureka服務(wù)端地址,我這是127.0.0.1:9001,可以查看注冊(cè)到注冊(cè)中心的服務(wù)。

注意:Eureka通過(guò)兩次服務(wù)檢測(cè)均到通過(guò),服務(wù)將在間隔10秒內(nèi)成功啟動(dòng)服務(wù)注冊(cè)等待30秒。
基于 Spring Boot + MyBatis Plus + Vue & Element 實(shí)現(xiàn)的后臺(tái)管理系統(tǒng) + 用戶(hù)小程序,支持 RBAC 動(dòng)態(tài)權(quán)限、多租戶(hù)、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
消費(fèi)者
1.發(fā)現(xiàn)客戶(hù)端方式
服務(wù)中既是服務(wù)是微信也可以是調(diào)用者,消費(fèi)者配置和上面配置的大體參考一致,依賴(lài)及配置服務(wù)端啟動(dòng)方式EnableEurekaClient:監(jiān)聽(tīng)啟動(dòng)。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
publicclassConsumerApplication_7002{
publicstaticvoidmain(String[]args){
SpringApplication.run(ConsumerApplication_7002.class,args);
}
}
核心配置類(lèi):
@Component
publicclassConsumerDiscoveryClient{
@Autowired
privateDiscoveryClientdiscoveryClient;
publicServiceInstancegetServiceInstance(){
ListserviceInstances=discoveryClient.getInstances("licensingservice");
if(serviceInstances.size()==0){
returnnull;
}
returnserviceInstances.get(0);
}
publicStringgetUrl(Stringurl){
ServiceInstanceserviceInstance=this.getServiceInstance();
if(serviceInstance==null)
thrownewRuntimeException("404,NOTFOUND");
StringurlR=String.format(url,serviceInstance.getUri().toString());
returnurlR;
}
}
通過(guò)DiscoveryClient從尤里卡中獲取licensingservice服務(wù)的實(shí)例,并返回第一個(gè)實(shí)例。
測(cè)試控制器
@RestController @RequestMapping("test") publicclassTestController{ //注意必須new,否則會(huì)被ribbon攔截器攔截,改變URL行為 privateRestTemplaterestTemplate=newRestTemplate(); @Autowired privateConsumerDiscoveryClientconsumerDiscoveryClient; @RequestMapping("/getAllEmp") publicListgetAllLicense(){ Stringurl=consumerDiscoveryClient.getUrl("%s/test/getAllEmp"); returnrestTemplate.getForObject(url,List.class); } }
測(cè)試:
調(diào)試信息

從該圖可以看到licensingservice,擁有兩個(gè)服務(wù)實(shí)例,并可以查看實(shí)例信息。
頁(yè)面返回信息

成功查詢(xún)到數(shù)據(jù)庫(kù)存儲(chǔ)信息。
2.Ribbon方式功能的Spring RestTemplate
同上。
核心配置類(lèi):
//指明負(fù)載均衡算法 @Bean publicIRuleiRule(){ returnnewRoundRobinRule(); } @Bean @LoadBalanced//告訴Spring創(chuàng)建一個(gè)支持Ribbon負(fù)載均衡的RestTemplate publicRestTemplaterestTemplate(){ returnnewRestTemplate(); }
設(shè)置均衡方式為輪詢(xún)方式。
測(cè)試類(lèi):
@RestController
@RequestMapping("rest")
publicclassConsumerRestController{
@Autowired
privateRestTemplaterestTemplate;
privatefinalstaticStringSERVICE_URL_PREFIX="http://LICENSINGSERVICE";
@RequestMapping("/getById")
publicEmpgetById(Longid){
MultiValueMapparamMap=newLinkedMultiValueMap();
paramMap.add("id",id);
returnrestTemplate.postForObject(SERVICE_URL_PREFIX+"/test/getById",paramMap,Emp.class);
}
}
測(cè)試結(jié)果:
第一次:

第二次:

第三次:

因?yàn)椴捎幂喸?xún)平均方式分別使用不同的服務(wù)實(shí)例,未區(qū)別,將名稱(chēng)確定了一定的。
這上面,Ribbon 是對(duì)第一種方式的封裝方式和不同的負(fù)載率。 Feign的方式則大大降低了開(kāi)發(fā)客戶(hù)端和提升速度。
3.feign客戶(hù)端方式
引入依賴(lài):
org.springframework.cloud spring-cloud-starter-feign
主要代碼:
@FeignClient(value="LICENSINGSERVICE",fallbackFactory=ServiceImp.class)
publicinterfaceServiceInterface{
@RequestMapping("/test/getById")
EmpgetById(@RequestParam("id")Longid);
@RequestMapping("/test/getLicenseById")
LicensegetLicenseById(@RequestParam("id")Longid);
@RequestMapping("/test/getAllEmp")
ListgetAllLicense();
}
@Component
publicclassServiceImpimplementsFallbackFactory{
@Override
publicServiceInterfacecreate(Throwablethrowable){
returnnewServiceInterface(){
@Override
publicEmpgetById(Longid){
Empemp=newEmp();
emp.setName("iamfeignfallbackcreate");
returnemp;
}
@Override
publicLicensegetLicenseById(Longid){
returnnull;
}
@Override
publicListgetAllLicense(){
returnnull;
}
};
}
}
注采用接口模式,通過(guò)指定服務(wù)名以及方法,在服務(wù)開(kāi)發(fā)結(jié)果不佳時(shí),方便返回默認(rèn)的,而不是一個(gè)不友好的錯(cuò)誤。
主啟動(dòng)類(lèi):
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
publicclassConsumer_feign_Application_7004{
publicstaticvoidmain(String[]args){
SpringApplication.run(Consumer_feign_Application_7004.class,args);
}
}
測(cè)試類(lèi):
@RestController
@RequestMapping("rest")
publicclassConsumerRestController{
@Autowired
privateServiceInterfaceserviceInterface;
@Autowired
privateRestTemplaterestTemplate;
@RequestMapping("/getById")
publicEmpgetById(Longid){
returnserviceInterface.getById(id);
}
}
測(cè)試結(jié)果:
正常測(cè)試:

關(guān)閉兩個(gè)實(shí)例,模擬服務(wù)實(shí)例死亡:

假裝能夠故障服務(wù)調(diào)用,也可以實(shí)現(xiàn)調(diào)用的服務(wù)時(shí),友好的信息。
以此方式,由低上,從不同層次實(shí)現(xiàn)了SpringCloud中的微服務(wù)相互引用。
審核編輯:劉清
-
模擬器件
+關(guān)注
關(guān)注
2文章
108瀏覽量
23721 -
服務(wù)端
+關(guān)注
關(guān)注
0文章
68瀏覽量
7330
原文標(biāo)題:SpringCloud 三種服務(wù)調(diào)用方式,你學(xué)會(huì)了嗎?
文章出處:【微信號(hào):芋道源碼,微信公眾號(hào):芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
伺服電機(jī)的三種控制方式
FPGA中三種常用復(fù)位電路
STM32的三種boot模式介紹
發(fā)電機(jī)勵(lì)磁方式有哪些_三種發(fā)電機(jī)勵(lì)磁方式
壓供電系統(tǒng)的三種運(yùn)行方式
三種常見(jiàn)的服務(wù)器詳細(xì)介紹
伺服電機(jī)的三種控制方式該如何應(yīng)用
如何應(yīng)用伺服電機(jī)的三種控制方式
深入介紹嵌入式開(kāi)發(fā)中三種不同的程序架構(gòu)
FPGA應(yīng)用之vivado三種常用IP核的調(diào)用
Redis實(shí)現(xiàn)限流的三種方式分享
光伏發(fā)電系統(tǒng)中三種DC-DC轉(zhuǎn)換電路的研究
變頻器的三種控制方式 | 變頻器三種控制方式的優(yōu)缺點(diǎn)

介紹SpringCloud中三種服務(wù)調(diào)用方式
評(píng)論