SideCar主要是用于将非JVM语言的服务,代理为Netflix服务
1、SideCarApp
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.sidecar.EnableSidecar; import org.springframework.web.bind.annotation.RequestMapping; //作用一、注册到eureka并提供服务健康状态 //其他语言的服务,首先服务封装为REST或JSON通信,并提供一个health路径,反馈服务是否正常 //sidecar会将这个服务注册到eureka //其他服务,可以通过eureka查询并调用该服务 //作用二、从sidecar获取服务地址,并调用 //访问sidecar,获取服务地址 http://localhost:port/hosts/{serviceId}. //分析返回结果,得到服务地址 //调用需要的服务 @EnableSidecar public class SideCarApp { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } @RequestMapping("/") public String hello() { return "It works"; } public static void main( String[] args ) { new SpringApplicationBuilder(SideCarApp.class).web(true).run(args); } }
2、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-sidecar-test</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-sidecar-test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3、application.properties
spring.application.name=zuul-gateway-app server.port=7001 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 sidecar.port=9999 sidecar.health-uri=http://localhost:9999/health.json