在上一篇博客文章中,我描述了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 –节点初始化相关推荐

  1. sidecar_Spring Cloud Sidecar –节点初始化

    sidecar 在上一篇博客文章中,我描述了Sidecar应用程序如何用于在Eureka中注册Cassandra节点,更一般地,它可以用于在Eureka中注册任何非JVM应用程序. 在本文中,我将介绍 ...

  2. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  3. 用Spring Cloud Alibaba开发微服务会更香吗?

    关注DD,除了前沿消息,还有每周福利哦 Spring Cloud Alibaba致力于提供微服务开发的一站式解决方案,它是Spring Cloud组件被植入Alibaba元素之后的产物. 利用Spri ...

  4. Spring Cloud Config Server迁移节点或容器化带来的问题

    如果您跟我一样,目前正在使用Spring Cloud Config做为配置中心的话,本篇将来要描述的问题,强烈推荐了解和关注!因为这个问题目前存在于所有的版本中,还没有修复,需要注意避开或设法解决. ...

  5. Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用

    文章目录 导读 官方文档 Eureka Server高可用集群概述 2个Eureka Server节点高可用集群搭建步骤 Step1. 新建子模块 microservice-discovery-eur ...

  6. Spring Cloud中查看服务网关(Zuul)中的所有路由节点

    问题描述:在Spring Cloud的早期版本(例如1.X版本)中如果想查看Zuul的所有路由节点,我们除了在pom.xml中引入依赖外,还需要在application.yml文件中添加如下配置: # ...

  7. Spring Cloud Gateway系列【4】初始化加载流程源码解析

    文章目录 核心源码 Route类 AsyncPredicate接口 ServerWebExchange 接口 GatewayFilter RouteLocator RouteDefinitionLoc ...

  8. SpringCloud重要知识点总结,动力节点Spring Cloud入门到精通学习教程

    SpringCloud的教程很多小伙伴一定都看过,那么,在SpringCloud学习中需要掌握哪些知识点?今天来给大家梳理下. 什么是分布式? 不同模块部署在不同服务器上 作用:分布式解决网站高并发带 ...

  9. Spring Cloud Netflix中文文档翻译笔记

    原文地址:http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.2.2.RELEASE/ Spring Cloud Net ...

最新文章

  1. DLL基础——Windows核心编程学习手札之十九
  2. C核心技术手册(五)
  3. 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
  4. [No000089]String的(补空位)左对齐,(补空位)右对齐
  5. C++异常处理全攻略
  6. jsp案例分析(二)-聊天室-2-逆向建模
  7. 锐捷无线项目调试(AC调试)
  8. 用友系统检查iis服务器不符,安装用友U8的时候已经安装了IIS但是环境检测的时候却通不过?...
  9. 确定性知识表示法--规则(产生式)表示法
  10. CA数字证书是什么?
  11. k43.第十七章 K8s运维篇-DevOps 持续集成 持续部署 (三)
  12. 教你玩转QQ的10大绝招
  13. VBA编程实例----绘制李萨茹图形
  14. 【简单图形解释】刚体上任意一点速度与刚体角速度的关系
  15. 谷歌移动UI框架Flutter教程之Widget
  16. [C语言]常用库函数
  17. Alexa | Alexa通信扩展
  18. android版本 6手机版下载,granboard安卓版下载-GranBoard 安卓版v6.1.2-PC6安卓网
  19. (附源码)计算机毕业设计ssm财务管理系统
  20. 常开常闭触点?限位开关?

热门文章

  1. js的三元表达式用来替换表格中的颜色
  2. bootstrap组件的案例代码
  3. 进入ASP .net mvc的世界
  4. JS中对象创建的五中方式
  5. 定时任务---SpringBoot
  6. C语言 立方体随鼠标转动,HTML5鼠标控制的旋转的立方体
  7. linux更改桌面壁纸的脚本,自动更换桌面壁纸的脚本,支持Ubuntu 18.04系统
  8. flask执行python脚本_如何在flask后端运行python脚本?
  9. finally块不被执行的情况总结
  10. aws 删除ec2实例_如何在AWS中启动EC2实例