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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud Sidecar –节点初始化

發布時間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud Sidecar –节点初始化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在上一篇博客文章中,我描述了Sidecar應用程序如何用于在Eureka中注冊Cassandra節點,并且更普遍地可以用于在Eureka中注冊任何非JVM應用程序。

在本文中,我將介紹應用程序如何查詢Sidecar注冊節點。

發現注冊的節點–初始化后

如果在Bean初始化階段不需要注冊的節點,則沿著以下方向很容易發現節點:

@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名稱注冊的節點。

請注意,節點是通過run方法打印的,該方法在Spring容器的初始化之后被調用。 但是,如果嘗試從某個生命周期階段中列出節點,請說postConstruct方法,那么很有可能會引發異常(此行為在Spring Cloud的“ Angel.SR3”版本中可見,但在“ Brixton。*”版本)

發現注冊節點–初始化期間

現在,如果應用程序需要在初始化期間發現節點,則流程會稍微復雜一些,有關潛在問題,請查看此工單 。

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

舉例來說,假設應用程序現在使用Sidecar注冊了Cassandra節點,以初始化Cassandra連接,一種方法是使用以下方式圍繞Cassandra連接創建包裝器:

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

這里,CassandraTemplate被重寫,以防止在afterPropertiesSet方法中檢查是否存在Cassandra會話,因為該會話將在啟動周期的更晚時間建立。

可以將Cassandra會話懶惰地注入到實現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會話可以在周期的后期創建。 有點粗糙,但是這種方法可行。

  • 如果您有興趣進一步探索此示例,請在以下位置獲得此代碼
    我的github倉庫在這里 。

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。