本篇內(nèi)容介紹了“spring-cloud-kubernetes的三個關(guān)鍵知識點是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)主營江夏網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),江夏h5小程序定制開發(fā)搭建,江夏網(wǎng)站營銷推廣歡迎江夏等地區(qū)企業(yè)咨詢
先來回顧一下上一章的DiscoveryController.java的內(nèi)容:
@RestController public class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; /** * 探針檢查響應(yīng)類 * @return */ @RequestMapping("/health") public String health() { return "health"; } /** * 返回遠(yuǎn)程調(diào)用的結(jié)果 * @return */ @RequestMapping("/getservicedetail") public String getUri( @RequestParam(value = "servicename", defaultValue = "") String servicename) { return "Service [" + servicename + "]'s instance list : " + JSON.toJSONString(discoveryClient.getInstances(servicename)); } /** * 返回發(fā)現(xiàn)的所有服務(wù) * @return */ @RequestMapping("/services") public String services() { return this.discoveryClient.getServices().toString() + ", " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } }
上述代碼中,我們并沒有寫創(chuàng)建DiscoveryClient實例的代碼,discoveryClient從何而來?
這一切,要從DiscoveryController.java所在項目的pom.xml說起;
在pom.xml中,有對spring-cloud-kubernetes框架的依賴配置:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-kubernetes-discovery</artifactId> <version>1.0.1.RELEASE</version> </dependency>
打開spring-cloud-kubernetes-discovery的源碼,地址是:https://github.com/spring-cloud/spring-cloud-kubernetes/tree/master/spring-cloud-kubernetes-discovery ,在這個工程中發(fā)現(xiàn)了文件spring.factories:
spring容器啟動時,會尋找classpath下所有spring.factories文件(包括jar文件中的),spring.factories中配置的所有類都會實例化,我們在開發(fā)springboot時常用到的XXX-starter.jar就用到了這個技術(shù),效果是一旦依賴了某個starter.jar很多功能就在spring初始化時候自動執(zhí)行了(例如MySQL的starter,啟動時會連接數(shù)據(jù)庫)
spring.factories文件中有兩個類:KubernetesDiscoveryClientAutoConfiguration和KubernetesDiscoveryClientConfigClientBootstrapConfiguration都會被實例化;
先看KubernetesDiscoveryClientConfigClientBootstrapConfiguration,很簡單的源碼,KubernetesAutoConfiguration和KubernetesDiscoveryClientAutoConfiguration這兩個類會被實例化:
/** * Bootstrap config for Kubernetes discovery config client. * * @author Zhanwei Wang */ @Configuration @ConditionalOnProperty("spring.cloud.config.discovery.enabled") @Import({ KubernetesAutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class }) public class KubernetesDiscoveryClientConfigClientBootstrapConfiguration { }
在KubernetesAutoConfiguration的源碼中,會實例化一個重要的類:DefaultKubernetesClient,如下:
@Bean @ConditionalOnMissingBean public KubernetesClient kubernetesClient(Config config) { return new DefaultKubernetesClient(config); }
再看KubernetesDiscoveryClientAutoConfiguration源碼,注意<font color="blue">kubernetesDiscoveryClient</font>方法,這里面實例化了DiscoveryController所需的DiscoveryClient接口實現(xiàn),還要重點關(guān)注的地方是KubernetesClient參數(shù)的值,是上面提到的DefaultKubernetesClient對象:
@Bean @ConditionalOnMissingBean @ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.enabled", matchIfMissing = true) public KubernetesDiscoveryClient kubernetesDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties properties, KubernetesClientServicesFunction kubernetesClientServicesFunction, DefaultIsServicePortSecureResolver isServicePortSecureResolver) { return new KubernetesDiscoveryClient(client, properties, kubernetesClientServicesFunction, isServicePortSecureResolver); }
至此,第一個問題算是弄清楚了:我們編寫的DiscoveryController類所需的DiscoveryClient接口實現(xiàn)類是KubernetesDiscoveryClient,用到的是spring規(guī)范中的spring.factories
另外有一點很重要,下面要用到的:KubernetesDiscoveryClient有個成員變量是KubernetesClient,該變量的值是DefaultKubernetesClient實例;
接下來看第二個問題;
看看DiscoveryController是如何獲取所在kubernetes的服務(wù)信息的:
@RequestMapping("/services") public String services() { return this.discoveryClient.getServices().toString() + ", " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); }
如上所示,<font color="blue">discoveryClient.getServices()</font>方法返回了所有kubernetes的服務(wù)信息; 2. discoveryClient對應(yīng)的類是spring-cloud-kubernetes項目的KubernetesDiscoveryClient.java,看方法:
public List<String> getServices(Predicate<Service> filter) { return this.kubernetesClientServicesFunction.apply(this.client).list().getItems() .stream().filter(filter).map(s -> s.getMetadata().getName()) .collect(Collectors.toList()); }
這段代碼的關(guān)鍵在于<font color="blue">this.kubernetesClientServicesFunction.apply(this.client).list()</font>,先看KubernetesClientServicesFunction實例的初始化過程,在KubernetesDiscoveryClientAutoConfiguration類中:
@Bean public KubernetesClientServicesFunction servicesFunction( KubernetesDiscoveryProperties properties) { if (properties.getServiceLabels().isEmpty()) { return KubernetesClient::services; } return (client) -> client.services().withLabels(properties.getServiceLabels()); }
KubernetesClientServicesFunction是個lambda表達(dá)式,用于KubernetesClient的時候,返回KubernetesClient.services()的結(jié)果,如果指定了標(biāo)簽過濾,就用指定的標(biāo)簽來做過濾(也就是kubernetes中的標(biāo)簽選擇器的效果)
因此,數(shù)據(jù)來源其實就是上面的<font color="blue">this.client</font>,調(diào)用其services方法的返回結(jié)果; 3. KubernetesDiscoveryClient.getServices方法中的<font color="blue">this.client</font>是什么呢?分析前面的問題時已經(jīng)提到過了,就是DefaultKubernetesClient類的實例,所以,此時要去看DefaultKubernetesClient.services方法,發(fā)現(xiàn)client是ServiceOperationsImpl實例:
@Override public MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> services() { return new ServiceOperationsImpl(httpClient, getConfiguration(), getNamespace()); }
接著看ServiceOperationsImpl.java,我們關(guān)心的是它的list方法,此方法在父類BaseOperation中找到:
public L list() throws KubernetesClientException { try { HttpUrl.Builder requestUrlBuilder = HttpUrl.get(getNamespacedUrl()).newBuilder(); String labelQueryParam = getLabelQueryParam(); if (Utils.isNotNullOrEmpty(labelQueryParam)) { requestUrlBuilder.addQueryParameter("labelSelector", labelQueryParam); } String fieldQueryString = getFieldQueryParam(); if (Utils.isNotNullOrEmpty(fieldQueryString)) { requestUrlBuilder.addQueryParameter("fieldSelector", fieldQueryString); } Request.Builder requestBuilder = new Request.Builder().get().url(requestUrlBuilder.build()); L answer = handleResponse(requestBuilder, listType); updateApiVersion(answer); return answer; } catch (InterruptedException | ExecutionException | IOException e) { throw KubernetesClientException.launderThrowable(forOperationType("list"), e); } }
展開上面代碼的handleResponse方法,可見里面是一次http請求,至于請求的地址,可以展開getNamespacedUrl()方法,里面調(diào)用的getRootUrl方法如下:
public URL getRootUrl() { try { if (apiGroup != null) { return new URL(URLUtils.join(config.getMasterUrl().toString(), "apis", apiGroup, apiVersion)); } return new URL(URLUtils.join(config.getMasterUrl().toString(), "api", apiVersion)); } catch (MalformedURLException e) { throw KubernetesClientException.launderThrowable(e); } }
可見最終的地址應(yīng)該是:<font color="blue">xxxxxx/api/v1</font>或者<font color="blue">xxxxxx/apis/xx/v1</font>這樣的字符串。
這樣的字符串意味著什么呢?<font color="red">這是訪問kubernetes的API Server時用到的URL標(biāo)準(zhǔn)格式</font>,有關(guān)API Server服務(wù)的詳情請參考官方文檔,地址是:https://kubernetes.io/docs/reference/using-api/api-concepts/
如下圖,用OperationSupport類的源碼和官方文檔的URL截圖做個對比,大家就一目了然了: 5. 還剩個小問題,上圖中,OperationSupport類的成員變量resourceT是什么值?官方文檔示例中是"pods",在獲取service的時候又該是多少呢?順著源碼一路找下去,找到了類的構(gòu)造方法,如下所示,第五個參數(shù)就是resourceT,這里直接被寫死為"services":
public ServiceOperationsImpl(OkHttpClient client, Config config, String apiVersion, String namespace, String name, Boolean cascading, Service item, String resourceVersion, Boolean reloadingFromServer, long gracePeriodSeconds, Map<String, String> labels, Map<String, String> labelsNot, Map<String, String[]> labelsIn, Map<String, String[]> labelsNotIn, Map<String, String> fields) { super(client, config, null, apiVersion, "services", namespace, name, cascading, item, resourceVersion, reloadingFromServer, gracePeriodSeconds, labels, labelsNot, labelsIn, labelsNotIn, fields); }
至此,第二個問題“controller中用到的kubernetes服務(wù)數(shù)據(jù)從何而來"已經(jīng)清楚了:最終是調(diào)用okhttp的newCall方法向kubernetes的API Server發(fā)起http請求,獲取service資源的數(shù)據(jù)列表;
接下來,該最后一個問題了;
關(guān)于API Server如何響應(yīng)各類http請求,本文只做一些簡單的說明,詳細(xì)信息還請參考官方文檔,地址是:https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/
如下圖所示,在kubernetes環(huán)境中,pod、service這些資源的數(shù)據(jù)都存儲在etcd,任何服務(wù)想要增刪改查etcd的數(shù)據(jù),都只能通過向API Server發(fā)起RestFul請求的方式來完成,咱們的DiscoveryController類獲取所有service也是發(fā)請求到API Server,由API Server從etcd中取得service的數(shù)據(jù)返回給DiscoveryController:
“spring-cloud-kubernetes的三個關(guān)鍵知識點是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)站名稱:spring-cloud-kubernetes的三個關(guān)鍵知識點是什么
文章網(wǎng)址:http://aaarwkj.com/article8/igeoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、用戶體驗、網(wǎng)站排名、定制開發(fā)、網(wǎng)站設(shè)計、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)