Netflix Eureka是由Netflix開源的一款基於REST的服務治理組件,包括Eureka Server及Eureka Client。因爲種種緣由,Eureka 2.x版本已經凍結開發,目前最新版本是2018年8月份發佈的1.9.4版本。
Spring Cloud Eureka是Pivotal公司爲Netflix Eureka整合於Spring Cloud生態系統提供的版本。
Eureka是Netflix公司提供的開源服務發現組件(現已閉源),最新版本是1.9.4,該組件提供的服務發現能夠爲負載均衡、failover等提供支持。Eureka包括Eureka Server和Eureka Client。Eureka Server提供REST服務,Eureka Clinet多數是使用Java編寫的客戶端(Eureka Client可使用其餘語言編寫,好比Node.js或.NET),用於簡化和Eureka Server的交互。
全部工程使用Spring Cloud的新版Greenwich.SR1和Maven構建。java
pom.xml內容以下:git
<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>
Finchley版本以後,Eureka的depenecy片斷稍微有點不一樣github
<!-- 原版本 --> <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註解。web
@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/出現以下頁面就說明服務端簡單案例構建成功。spring
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/,會出現文字信息和日誌輸出。app
簡單的入門案例就此搭建結束,雖然沒實現什麼功能,可是咱們能夠從服務註冊中心觀察到可用的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,會獲得如下返結果。maven
<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這兩款產品,這兩款產品各有所長,各有所適,開發者可用按需選擇。
我的微信公衆號,歡迎交流鴨!