eureka單機搭建
搭建eureka server
創建一個springboot項目
1.pom中添加依賴
<!--端點監控工具依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka server依賴-->
<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>
2.啓動類中添加@EnableEurekaServer註解
在配置文件中配置基本項
server:
port: 9001
eureka:
## 配置實例名稱
instance:
hostname: dev
client:
## 檢索服務 是否從Eureka Server獲取註冊信息,默認爲true
fetch-registry: false
## 是否註冊到eureka上
register-with-eureka: false
## 定義service-url, 默認值爲 http://localhost:8761/eureka/
service-url:
defaultZone: http://localhost:${server.port}/eureka/
server:
## 當eureka啓動時,不能從集羣節點中獲取到instance註冊信息,應該等待的時間
wait-time-in-ms-when-sync-empty: 0
## 是否開啓自我保護機制
enable-self-preservation: true
### eureka多長時間更新一次數據
peer-eureka-nodes-update-interval-ms: 100000
啓動項目,訪問端口:
http://localhost:9001/
可以看到已經啓動完畢
搭建eureka client
1.導入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
<version>2.1.3.RELEASE</version>
</dependency>
啓動類加上註解@EnableEurekaClient
配置文件中配置基本項
server:
port: 9000
spring:
application:
name: eureka-client
eureka:
client:
service-url:
## 註冊地址
defaultZone: http://localhost:9001/eureka/
訪問之前搭建的註冊中心
可以看到,我們的 eureka-client 註冊上去了
在eurekaClient端創建一個api接口,試試能否正常使用
@RestController
public class HelloEurekaDemo {
@Value("${server.port}")
private String port;
@GetMapping("/hello")
public String helloEureka (){
return "hello Eureka ==> port: " + port;
}
}
訪問http://localhost:9000/hello,瀏覽器現實如下信息:
至此單機的 eureka-client搭建完畢,已經可以初步使用了
eureka集羣搭建
1. eureka-server集羣基本配置
spring:
profiles:
active: dev2
---
spring:
application:
###當前服務名
name: eureka-peer
profiles: dev
server:
##端口號
port: 10000
---
spring:
profiles: dev1
application:
name: eureka-peer2
server:
port: 10001
---
spring:
profiles: dev2
application:
name: eureka-peer3
server:
port: 10002
---
eureka:
instance:
hostname: dev1
client:
##是否從Eureka Server獲取註冊信息,默認爲true
fetch-registry: false
##是否將自己註冊到eureka
register-with-eureka: false
##客戶端默認去這個地址找註冊中心
service-url:
defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/
server:
### 當eureka啓動時,不能從集羣節點中獲取到instance註冊信息,應該等多久
wait-time-in-ms-when-sync-empty: 0
enable-self-preservation: true
###eureka多長時間更新一次數據
peer-eureka-nodes-update-interval-ms: 100000
分別啓動三個集羣服務
啓動成功後,訪問3個端口:
http://localhost:10000/
http://localhost:10001/
http://localhost:10002/
都可以正常訪問
2. eureka-client集羣基本配置
只需要將多個註冊的地址按,分隔即可
...
eureka:
client:
service-url:
## 集羣註冊地址 用,號隔開
defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/
訪問api接口,正常
關閉2個server服務
依然能正常訪問
代碼鏈接:
碼雲.