一、Eureka簡介
包含兩個組件:
Eureka Server :提供服務發現能力,各個微服務啓動時,會向Eureka Server註冊自己的信息(IP、端口、微服務名稱等),Eureka Server會存儲這些信息。
Eureka Client:是一個java客戶端,可以簡化與服務器的交互、作爲輪詢負載均衡器,能提供服務的故障切換支持。
二、創建服務註冊中心【Eureka Server】
按步驟,直接上圖:
至此,點擊Next,直至 完成,在窗口打開項目,如下:
1.pom.xml
繼承了父pom文件,並引入了spring-cloud-starter-netflix-eureka-server依賴
2.springboot工程的啓動application類:DemoEurekaServerApplication
添加@EnableEurekaServer,啓動一個服務註冊中心,只需要這一個註解。
package com.example.demo_eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
/*啓動一個服務註冊中心,只需要一個註解@EnableEurekaServer*/
@EnableEurekaServer
public class DemoEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaServerApplication.class, args);
}
}
3.application.yml
eureka是一個高可用的組件,沒有後端緩存,每一個實例註冊之後需要向註冊中心發送心跳,因此可以在內存中完成,在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。
server:
port: 8763
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server。
4.至此,一個eureka server 搭建完成,啓動工程後,打開瀏覽器訪問:
http://localhost:8763/ ,界面如下:
No application available 沒有服務被發現 ……原因:還沒有註冊服務。
三、創建一個服務的提供者【Eureka Client】
按步驟:直接上圖
至此,點擊Next,直至 完成,在窗口打開項目,如下圖:
client向server註冊時,它會提供一些元數據,如主機和端口,URL,主頁等。
Eureka server 從每個client實例接收心跳消息。 如果心跳超時,則通常將該實例從註冊server中刪除。
1.pom.xml
繼承了父pom文件,並引入了spring-cloud-starter-netflix-eureka-server依賴。
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_eureka_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_eureka_client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</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.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>
</project>
2.springboot工程的啓動application類:DemoEurekaClientApplication
添加註解@EnableEurekaClient ,表明自己是一個Eureka Client。
package com.example.demo_eureka_client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
/*通過註解@EnableEurekaClient 表明自己是一個eurekaclient*/
@EnableEurekaClient
@RestController
public class DemoEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "zyy") String name) {
return "hello " + name + " ,I am from port:" + port;
}
}
3.application.yml
除了添加@EnableEurekaClient,還需在配置文件中註明自己的服務註冊中心的地址。
特別說明:spring.application.name,很重要,在以後,服務與服務之間 相互調用一般都是根據這個name 。
server:
port: 8762
spring:
application:
name: eureka-client-zyy-demo
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8763/eureka/
4.至此,一個Eureka Client搭建完成,啓動工程後,打開瀏覽器訪問Eureka Server 的網址:
http://localhost:8763/ ,界面如下:
會發現有一個服務已經註冊在服務中了,服務名爲eureka-client-zyy-demo ,端口爲8762。
再打開 http://localhost:8762/hi?name=zyy ,會在瀏覽器上看到 :