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

注意:Eureka通過兩次服務檢測均到通過,服務將在間隔10秒內(nèi)成功啟動服務注冊等待30秒。
基于 Spring Boot + MyBatis Plus + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權限、多租戶、數(shù)據(jù)權限、工作流、三方登錄、支付、短信、商城等功能
消費者
1.發(fā)現(xiàn)客戶端方式
服務中既是服務是微信也可以是調用者,消費者配置和上面配置的大體參考一致,依賴及配置服務端啟動方式EnableEurekaClient:監(jiān)聽啟動。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
publicclassConsumerApplication_7002{
publicstaticvoidmain(String[]args){
SpringApplication.run(ConsumerApplication_7002.class,args);
}
}
核心配置類:
@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;
}
}
通過DiscoveryClient從尤里卡中獲取licensingservice服務的實例,并返回第一個實例。
測試控制器
@RestController @RequestMapping("test") publicclassTestController{ //注意必須new,否則會被ribbon攔截器攔截,改變URL行為 privateRestTemplaterestTemplate=newRestTemplate(); @Autowired privateConsumerDiscoveryClientconsumerDiscoveryClient; @RequestMapping("/getAllEmp") publicListgetAllLicense(){ Stringurl=consumerDiscoveryClient.getUrl("%s/test/getAllEmp"); returnrestTemplate.getForObject(url,List.class); } }
測試:
調試信息

從該圖可以看到licensingservice,擁有兩個服務實例,并可以查看實例信息。
頁面返回信息

成功查詢到數(shù)據(jù)庫存儲信息。
2.Ribbon方式功能的Spring RestTemplate
同上。
核心配置類:
//指明負載均衡算法 @Bean publicIRuleiRule(){ returnnewRoundRobinRule(); } @Bean @LoadBalanced//告訴Spring創(chuàng)建一個支持Ribbon負載均衡的RestTemplate publicRestTemplaterestTemplate(){ returnnewRestTemplate(); }
設置均衡方式為輪詢方式。
測試類:
@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);
}
}
測試結果:
第一次:

第二次:

第三次:

因為采用輪詢平均方式分別使用不同的服務實例,未區(qū)別,將名稱確定了一定的。
這上面,Ribbon 是對第一種方式的封裝方式和不同的負載率。 Feign的方式則大大降低了開發(fā)客戶端和提升速度。
3.feign客戶端方式
引入依賴:
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;
}
};
}
}
注采用接口模式,通過指定服務名以及方法,在服務開發(fā)結果不佳時,方便返回默認的,而不是一個不友好的錯誤。
主啟動類:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
publicclassConsumer_feign_Application_7004{
publicstaticvoidmain(String[]args){
SpringApplication.run(Consumer_feign_Application_7004.class,args);
}
}
測試類:
@RestController
@RequestMapping("rest")
publicclassConsumerRestController{
@Autowired
privateServiceInterfaceserviceInterface;
@Autowired
privateRestTemplaterestTemplate;
@RequestMapping("/getById")
publicEmpgetById(Longid){
returnserviceInterface.getById(id);
}
}
測試結果:
正常測試:

關閉兩個實例,模擬服務實例死亡:

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