日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring Cloud Sidecar –节点初始化

發(fā)布時(shí)間:2023/12/3 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud Sidecar –节点初始化 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在上一篇博客文章中,我描述了Sidecar應(yīng)用程序如何用于在Eureka中注冊(cè)Cassandra節(jié)點(diǎn),并且更普遍地可以用于在Eureka中注冊(cè)任何非JVM應(yīng)用程序。

在本文中,我將介紹應(yīng)用程序如何查詢(xún)Sidecar注冊(cè)節(jié)點(diǎn)。

發(fā)現(xiàn)注冊(cè)的節(jié)點(diǎn)–初始化后

如果在Bean初始化階段不需要注冊(cè)的節(jié)點(diǎn),則沿著以下方向很容易發(fā)現(xiàn)節(jié)點(diǎn):

@Component public class SampleCommandLineRunner implements CommandLineRunner {@Autowiredprivate DiscoveryClient discoveryClient;@PostConstructpublic void postConstruct() { // System.out.println("Printing from postConstruct"); // printDiscoveredNodes();}@Overridepublic void run(String... strings) throws Exception {System.out.println("Printing from run method");printDiscoveredNodes();}public void printDiscoveredNodes() {System.out.println(" Printing Discovered Nodes ");for (ServiceInstance instance: discoveryClient.getInstances("samplecassandra.vip")) {System.out.println("Host: Port = " + instance.getHost() + ":" + instance.getPort());}} }

這些將打印以“ samplecasssandra.vip” VIP名稱(chēng)注冊(cè)的節(jié)點(diǎn)。

請(qǐng)注意,節(jié)點(diǎn)是通過(guò)run方法打印的,該方法在Spring容器的初始化之后被調(diào)用。 但是,如果嘗試從某個(gè)生命周期階段中列出節(jié)點(diǎn),請(qǐng)說(shuō)postConstruct方法,那么很有可能會(huì)引發(fā)異常(此行為在Spring Cloud的“ Angel.SR3”版本中可見(jiàn),但在“ Brixton。*”版本)

發(fā)現(xiàn)注冊(cè)節(jié)點(diǎn)–初始化期間

現(xiàn)在,如果應(yīng)用程序需要在初始化期間發(fā)現(xiàn)節(jié)點(diǎn),則流程會(huì)稍微復(fù)雜一些,有關(guān)潛在問(wèn)題,請(qǐng)查看此工單 。

DiscoveryClient在Spring生命周期的很晚才初始化,并且如果在任何bean的后期處理活動(dòng)中使用DiscoveryClient,則很可能會(huì)引發(fā)異常。

舉例來(lái)說(shuō),假設(shè)應(yīng)用程序現(xiàn)在使用Sidecar注冊(cè)了Cassandra節(jié)點(diǎn),以初始化Cassandra連接,一種方法是使用以下方式圍繞Cassandra連接創(chuàng)建包裝器:

import org.springframework.data.cassandra.core.CassandraTemplate;public class CassandraTemplateWrapper extends CassandraTemplate {@Overridepublic void afterPropertiesSet() {} }

這里,CassandraTemplate被重寫(xiě),以防止在afterPropertiesSet方法中檢查是否存在Cassandra會(huì)話(huà),因?yàn)樵摃?huì)話(huà)將在啟動(dòng)周期的更晚時(shí)間建立。

可以將Cassandra會(huì)話(huà)懶惰地注入到實(shí)現(xiàn)SmartLifecyle的bean中的以下自定義CassandraTemplate中:

package mvctest.cassandra;import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.SmartLifecycle; import org.springframework.core.Ordered; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.stereotype.Component;@Component("cassandraTemplate") public class EurekaCassandraTemplateFactoryBean implements SmartLifecycle, FactoryBean<CassandraTemplate>, Ordered {....@Overridepublic boolean isAutoStartup() {return true;}@Overridepublic void start() {LOGGER.info("About to start Discovery client lookup of Cassandra Cluster!!!");final Cluster cluster = this.eurekaClusterBuilder.build();Session session = cluster.connect(this.cassandraProperties.getKeyspace());this.cassandraTemplateWrapper.setSession(session);LOGGER.info("Completed Discovery client lookup of Cassandra Cluster!!!");running = true;}@Overridepublic boolean isRunning() {return this.running;}@Overridepublic int getPhase() {return Integer.MAX_VALUE;}@Overridepublic int getOrder() {return 1;} }

這樣,Cassandra會(huì)話(huà)可以在周期的后期創(chuàng)建。 有點(diǎn)粗糙,但是這種方法可行。

  • 如果您有興趣進(jìn)一步探索此示例,請(qǐng)?jiān)谝韵挛恢毛@得此代碼
    我的github倉(cāng)庫(kù)在這里 。

翻譯自: https://www.javacodegeeks.com/2015/09/spring-cloud-sidecar-initialization-of-nodes.html

總結(jié)

以上是生活随笔為你收集整理的Spring Cloud Sidecar –节点初始化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。