简介
Netflix Eureka是由Netflix开源的一款基于REST的服务发现组件,包括Eureka Server和Eureka Client。
单从命名就可以看出这个组件的作用。目前流行着很多服务发现组件,如consul、zookeeper等。各有各的特点,Eureka符合CAP理论中AP。
Eureka Server
基本Maven依赖
创建顶级Maven工程,部分依赖如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring Cloud 版本选用目前最新的Finchley.SR2版本,对应SpringBoot 版本为2.0.5.RELEASE。
创建EurekaServer的maven module,添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
创建启动类:
package personal.nathan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Description:
* <p>
* Created by nathan.z on 18-7-4.
*/
@SpringBootApplication
@EnableEurekaServer
public class Eureka {
public static void main(String[] args) {
SpringApplication.run(Eureka.class, args);
}
}
两个关键注解@SpringBootApplication不多说,@EnableEurekaServer 这样就使服务成为了EurekaServer。
添加配置信息:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false # 是否到eureka服務器中抓取註冊信息
server:
wait-time-in-ms-when-sync-empty: 0
enable-self-preservation: false
spring:
application:
name: eureka
启动Eureka,然后在浏览器输入localhost:8761,如果能看到Eureka的管理界面的化,就表明启动成功。
ErekaClient
接下来搭建服务组件.
maven 依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
启动类:
package personal.nathan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Description:
* <p>
* Created by nathan.z on 18-11-18.
*/
@SpringBootApplication
@EnableDiscoveryClient
public class DemoServiceApp {
public static void main(String[] args) {
SpringApplication.run(DemoServiceApp.class, args);
}
}
关键注解@EnableDiscoveryClient,表示这个服务能被Eureka Server发现。
配置信息:
server:
port: 9001
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: demo-service1
defaultZone 指定服务发现组件的地址,spring.application.name指定应用名称,在Eureka管理页面会显示。
启动服务,刷新Eureka的管理页面,可以看到:
Eureka.png
可以从图中看到demoService1已经成功注册到了Eureka Server上。
源码地址:https://github.com/nathanchoui/spring-cloud-study.git, feature/scf分支
总结:基本的服务注册发现就完成了,谢谢,下一篇会讲eureka的rest api。
网友评论