服務之間的呼叫
在上面兩篇文章中,我們分別建立了註冊中心eurek和乙個服務pricing,這一單介紹服務之間的調.
@enableeurekaclient // 用於註冊中心發現這個服務,另外這裡注意一點,這裡可以使用@enablediscoveryclient. 這倆個的區別是前面的這個只能用於eureka註冊中心,後面的也可以用於其他註冊中心.
@enablefeignclients // 啟用feign client的搜尋, 新增這個annotation 之後會自動搜尋帶有@feignclient的標識的服務,只有新增了這個annotation才可以呼叫其他的服務
import org.springframework.cloud.netflix.eureka.enableeurekaclient;
import org.springframework.cloud.openfeign.enablefeignclients;
@enableeurekaclient
@enablefeignclients
public static void main(string args) }
接著建立乙個productresource(class)和pricingfeignclient (inte***ce)
pricingfeignclient // 呼叫pricing服務
package com.example.demo.client;
import org.springframework.cloud.openfeign.feignclient;
@feignclient(name="pricing")
public inte***ce pricingfeignclient
注意,我在這裡遇見乙個坑,當我完成之後,發現跑不起來,報如下的錯誤,
一直報錯failed to introspect class [org.springframework.cloud.openfeign.feignclientfactorybean] from classl......
檢查之後發現是因為在pricing服務中引入了org.springframework.cloud.netflix.feign.feignclient,而在product服務中引入的是org.springframework.cloud.openfeign.feignclient. 這裡要註冊,一定要引用一樣的feignclient包
productresource
package com.example.demo.rest;
import com.example.demo.client.pricingfeignclient;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class productresource
}
編寫完之後拋起eureka,pricing,product這三個工程.訪問localhost:8761
這時候去訪問http://localhost:8082/v1/api/product-pricing
最終得到了pricing介面的返回值100. 服務之間的呼叫完成.
SpringCloud入門實戰全系列(超詳細)
springcloudlearning lingluocloud api 構建公共子模組 hystrix服務降級 lingluocloud euraka 7001 eureka集群 lingluocloud euraka 7002 eureka集群 lingluocloud euraka 7003 ...
Spring Cloud(十二)Zuul實戰
1 建立乙個eureka server工程,專案名稱 eureka server 2 建立乙個eureka client 服務提供者 工程,專案名稱 eureka client1 工程eureka server和eureka client1 建立請參考 spring cloud 三 eureka實戰...
SpringCloud 入門介紹
業界大牛馬丁.福勒 martin fowler 這樣描述微服務 微服務 強調的是服務的大小,它關注的是某乙個點,是具體解決某乙個問題 提供落地對應服務的乙個服務應用,狹意的看,可以看作eclipse裡面的乙個個微服務工程 或者module 微服務化的核心就是將傳統的一站式應用,根據業務拆分成乙個乙個...