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