Eureka 伺服器
配置高可用Eureka伺服器
1.配置公用Eureka 伺服器
application.properties
##定義應用名稱
spring.application.name=spring-cloud-eureka-server
##配置端口
##通過啟動參數覆寫9090端口
#server.port=9090
##取消向注冊中心注冊
eureka.client.register-with-eureka=true
##取消向注冊中心擷取注冊資訊,執行個體資訊
eureka.client.fetch-registry=true
##解決Peer/叢集連接配接問題
#eureka.instance.hostname=localhost
#eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
2.配置Peer 1 Eureka伺服器
application-peer1.properties(單機情況相當于profile="peer1")
##peer1完整配置
##配置端口
#peer1端口9090
server.port=9090
#peer2主機:localhost 端口9091
peer2.server.host=localhost
peer2.server.port=9091
#Eureka注冊資訊
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${peer2.server.host}:${peer2.server.port}/eureka
啟動Peer1 Eureka伺服器
--spring.profiles.active=peer1
,相當于讀取了application-peer1.properties和application.properties
3.配置Peer 2 Eureka伺服器
application-peer2.properties(單機情況相當于profile="peer2")
##peer2完整配置
##配置端口
#peer2端口9090
server.port=9091
#peer1主機:localhost 端口9090
peer1.server.host=localhost
peer1.server.port=9090
##取消向注冊中心注冊
#eureka.client.register-with-eureka=false
##取消向注冊中心擷取注冊資訊,執行個體資訊
#eureka.client.fetch-registry=false
##解決Peer/叢集連接配接問題
#Eureka注冊資訊
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${peer1.server.host}:${peer1.server.port}/eureka
啟動Peer2 Eureka伺服器
--spring.profiles.active=peer2
,相當于讀取了application-peer2.properties和application.properties
效果如圖:
Spring cloud Consul
Consul元件
- 服務發現(Service Discovery)
- 健康檢查(Health Check)
- 鍵值存儲(KV Store)
-
多資料中心(Multi Datacenter)
了解Raft協定:
http://thesecretlivesofdata.com/raft/ -
Agent的啟動
我的作業系統是windows是以下面的步驟是windows下安裝步驟
1.首先下載下傳consul的windows版本
2.解壓到指定檔案夾
我的檔案夾路徑是
D:\consul
3.配置環境變量
在path下添加
4.啟動consul
cmd下輸入
consul agent -dev
5.在浏覽器中輸入
localhost:8500
出現下圖所示則為正确
consul agent -dev
檢視本機資訊
consul members
建立K/V
consul kv put abc 123
擷取K/V
consul kv get abc
-
UI控制台
localhost:8500/ui
Spring Cloud整合Consul
1.引入依賴
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-lesson-consul-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-lesson-consul-client</name>
<description>Demo project for Spring Boot</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-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-consul-discovery</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.激活服務發現用戶端
使用
@EnableDiscoveryClient
注解
package com.example.springcloudlessonconsulclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudLessonConsulClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLessonConsulClientApplication.class, args);
}
}
利用服務發現API操作
配置應用資訊
##應用名稱
spring.application.name=spring-cloud-consul
##服務端口
server.port=8080
##配置連接配接Consul伺服器的配置
#Consul主機位址
spring.cloud.consul.host=localhost
##Consul服務端口
spring.cloud.consul.port=8500