javascript
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 –节点初始化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 廊坊银行客服电话是多少?
- 下一篇: guice 框架_玩! 框架+ Goog