注冊中心Eureka

1、注冊中心的意義

注冊中心管理服務的注冊、發(fā)現(xiàn)、熔斷、負載、降級等。

2、Eureka的組成

Eureka由兩個組件組成:Eureka服務器和Eureka客戶端。

  • Eureka服務器用作服務注冊服務器。
  • Eureka客戶端是一個java客戶端,用來簡化與服務器的交互、作為輪詢負載均衡器,并提供服務的故障切換支持

上圖簡要描述了Eureka的基本架構(gòu),由3個角色組成:

(1)Eureka Server:提供服務注冊和發(fā)現(xiàn)
(2)Service Provider:服務提供方
將自身服務注冊到Eureka,從而使服務消費方能夠找到
(3)Service Consumer:服務消費方
從Eureka獲取注冊服務列表,從而能夠消費服務

3、項目實例

(1)pom文件

<?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 https://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.springcloud</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</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.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>
    </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)yml文件

server:
  port: 8761
spring:
  application:
    name: eureka-serve
eureka:
  server:
    enable-self-preservation: false #關閉eureka的自我保護,防止已被關停的節(jié)點也錯誤的顯示在線
  client:
    register-with-eureka: false #是否允許客戶端向Eureka獲取注冊表信息,單機模式服務器端設置為false
    fetch-registry: false #是否允許向Eureka Server注冊信息,單機模式服務器端設置為false
    service-url:
      defaultZone: http://localhost:8761/eureka/ #此eureka server的應用注冊地址

(3)啟動類

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }

}

啟動后訪問localhost:8761

集群

(1)修改host

127.0.0.1 peer1 peer2 peer3

(2)從application.yml復制出application-peer1.yml,application-peer2.yml,application-peer3.yml三個文件,內(nèi)容如下

# application-peer1.yml
server:
  port: 8761
spring:
  application:
    name: eureka-serve
eureka:
  instance:
    hostname: peer1
  server:
    enable-self-preservation: false #關閉eureka的自我保護,防止已被關停的節(jié)點也錯誤的顯示在線
  client:
    register-with-eureka: true #是否允許客戶端向Eureka獲取注冊表信息,集群模式服務器端設置為true
    fetch-registry: true #是否允許向Eureka Server注冊信息 ,集群模式服務器端設置為true
    service-url:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/ #此eureka server的應用注冊地址
# application-peer2.yml
server:
  port: 8762
spring:
  application:
    name: eureka-serve
eureka:
  instance:
    hostname: peer2
  server:
    enable-self-preservation: false #關閉eureka的自我保護,防止已被關停的節(jié)點也錯誤的顯示在線
  client:
    register-with-eureka: true #是否允許客戶端向Eureka獲取注冊表信息,集群模式服務器端設置為true
    fetch-registry: true #是否允許向Eureka Server注冊信息 ,集群模式服務器端設置為true
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/ #此eureka server的應用注冊地址
# application-peer3.yml
server:
  port: 8763
spring:
  application:
    name: eureka-serve
eureka:
  instance:
    hostname: peer3
  server:
    enable-self-preservation: false #關閉eureka的自我保護,防止已被關停的節(jié)點也錯誤的顯示在線
  client:
    register-with-eureka: true #是否允許客戶端向Eureka獲取注冊表信息,集群模式服務器端設置為true
    fetch-registry: true #是否允許向Eureka Server注冊信息 ,集群模式服務器端設置為true
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/ #此eureka server的應用注冊地址

(3)打包啟動

# 打包
mvn clean package  -Dmaven.test.skip=true

# 分別以 peer1,peer2,peer3配置信息啟動 Eureka
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar  --spring.profiles.active=peer1
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar  --spring.profiles.active=peer2
java -jar target/eureka-server-0.0.1-SNAPSHOT.jar  --spring.profiles.active=peer3

在啟動peer1的時候,控制臺會有一些異常信息,大概就是拒絕連接、請求超時,不用管,等啟動peer2,peer3的時候就好了。

依次啟動完成后,訪問http://localhost:8761/,http://localhost:8762/,http://localhost:8763/

根據(jù)圖可以看出 peer1 的注冊中心 DS Replicas 已經(jīng)有了 peer2,peer3 的相關配置信息,并且出現(xiàn)在 available-replicas 中。我們手動停止 peer3 來觀察,發(fā)現(xiàn) peer3 就會移動到 unavailable-replicas 一欄中,表示 peer3 不可用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容