溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringCloud中eureka組件如何使用

發(fā)布時(shí)間:2021-08-12 10:57:50 來源:億速云 閱讀:120 作者:Leah 欄目:大數(shù)據(jù)

SpringCloud中eureka組件如何使用,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

Eureka 是 Netflix 開發(fā)的,一個(gè)基于 REST 服務(wù)的,服務(wù)注冊與發(fā)現(xiàn)的組件

它主要包括兩個(gè)組件:Eureka Server 和 Eureka Client

  • Eureka Client:一個(gè)Java客戶端,用于簡化與 Eureka Server 的交互(通常就是微服務(wù)中的客戶端和服務(wù)端)

  • Eureka Server:提供服務(wù)注冊和發(fā)現(xiàn)的能力(通常就是微服務(wù)中的注冊中心)

工作結(jié)構(gòu)圖:

SpringCloud中eureka組件如何使用 

各個(gè)微服務(wù)啟動時(shí),會通過 Eureka Client 向 Eureka Server 注冊自己,Eureka Server 會存儲該服務(wù)的信息

也就是說,每個(gè)微服務(wù)的客戶端和服務(wù)端,都會注冊到 Eureka Server,這就衍生出了微服務(wù)相互識別的話題

  • 同步:每個(gè) Eureka Server 同時(shí)也是 Eureka Client(邏輯上的)
       多個(gè) Eureka Server 之間通過復(fù)制的方式完成服務(wù)注冊表的同步,形成 Eureka 的高可用

  • 識別:Eureka Client 會緩存 Eureka Server 中的信息
       即使所有 Eureka Server 節(jié)點(diǎn)都宕掉,服務(wù)消費(fèi)者仍可使用緩存中的信息找到服務(wù)提供者(筆者已親測)

  • 續(xù)約:微服務(wù)會周期性(默認(rèn)30s)地向 Eureka Server 發(fā)送心跳以Renew(續(xù)約)信息(類似于heartbeat)

  • 續(xù)期:Eureka Server 會定期(默認(rèn)60s)執(zhí)行一次失效服務(wù)檢測功能
       它會檢查超過一定時(shí)間(默認(rèn)90s)沒有Renew的微服務(wù),發(fā)現(xiàn)則會注銷該微服務(wù)節(jié)點(diǎn)

Spring Cloud 已經(jīng)把 Eureka 集成在其子項(xiàng)目 Spring Cloud Netflix 里面

Eureka Server在springCloud集成

1.pom文件配置

<project xmlns="http://maven.apache.org/POM/4.0.0">

<dependencies>
        <dependency><!--eureka-server注冊與發(fā)現(xiàn)依賴-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency><!--eureka-server安全管理依賴,例如用戶名,密碼登錄-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

2 yml配置文件

security:
  basic:
    enabled: true
  user:
    name: user
    password: password123
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka

3.啟動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

    SpringCloud 集成Eureka client 

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<artifactId>microservice-provider-user</artifactId>
	<packaging>jar</packaging>

	<name>microservice-provider-user</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.itmuch.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h3database</groupId>
			<artifactId>h3</artifactId>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>

</project>

       <dependency><!--客戶端依賴-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency><!--運(yùn)維監(jiān)控瀏覽器查看依賴-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.yml配置文件

server:
  port: 7900
spring:
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    platform: h3
    schema: classpath:schema.sql
    data: classpath:data.sql
  application:
    name: microservice-provider-user
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.itmuch: DEBUG
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    metadata-map:
      zone: ABC      # eureka可以理解的元數(shù)據(jù)
      lilizhou: BBC  # 不會影響客戶端行為
    lease-renewal-interval-in-seconds: 5

3.啟動客戶端類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {

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

看完上述內(nèi)容,你們掌握SpringCloud中eureka組件如何使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI