Netflix Eureka是由Netflix开源的一款基于REST的服务治理组件,包括Eureka Server及Eureka Client。由于种种原因,Eureka 2.x版本已经冻结开发,目前最新版本是2018年8月份发布的1.9.4版本。
Eureka是Netflix公司提供的开源服务发现组件(现已闭源),最新版本是1.9.4,该组件提供的服务发现可以为负载均衡、failover等提供支持。Eureka包括Eureka Server和Eureka Client。Eureka Server提供REST服务,Eureka Clinet多数是使用Java编写的客户端(Eureka Client可以使用其他语言编写,比如Node.js或.NET),用于简化和Eureka Server的交互。
pom.xml内容如下:
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>watermelon.cloud</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Spring Cloud Eureka Server</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
<!-- 原版本 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 新版本 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
在启动类上加上@EnableEurekaServer注解。
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
server: port: 1111 spring: application: name: eureka-server eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ server: waitTimeInMsWhenSyncEmpty: 0 enableSelfPreservation: false # 后面详细讲配置
浏览器访问 http://localhost :1111/出现如下页面就说明服务端简单案例构建成功。
pom.xml内容如下:
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>watermelon.cloud</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Spring Cloud Eureka Client</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
@SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
server: port: 2222 spring: application: name: eureka-client # 如果不配置name属性,在注册中心的实例名都将是UNKNOWN eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/ # 服务注册中心地址
@RestController @Slf4j public class HelloController { private final DiscoveryClient client; @Autowired public HelloController(DiscoveryClient client) { this.client = client; } @GetMapping("/hello") public String sayHello() { List<ServiceInstance> serviceInstances = client.getInstances("eureka-client"); serviceInstances.forEach(serviceInstance -> { log.info("Host: {}, Port: {}", serviceInstance.getHost(), serviceInstance.getPort()); log.info("serviceId: {}, InstanceId: {}", serviceInstance.getServiceId(), serviceInstance.getInstanceId()); }); return "Hello, Spring Cloud Eureka. - " + LocalDateTime.now().toString(); } }
访问 http://localhost :111/eureka/,如果Server和Client都启动成功并且配置正确的情况将会如下情况。
访问 http://localhost :2222/hello/,会出现文字信息和日志输出。
简单的入门案例就此搭建结束,虽然没实现什么功能,但是我们可以从服务注册中心观察到可用的Eureka Client实例,和在日志中打印服务实例的一些简短信息。
Eureka 在 GitHub 的 wiki 上专门写了一篇 《 Eureka REST operations》 来介绍 Eureka Server 的 REST API 接口,Spring Cloud Netfix Eureka 跟 Spring Boot 适配之后,提供的 REST API 与原始的 REST API 有一点点不同,其路径中的 {version} 值固定为 eureka,其他的变化不大.
以下简单演示apps的REST端点:访问 http://localhost :1111/eureka/apps,会得到以下返结果。
<applications> <versions__delta>1</versions__delta> <apps__hashcode>UP_1_</apps__hashcode> <application> <name>EUREKA-CLIENT</name> <instance> <instanceId>localhost:eureka-client:2222</instanceId> <hostName>localhost</hostName> <app>EUREKA-CLIENT</app> <ipAddr>192.168.91.1</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">2222</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1557113978372</registrationTimestamp> <lastRenewalTimestamp>1557114128293</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1557113978373</serviceUpTimestamp> </leaseInfo> <metadata> <management.port>2222</management.port> </metadata> <homePageUrl>http://localhost:2222/</homePageUrl> <statusPageUrl>http://localhost:2222/actuator/info</statusPageUrl> <healthCheckUrl>http://localhost:2222/actuator/health</healthCheckUrl> <vipAddress>eureka-client</vipAddress> <secureVipAddress>eureka-client</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1557113978373</lastUpdatedTimestamp> <lastDirtyTimestamp>1557113978278</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application> </applications>
文末提供一些,服务发现选型对比,下篇文章介绍Eureka的核心类及其内容。
其中比较受众关注的就是Eureka和Consul这两款产品,这两款产品各有所长,各有所适,开发者可用按需选择。
个人微信公众号,欢迎交流鸭!