Spring Sidecar如何与Docker配合使用

缺口

我们有一个sidecar应用程序,使我们能够向Eureka注册我们的node.js应用程序以启用服务发现。

我们的sidecar应用程序配置如下所示:

server:
  port: 9000

spring:
  application:
    name: session-service

sidecar:
  port: 3000
  health-url: http://sessionServiceNode:${sidecar.port}/health.json

eureka:
  client:
    serviceUrl:
      defaultZone: http://discoveryService:8761/eureka/
  instance:
    lease-renewal-interval-in-seconds: 5
    prefer-ip-address: true

按照该配置,我们的节点应用在端口上运行3000由定义的sidecar.port属性,我们的车斗的应用程序应该在端口上运行9000server.port

我们在节点应用程序中添加了一个端点,以允许Sidecar检查应用程序的运行状况(sidecar.health-url)。主机名sessionServiceNode是我们赋予运行节点应用程序的容器的别名的名称。

我们的Eureka服务在单独的容器中运行,该容器也通过别名DiscoveryService链接到sidecar应用程序容器。

我们有一个单独的测试spring boot应用程序,它在一个单独的容器中运行,该容器是会话服务的使用者。该容器仅链接到发现服务容器。

边车应用程序按预期向Eureka注册

在此处输入图片说明

测试服务对会话服务使用两种形式的查找。一个使用假冒客户:

@FeignClient(value = "session-service") // name of our registered service in eureka
interface SessionServiceClient {

    @RequestMapping(value = "/document/get/24324", method = GET)
    String documentGetTest();

}

另一种方法使用更具编程性的查找:

@Autowired
private DiscoveryClient discoveryClient;

...
discoveryClient.getInstances("session-service");

当我们向测试服务发出请求时,测试服务会为会话服务进行查找,但是eureka给我们返回的实例信息具有URI ::http://172.17.0.5:3000这是不正确的。172.17.0.5边车应用程序容器的IP地址3000是什么,但是端口是节点应用程序运行所在的端口

应该期望看到eureka通过会话服务端口(http://172.17.0.5:9000向我们返回会话服务容器的位置,然后sidecarhttp://172.17.0.6:3000通过zuul代理为我们(向节点应用程序“转发”吗?还是应该Eureka直接给我们提供节点应用程序的位置?

我在下面包括了来自尤里卡的会话服务实例信息:

<?xml version="1.0" encoding="UTF-8"?>
<application>
   <name>SESSION-SERVICE</name>
   <instance>
      <hostName>172.17.0.5</hostName>
      <app>SESSION-SERVICE</app>
      <ipAddr>172.17.0.5</ipAddr>
      <status>UP</status>
      <overriddenstatus>UNKNOWN</overriddenstatus>
      <port enabled="true">3000</port>
      <securePort enabled="false">443</securePort>
      <countryId>1</countryId>
      <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
         <name>MyOwn</name>
      </dataCenterInfo>
      <leaseInfo>
         <renewalIntervalInSecs>5</renewalIntervalInSecs>
         <durationInSecs>90</durationInSecs>
         <registrationTimestamp>1461223810081</registrationTimestamp>
         <lastRenewalTimestamp>1461224812429</lastRenewalTimestamp>
         <evictionTimestamp>0</evictionTimestamp>
         <serviceUpTimestamp>1461223810081</serviceUpTimestamp>
      </leaseInfo>
      <metadata class="java.util.Collections$EmptyMap" />
      <homePageUrl>http://c892e0c03cf4:3000/</homePageUrl>
      <statusPageUrl>http://c892e0c03cf4:9000/info</statusPageUrl>
      <healthCheckUrl>http://c892e0c03cf4:9000/health</healthCheckUrl>
      <vipAddress>session-service</vipAddress>
      <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
      <lastUpdatedTimestamp>1461223810081</lastUpdatedTimestamp>
      <lastDirtyTimestamp>1461223033045</lastDirtyTimestamp>
      <actionType>ADDED</actionType>
   </instance>
</application>

编辑:

查看代码后,Eureka使用从InetAddress.getLocalHost().getHostAddress()返回的主机信息InetAddress.getLocalHost().getHostName()分别确定实例的地址。这就是为什么我们要获得sidecar容器的IP地址的原因。有什么方法可以覆盖此行为?

缺口

因此,从外观上看,Sidecar假设spring boot sidecar应用程序和非jvm应用程序在同一主机上运行。在我们的方案中,我们在单独的容器中运行所有内容。我们的sidecar jvm应用程序一个容器,我们的node.js应用程序一个容器。从理论上讲,我们可以在同一个容器中运行两个应用程序,但这违反了Docker的最佳实践,即每个容器只有一个unix进程。

为了使它起作用,我们重写了EurekaInstanceConfigBean,它允许我们控制为实例选择的主机名和IP地址。在这种情况下,我们委托给inetUtils该类并从主机名(这是通过docker链接的非jvm应用程序的别名)中查找IP地址。我们利用spring@ConfigurationPropertiesapplication.yml配置文件中控制主机名/端口

SessionServiceSidecar.java

@Component
@ConfigurationProperties
public class SessionServiceSidecarProperties {

    @Value("${sidecar.hostname}")
    private String hostname;

    @Value("${sidecar.port}")
    private Integer port;

    public String getHostname() {
        return hostname;
    }

    public Integer getPort() {
        return port;
    }

}

SessionServiceApp.java

@SpringBootApplication
@EnableSidecar
@EnableDiscoveryClient
@Configuration
@ComponentScan
@EnableConfigurationProperties
public class SessionServiceApp {

    private @Autowired SessionServiceSidecarProperties properties;

    public static void main(String[] args) {
        SpringApplication.run(SessionServiceApp.class, args);
    }

    @Bean
    public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils) {

        final String sidecarHostname = properties.getHostname();
        final Integer sidecarPort = properties.getPort();

        try {

            final EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);
            instance.setHostname(sidecarHostname);
            instance.setIpAddress(inetUtils.convertAddress(InetAddress.getByName(sidecarHostname)).getIpAddress());
            instance.setNonSecurePort(sidecarPort);
            return instance;

        } catch(UnknownHostException e) {
            throw new IllegalStateException("Could not resolve IP address of sidecar application using hostname: " + sidecarHostname);
        }

    }

}

application.yml

spring:
  application:
    name: session-service

server:
  port: 9000

sidecar:
  hostname: sessionServiceNode
  port: 3000
  health-url: http://sessionServiceNode:${sidecar.port}/health.json

eureka:
  client:
    serviceUrl:
      defaultZone: http://discoveryService:8761/eureka/
  instance:
    lease-renewal-interval-in-seconds: 5
    prefer-ip-address: true

希望Spring家伙将来允许我们通过sidecar.hostname属性控制Sidecar应用程序的主机名以及端口

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章