單機模擬配置Eureka叢集
阿新 • • 發佈:2021-02-02
# 首先先提醒單機部署的重要點
> 1. 如果使用一個ip地址(適用於單網絡卡)每個eureka例項使用不同的域名對映到同一個IP
> 2. 如果每個eureka例項使用不同的IP(多網絡卡),要確保這些IP要都表示本地
本文假設使用第一種情況
## 1. 設定本機host檔案以便模擬對映域名
> sudo /etc/hosts
新增如下行
> 127.0.0.1 registry01
> 127.0.0.1 registry02
可以輸入
> ping registry01
> ping registry02
來驗證是否解析到 127.0.0.1
## 2. 建立Eureka叢集成員伺服器registry01
idea嚮導如何建立eureka server就不詳細寫了
我直接扔配置
pom.xml
```xml
```
主程式
```java
package com.linkanyway.registry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author linkanyway
*/
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run (RegistryApplication.class, args);
}
}
```
application.yml
```yml
server:
port: 8080
spring:
application:
name: EurekaCluster # 所有叢集內都採用一樣Name
eureka:
instance:
hostname: registry01
prefer-ip-address: true # 採用ip地址
ip-address: 127.0.0.1
client:
register-with-eureka: true # 控制當Spring Boot啟動服務完成後是否將該服務註冊到服務治理伺服器上。這裡因為服務本身就是服務治理伺服器且有叢集因此為true,如果未構建任何服務治理叢集,需將其設定為false,表示不註冊
fetch-registry: true # eureka.client.fetch-registry屬性設定表示應用啟動後是否需要從服務治理伺服器中同步已註冊的服務註冊列表資料到本地 本處叢集伺服器成員則需要註冊此處為true
service-url:
defaultZone: http://registry02:8081/eureka # 叢集需要配置相互複製
server:
wait-time-in-ms-when-sync-empty: 5
```
## 3. 建立Eureka叢集伺服器成員registry02
pom檔案
```xml
```
主程式
```java
package com.linkanyway.registry02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Registry02Application {
public static void main(String[] args) {
SpringApplication.run (Registry02Application.class, args);
}
}
```
application.yml
```yml
server:
port: 8081
spring:
application:
name: EurekaCluster # 所有叢集內都一樣name
eureka:
instance:
hostname: registry02
prefer-ip-address: true # 採用ip地址
ip-address: 127.0.0.1
client:
register-with-eureka: true # 控制當Spring Boot啟動服務完成後是否將該服務註冊到服務治理伺服器上。這裡因為服務本身就是服務治理伺服器且有叢集因此為true,如果未構建任何服務治理叢集,需將其設定為false,表示不註冊
fetch-registry: true # eureka.client.fetch-registry屬性設定表示應用啟動後是否需要從服務治理伺服器中同步已註冊的服務註冊列表資料到本地 本處叢集伺服器成員則需要註冊此處為true
service-url:
defaultZone: http://registry01:8080/eureka # 叢集需要配置相互複製
server:
wait-time-in-ms-when-sync-empty: 5
```
## 3 分別啟動registry01,registry02
因為eureka同步需要時間,稍後會就可以看到下圖的效果
![](https://img2020.cnblogs.com/blog/285590/202102/285590-20210201233429132-41853987.png)
## 4 模擬建立一個服務和消費服務
### service01 服務
pom.xml
```xml
```
主程式
```java
package com.linkanyway.service01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author linkanyway
*/
@EnableDiscoveryClient
@SpringBootApplication
public class Service01Application {
public static void main(String[] args) {
SpringApplication.run (Service01Application.class, args);
}
}
```
application.yml
```yml
server:
port: 8082
eureka:
client:
fetch-registry: true
register-with-eureka: true
serviceUrl:
defaultZone: http://127.0.0.1:8080/eureka/
instance:
prefer-ip-address: true #強烈推薦在專案中使用IP地址而不是主機名稱來註冊微服務。
lease-expiration-duration-in-seconds: 90
lease-renewal-interval-in-seconds: 30
ip-address: 127.0.0.1 # 因此,強烈推薦在專案中使用IP地址而不是主機名稱來註冊微服務。
spring:
application:
name: service01
```
編寫服務(此處簡略的只是做一個控制器)
```java
package com.linkanyway.service01.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author linkanyway
* @version 1.0
* @name ServiceController
* @description TODO
* @date 2021/02/01 20:21
*/
@RestController
@RequestMapping("service")
public class ServiceController {
@GetMapping("get")
public String get() {
return "Service01";
}
}
```
### service02 消費服務
pom.xml
```xml
```
application.yml配置檔案
```yml
server:
port: 8083
eureka:
client:
fetch-registry: true
register-with-eureka: true
serviceUrl:
defaultZone: http://127.0.0.1:8081/eureka/
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
spring:
application:
name: service02
feign:
client:
default-config:
connectTimeout: 10000
readTimeout: 10000
```
主程式
```java
package com.linkanyway.service02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author linkanyway
*/
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class Service02Application {
public static void main(String[] args) {
SpringApplication.run (Service02Application.class, args);
}
}
```
服務呼叫的feign定義
```java
package com.linkanyway.service02.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author linkanyway
* @version 1.0
* @name Service01
* @description TODO
* @date 2021/02/01 20:31
*/
@FeignClient("service01")
public interface Service01 {
/**
* get
* @return
*/
@RequestMapping("service/get")
String get();
}
```
控制器程式碼
```java
package com.linkanyway.service02.controller;
import com.linkanyway.service02.feign.Service01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author linkanyway
* @version 1.0
* @name ServiceController
* @description TODO
* @date 2021/02/01 20:34
*/
@RestController
@RequestMapping("service")
public class ServiceController {
final
Service01 service01;
public ServiceController(Service01 service01) {
this.service01 = service01;
}
@RequestMapping("get")
public String get()
{
return service01.get ();
}
}
```
啟動service01,02
可以看到成功註冊到註冊中心
![](https://img2020.cnblogs.com/blog/285590/202102/285590-20210201234052097-19240768