当一个项目变得庞大,可以不是同一个语言,例如php和java

这时将不同的语言和springcloud结合起来时需要用到sidecar

一、maven依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-netflix-sidecar</artifactId>
  12. </dependency>

二、配置文件

这里使用php和springcloud结合php

php项目的端口是80,为了确保该项目注册到eureka中并且可用,php项目需要返回一个health.json

# 端口
server.port=10980
# 应用别名
spring.application.name=sidecar
eureka.client.service-url.defaultZone=http://user:123@localhost:8761/eureka/,http://user:123@localhost:8762/eureka/,http://user:123@localhost:8763/eureka/
eureka.instance.prefer-ip-address=true

# 配置异构服务器端口 host(域名)必须与sidecar所在的一致
sidecar.port=80
sidecar.health-uri=http://localhost:80/health.json

返回health.json的数据

{ "status" : "UP" }

这样就表示该项目没有挂

三、启动类

@SpringBootApplication
@EnableSidecar
public class SideCarApp {

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

}

项目启动之后本项目就可以当php项目用了