javascript
sidecar_Spring Cloud Sidecar –节点初始化
sidecar
在上一篇博客文章中,我描述了Sidecar應(yīng)用程序如何用于在Eureka中注冊Cassandra節(jié)點,更一般地,它可以用于在Eureka中注冊任何非JVM應(yīng)用程序。
在本文中,我將介紹應(yīng)用程序如何查詢Sidecar注冊節(jié)點。
發(fā)現(xiàn)注冊節(jié)點–初始化后
如果在bean初始化階段不需要注冊的節(jié)點,則沿著以下方向很容易發(fā)現(xiàn)節(jié)點:
@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名稱注冊的節(jié)點。
請注意,節(jié)點是通過run方法打印的,該方法在Spring容器的初始化之后被調(diào)用。 但是,如果嘗試從某個生命周期階段中列出節(jié)點,請說postConstruct方法,那么很有可能會引發(fā)異常(此行為在Spring Cloud的“ Angel.SR3”版本中可見,但似乎可以與“ Brixton。*”版本)
發(fā)現(xiàn)注冊節(jié)點–初始化期間
現(xiàn)在,如果應(yīng)用程序需要在初始化期間發(fā)現(xiàn)節(jié)點,則流程會稍微復(fù)雜一點,有關(guān)潛在問題,請查看此工單 。
DiscoveryClient在Spring生命周期的很晚才初始化,并且如果在任何bean的后處理活動中使用DiscoveryClient,則很可能會引發(fā)異常。
例如,假設(shè)應(yīng)用程序現(xiàn)在使用Sidecar注冊的Cassandra節(jié)點被應(yīng)用程序用來初始化Cassandra連接,一種實現(xiàn)方法是用以下方式圍繞Cassandra連接創(chuàng)建包裝器:
import org.springframework.data.cassandra.core.CassandraTemplate;public class CassandraTemplateWrapper extends CassandraTemplate {@Overridepublic void afterPropertiesSet() {} }在這里,CassandraTemplate被重寫,以防止在afterPropertiesSet方法中檢查是否存在Cassandra會話,因為該會話將在啟動周期的更晚時間建立。
可以將Cassandra會話懶惰地注入到實現(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會話可以在周期的后期創(chuàng)建。 有點粗糙,但是這種方法可行。
- 如果您有興趣進(jìn)一步探索此示例,請在以下位置獲得此代碼
我的github倉庫在這里 。
翻譯自: https://www.javacodegeeks.com/2015/09/spring-cloud-sidecar-initialization-of-nodes.html
sidecar
總結(jié)
以上是生活随笔為你收集整理的sidecar_Spring Cloud Sidecar –节点初始化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bios设置图解教程电脑如何bios
- 下一篇: 快影APP怎样剪辑视频?快影APP剪辑视