Eureka是Netflix開源的服務註冊與發現框架,Eureka由兩個組件組成:Eureka服務器和Eureka客戶端。java
Eureka Server提供服務註冊服務,各個節點啓動後,會在Eureka Server中進行註冊,這樣EurekaServer中的服務註冊表中將會存儲全部可用服務節點的信息,服務節點的信息能夠在界面中直觀的看到。git
Eureka客戶端是一個Java客戶端,用來簡化與服務器的交互、做爲輪詢負載均衡器,並提供服務的故障切換支持。github
行爲 | ken.io 的說明 |
---|---|
1. Registry | 服務啓動時,Eureka客戶端會將Service(ServiceName、IP、Port)註冊到Eureka Server |
2. Query | 應用經過Eureka客戶端調用註冊在Eureka Server的服務時,會經過ServiceName查詢到服務實例列表 |
3. Call | 根據拿到的服務實例信息,經過負載均衡或其餘策略訪問指定服務實例 |
框架 | 版本 |
---|---|
Spring Boot | 2.0.0.RELEASE |
Spring Cloud | Finchley.BUILD-SNAPSHOT |
JDK | 1.8.x |
使用maven-archtype-quickstart模板建立項目web
項 | 說明 |
---|---|
GroupId | io.ken.springcloud.eurekaserver |
ArtifactId | eurekaserver |
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>io.ken.springcloud.eurekaserver</groupId> <artifactId>eurekaserver</artifactId> <version>1.0-SNAPSHOT</version> <name>eurekaserver</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <finalName>eurekaserver</finalName> </build> </project>
修改\src\main\java\io\ken\springcloud\eurekaserver\App.java
基於Spring Boot建立啓動類,添加上 @EnableEurekaServer
註解spring
package io.ken.springcloud.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
在\src\main下建立文件夾resources文件夾並設置爲Resources Rootapache
在resources文件夾下建立application.yml文件並配置Eureka Server服務器
server: port: 8800 spring: application: name: EurekaServer eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:${server.port}/eureka/
Eureka默認會以客戶端來註冊本身,因此registerWithEureka、fetchRegistry要配置爲false。app
啓動Eureka Server項目後,訪問:http://localhost:8800/ ,能夠看到下面的頁面,其中尚未發現任何服務。負載均衡
使用maven-archtype-quickstart模板建立項目框架
項 | 說明 |
---|---|
GroupId | io.ken.springcloud.helloservice |
ArtifactId | helloservice |
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>io.ken.springcloud.helloservice</groupId> <artifactId>helloservice</artifactId> <version>1.0-SNAPSHOT</version> <name>helloservice</name> <url>http://ken.io</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <finalName>helloservice</finalName> </build> </project>
修改\src\main\java\io\ken\springcloud\helloservice\App.java
基於Spring Boot建立啓動類,添加上 @EnableDiscoveryClient
註解
package io.ken.springcloud.helloservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
在\src\main\java\io\ken\springcloud\helloservice建立package:controller
而後建立HelloController.java
package io.ken.springcloud.helloservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired DiscoveryClient discoveryClient; @RequestMapping("/") public Object index() { return "hello service"; } @RequestMapping("/info") public Object info() { return discoveryClient.getServices(); } }
在\src\main下建立文件夾resources文件夾並設置爲Resources Root
在resources文件夾下建立application.yml文件並配置Eureka Client
server: port: 8601 spring: application: name: helloservice eureka: client: serviceUrl: defaultZone: http://localhost:8800/eureka/
HelloService項目成功啓動後,訪問 http://localhost:8601 ,或者 http://localhost:8601/info
查看是否正常啓動
訪問 http://localhost:8800 ,會發現服務已註冊
搞定!
https://github.com/ken-io/springcloud-course/tree/master/chapter-01