日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud--Eureka服务注册与发现 Eureka 集群搭建 详细案例!!!

發布時間:2025/3/19 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud--Eureka服务注册与发现 Eureka 集群搭建 详细案例!!! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringCloud組件--Eureka

        • 完整筆記
    • 一、Eureka基礎知識
      • 1.1、什么是服務治理 ?
      • 1.2、 什么是服務注冊與發現?
      • 1.3、Eureka兩組件:Eureka Server和Eureka Client
    • 二、單機Eureka構建步驟
      • 2.1、IDEA生成eurekaServer端服務注冊中心類似物業公司
      • 2.2、EurekaClient端cloud-provider-payment8001
      • 2.3、EurekaClient端cloud-consumer-order80
    • 三、集群Eureka構建步驟
      • 3.1、Eureka集群原理說明
      • 3.2、EurekaServer集群環境構建步驟
      • 3.3、將支付服務8001微服務發布到上面2臺Eureka集群配置中
      • 3.4、將訂單服務80微服務發布到上面2臺Eureka集群配置中
      • 3.5、測試
      • 3.6、支付服務提供者8001集群環境構建
      • 3.7、負載均衡
      • 3.8測試-2
    • 四、actuator微服務信息完善
      • 4.1、主機名稱:服務名稱修改
      • 4.2、訪問信息有IP信息提示
    • 五、服務發現Discovery
      • 對于注冊進eureka里面的微服務,可以通過服務發現來獲得該服務的信息
    • 六、Eureka自我保護
      • 6.1、故障現象
      • 6.2、導致原因
      • 6.3、怎么禁止自我保護
    • 自言自語

完整筆記

一、Eureka基礎知識

1.1、什么是服務治理 ?

  • Spring Cloud 封裝了 Netflix 公司開發的 Eureka 模塊來實現服務治理

  • 在傳統的rpc遠程調用框架中,管理每個服務與服務之間依賴關系比較復雜,管理比較復雜,所以需要使用服務治理,管理服務于服務之間依賴關系,可以實現服務調用、負載均衡、容錯等,實現服務發現與注冊。

1.2、 什么是服務注冊與發現?

  • Eureka采用了CS的設計架構,Eureka Server 作為服務注冊功能的服務器,它是服務注冊中心。而系統中的其他微服務,使用 Eureka的客戶端連接到 Eureka Server并維持心跳連接。這樣系統的維護人員就可以通過 Eureka Server 來監控系統中各個微服務是否正常運行。
  • 在服務注冊與發現中,有一個注冊中心。當服務器啟動的時候,會把當前自己服務器的信息 比如 服務地址通訊地址等以別名方式注冊到注冊中心上。另一方(消費者|服務提供者),以該別名的方式去注冊中心上獲取到實際的服務通訊地址,然后再實現本地RPC調用RPC遠程調用框架核心設計思想:在于注冊中心,因為使用注冊中心管理每個服務與服務之間的一個依賴關系(服務治理概念)。在任何rpc遠程框架中,都會有一個注冊中心(存放服務地址相關信息(接口地址))

1.3、Eureka兩組件:Eureka Server和Eureka Client

Eureka Server提供服務注冊服務

  • 各個微服務節點通過配置啟動后,會在EurekaServer中進行注冊,這樣EurekaServer中的服務注冊表中將會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀看到。

EurekaClient通過注冊中心進行訪問

  • 是一個Java客戶端,用于簡化Eureka Server的交互,客戶端同時也具備一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啟動后,將會向Eureka Server發送心跳(默認周期為30秒)。如果Eureka Server在多個心跳周期內沒有接收到某個節點的心跳,EurekaServer將會從服務注冊表中把這個服務節點移除(默認90秒)

二、單機Eureka構建步驟

2.1、IDEA生成eurekaServer端服務注冊中心類似物業公司

  • 建Module

    • cloud-eureka-server7001
  • 改POM

    • 現在新版本(當前使用2020.2) <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
  • 寫YML

    ver:port: 7001eureka:instance:hostname: localhost #eureka服務端的實例名稱client:#false表示不向注冊中心注冊自己。register-with-eureka: false#false表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務fetch-registry: falseservice-url:#設置與Eureka Server交互的地址查詢服務和注冊服務都需要依賴這個地址。defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 主啟動

    @SpringBootApplication @EnableEurekaServer // 這個注解就是啟動EurekaServer public class EurekaMain7001 {public static void main(String[] args){SpringApplication.run(EurekaMain7001.class,args);} }
  • 測試

    • http://localhost:7001/

    • 結果頁面

    • No application available 沒有服務被發現 O(∩_∩)O 因為沒有注冊服務進來當然不可能有服務被發現

  • 2.2、EurekaClient端cloud-provider-payment8001

    將注冊進EurekaServer成為服務提供者provider

  • cloud-provider-payment8001

  • 改POM

    • <!--eureka-client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
  • 寫YML

    eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:defaultZone: http://localhost:7001/eureka
  • 主啟動

    @SpringBootApplication @EnableEurekaClient public class PaymentMain8001 {public static void main(String[] args){SpringApplication.run(PaymentMain8001.class,args);} }
  • 測試

  • 先要啟動EurekaServer
  • localhost:7001/
  • 自我保護機制

    • 自我保護機制的工作機制是:如果在15分鐘內超過85%的客戶端節點都沒有正常的心跳,那么Eureka就認為客戶端與注冊中心出現了網絡故障,Eureka Server自動進入自我保護機制。、
  • 2.3、EurekaClient端cloud-consumer-order80

    將注冊進EurekaServer成為服務消費者consumer。 和生產者一樣的修改。

  • cloud-consumer-order80

  • POM

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
  • YML

    eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:defaultZone: http://localhost:7001/eureka
  • 主啟動

    @SpringBootApplication @EnableEurekaClient public class OrderMain80 {public static void main(String[] args){SpringApplication.run(OrderMain80.class,args);} }
  • 測試

    • 先要啟動EurekaServer,7001服務

    • 再要啟動服務提供者provider,8001服務

    • eureka服務器

    • http://localhost/consumer/payment/get/1 看ok不

  • 出bug 的話

    Failed to bind properties under ‘eureka.client.service-url’ to java.util.Map<java.lang.String, java.lang.String>

  • 三、集群Eureka構建步驟

    3.1、Eureka集群原理說明

    問題:微服務RPC遠程服務調用最核心的是什么
    高可用,試想你的注冊中心只有一個only one, 它出故障了那就呵呵( ̄▽ ̄)"了,會導致整個為服務環境不可用,所以

    解決辦法:搭建Eureka注冊中心集群 ,實現負載均衡+故障容錯

    3.2、EurekaServer集群環境構建步驟

  • 參考cloud-eureka-server7001

  • 新建cloud-eureka-server7002

  • 導入的依賴和7001是一樣的。

  • 因為我們要模擬兩臺eureka 如果在本機上模擬, 需要改一下本機的配置映射文件。

    • 找到C:\Windows\System32\drivers\etc路徑下的hosts文件

    • 修改映射配置添加進hosts文件 修改完 生效需要重啟電腦才可以。

      127.0.0.1 eureka7001.com 127.0.0.1 eureka7001.com
  • 更改YML配置 讓兩臺機器相互注冊 相互守望

    7001

    server:port: 7001 eureka:instance:hostname: eureka7001.com #eureka服務端的實例名稱client:register-with-eureka: false #false表示不向注冊中心注冊自己。fetch-registry: false #false表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務service-url:defaultZone: http://eureka7002.com:7002/eureka/

    7002

    server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服務端的實例名稱client:register-with-eureka: false #false表示不向注冊中心注冊自己。fetch-registry: false #false表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務service-url:defaultZone: http://eureka7001.com:7001/eureka/
  • 主啟動

    @SpringBootApplication @EnableEurekaServer public class EurekaMain7002 {public static void main(String[] args){SpringApplication.run(EurekaMain7002.class,args);} }
  • 3.3、將支付服務8001微服務發布到上面2臺Eureka集群配置中

  • 更改yml 配置

    server:port: 8001spring:application:name: cloud-payment-servicedatasource:type: com.alibaba.druid.pool.DruidDataSource # 當前數據源操作類型driver-class-name: org.gjt.mm.mysql.Driver # mysql驅動包url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword: 123456eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:#defaultZone: http://localhost:7001/eurekadefaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版mybatis:mapperLocations: classpath:mapper/*.xmltype-aliases-package: com.atguigu.springcloud.entities # 所有Entity別名類所在包
  • 3.4、將訂單服務80微服務發布到上面2臺Eureka集群配置中

  • 更改yml 配置

    server:port: 80spring:application:name: cloud-order-service eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:#defaultZone: http://localhost:7001/eurekadefaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
  • 3.5、測試

  • 先要啟動EurekaServer,7001/7002服務
  • 再要啟動服務提供者provider,8001
  • 再要啟動消費者,80
  • http://localhost/consumer/payment/get/31
  • 3.6、支付服務提供者8001集群環境構建

  • 參考cloud-provider-payment8001

  • 新建cloud-provider-payment8002

  • 可以直接復制粘貼 ,只需要修改 yaml 文件中的端口號 和 啟動類名字即可

  • 不過了更方便查看到我們調用的是那個微服務 我們將Controller 修改一下

  • 修改8001Controller

    @RestController @Slf4j public class PaymentController {@Value("${server.port}")private String serverPort;@Resourceprivate PaymentService paymentService;@PostMapping(value = "/payment/create")public CommonResult create(@RequestBody Payment payment){int result = paymentService.create(payment);log.info("*****插入操作返回結果:" + result);if(result > 0){return new CommonResult(200,"插入成功,返回結果"+result+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"插入失敗",null);}}@GetMapping(value = "/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){Payment payment = paymentService.getPaymentById(id);log.info("*****查詢結果:{}",payment);if (payment != null) {return new CommonResult(200,"查詢成功"+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"沒有對應記錄,查詢ID: "+id,null);}} }
  • 8002 Controller

    @RestController @Slf4j public class PaymentController {@Value("${server.port}")private String serverPort;@Resourceprivate PaymentService paymentService;@PostMapping(value = "/payment/create")public CommonResult create(@RequestBody Payment payment){int result = paymentService.create(payment);log.info("*****新增結果:" + result);if(result > 0){return new CommonResult(200,"插入成功,返回結果"+result+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"插入失敗",null);}}@GetMapping(value = "/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){Payment payment = paymentService.getPaymentById(id);log.info("*****查詢結果:{}",payment);if (payment != null) {return new CommonResult(200,"查詢成功"+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"沒有對應記錄,查詢ID: "+id,null);}} }
  • 3.7、負載均衡

    1.出現bug 不能把訂單服務地址寫死

    //public static final String PAYMENT_SRV = "http://localhost:8001";// 通過在eureka上注冊過的微服務名稱調用 public static final String PAYMENT_SRV = "http://CLOUD-PAYMENT-SERVICE";

    2.使用@LoadBalanced注解賦予RestTemplate負載均衡的能力

    3.ApplicationContextBean

    /*** @auther zzyy* @create 2020-01-28 16:22*/ @Configuration public class ApplicationContextBean {@Bean@LoadBalanced //使用@LoadBalanced注解賦予RestTemplate負載均衡的能力public RestTemplate getRestTemplate(){return new RestTemplate();} }

    3.8測試-2

  • 先要啟動EurekaServer,7001/7002服務
  • 再要啟動服務提供者provider,8001/8002服務
  • http://localhost/consumer/payment/get/1
  • 結果
  • 負載均衡效果達到
  • 8001/8002端口交替出現
  • Ribbon和Eureka整合后Consumer可以直接調用服務而不用再關心地址和端口號,且該服務還有負載功能了。O(∩_∩)O
  • 四、actuator微服務信息完善

    4.1、主機名稱:服務名稱修改

    • 修改cloud-provider-payment8001

      • eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版#defaultZone: http://localhost:7001/eureka # 單機版instance:instance-id: payment8001
    • 修改之后

    4.2、訪問信息有IP信息提示

  • 當前問題 沒有IP提示

  • 修改cloud-provider-payment8001

    eureka:client:#表示是否將自己注冊進EurekaServer默認為true。register-with-eureka: true#是否從EurekaServer抓取已有的注冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡fetchRegistry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版#defaultZone: http://localhost:7001/eureka # 單機版instance:instance-id: payment8001prefer-ip-address: true #訪問路徑可以顯示IP地址
  • 修改之后

  • 8002 是一樣的操作,大家自己實操啊

    五、服務發現Discovery

    對于注冊進eureka里面的微服務,可以通過服務發現來獲得該服務的信息

  • 修改cloud-provider-payment8001的Controller

  • @RestController @Slf4j public class PaymentController {@Value("${server.port}")private String serverPort;@Resourceprivate PaymentService paymentService;@Resourceprivate DiscoveryClient discoveryClient;@PostMapping(value = "/payment/create")public CommonResult create(@RequestBody Payment payment){int result = paymentService.create(payment);log.info("*****插入操作返回結果:" + result);if(result > 0){return new CommonResult(200,"插入成功,返回結果"+result+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"插入失敗",null);}}@GetMapping(value = "/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){Payment payment = paymentService.getPaymentById(id);log.info("*****查詢結果:{}",payment);if (payment != null) {return new CommonResult(200,"查詢成功"+"\t 服務端口:"+serverPort,payment);}else{return new CommonResult(444,"沒有對應記錄,查詢ID: "+id,null);}}@GetMapping(value = "/payment/discovery")public Object discovery(){List<String> services = discoveryClient.getServices();for (String element : services) {System.out.println(element);}List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");for (ServiceInstance element : instances) {System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"+ element.getUri());}return this.discoveryClient;} }
  • 8001主啟動類

    @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //服務發現 public class PaymentMain8001 {public static void main(String[] args){SpringApplication.run(PaymentMain8001.class,args);} }
  • 自測

  • 先要啟動EurekaServer
  • 再啟動8001主啟動類,需要稍等一會兒
  • http://localhost:8001/payment/discovery
  • 8002 如果想要實現 一樣的操作

    六、Eureka自我保護

    6.1、故障現象

    概述
    保護模式主要用于一組客戶端和Eureka Server之間存在網絡分區場景下的保護。一旦進入保護模式,
    Eureka Server將會嘗試保護其服務注冊表中的信息,不再刪除服務注冊表中的數據,也就是不會注銷任何微服務。

    6.2、導致原因

    • 為什么會產生Eureka自我保護機制?

      為了防止EurekaClient可以正常運行,但是 與 EurekaServer網絡不通情況下,EurekaServer不會立刻將EurekaClient服務剔除

    • 什么是自我保護模式?
      默認情況下,如果EurekaServer在一定時間內沒有接收到某個微服務實例的心跳,EurekaServer將會注銷該實例(默認90秒)。但是當網絡分區故障發生(延時、卡頓、擁擠)時,微服務與EurekaServer之間無法正常通信,以上行為可能變得非常危險了——因為微服務本身其實是健康的,此時本不應該注銷這個微服務。Eureka通過“自我保護模式”來解決這個問題——當EurekaServer節點在短時間內丟失過多客戶端時(可能發生了網絡分區故障),那么這個節點就會進入自我保護模式。

    • 在自我保護模式中,Eureka Server會保護服務注冊表中的信息,不再注銷任何服務實例。
      它的設計哲學就是寧可保留錯誤的服務注冊信息,也不盲目注銷任何可能健康的服務實例。一句話講解:好死不如賴活著

    綜上,自我保護模式是一種應對網絡異常的安全保護措施。它的架構哲學是寧可同時保留所有微服務(健康的微服務和不健康的微服務都會保留)也不盲目注銷任何健康的微服務。使用自我保護模式,可以讓Eureka集群更加的健壯、穩定。

    屬于CAP里面的AP分支

    6.3、怎么禁止自我保護

  • 注冊中心eureakeServer端7001

    • 出廠默認,自我保護機制是開啟的 eureka.server.enable-self-preservation=true

    • 使用eureka.server.enable-self-preservation = false 可以禁用自我保護模式

      server:port: 7001spring:application:name: eureka-cluster-servereureka:instance:hostname: eureka7001.comclient:register-with-eureka: falsefetch-registry: falseservice-url:#defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eurekadefaultZone: http://eureka7001.com:7001/eurekaserver:#關閉自我保護機制,保證不可用服務被及時踢除enable-self-preservation: falseeviction-interval-timer-in-ms: 2000
    • 關閉效果

  • 生產者客戶端eureakeClient端8001

    eureka:client: #服務提供者provider注冊進eureka服務列表內service-url:register-with-eureka: truefetch-registry: true# cluster version#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka# singleton versiondefaultZone: http://eureka7001.com:7001/eureka #心跳檢測與續約時間 #開發時設置小些,保證服務關閉后注冊中心能即使剔除服務instance:#Eureka客戶端向服務端發送心跳的時間間隔,單位為秒(默認是30秒)lease-renewal-interval-in-seconds: 1#Eureka服務端在收到最后一次心跳后等待時間上限,單位為秒(默認是90秒),超時將剔除服務lease-expiration-duration-in-seconds: 2
  • 測試

    • 7001和8001都配置完成

    • 先啟動7001再啟動8001

    • 先關閉8001 我們可以看到立馬就被刪除啦。

  • 自言自語

    繼續干活。

    總結

    以上是生活随笔為你收集整理的SpringCloud--Eureka服务注册与发现 Eureka 集群搭建 详细案例!!!的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。