Spring Cloud Eureka 服務註冊與發現

Eureka是Netflix開源的一個RESTful服務,主要用於服務的註冊發現。Eureka由兩個組件組成:Eureka服務器和Eureka客戶端。Eureka服務器用作服務註冊服務器。Eureka客戶端是一個java客戶端,用來簡化與服務器的交互、作爲輪詢負載均衡器,並提供服務的故障切換支持。

Eureka 官方架構圖

  • Eureka Server 提供用於Client的服務註冊與發現
  • Application Service 服務提供方 將自己註冊到Server中
  • Application Client 服務調用方 通過Server發現服務提供方,進行調用

Spring Cloud 中使用

Eureka Server

1.引入依賴

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

或者使用Idea 勾選依賴
這裏寫圖片描述
2.在啓動類上添加註解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class Eurekaserver1Application {

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

3.application.yml

server:
  port: 8761 #服務端口 默認Eureka端口爲8761
eureka:
  client:
    service-url:
      defaultZone: http://local2:8762/eureka
  server:
    enable-self-preservation: false #是否開啓自我保護(在開發環境關閉)
spring:
  application:
    name: es

Eureka Client

1.添加依賴

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


這裏寫圖片描述
2.啓動類添加@EnableEurekaClient或@EnableDiscoveryClient

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

這兩個註解的區別?
3.application.yml

server:
  port: 8081
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      defaultZone: http://local1:8761/eureka,http://local2:8762/eureka
  instance:
    prefer-ip-address: true
spring:
  application:
    name: ep
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章