介绍:Eureka,古希腊词语,含义为我找到了,我发现了!相传阿基米德发现福利原理时说出了这个词。
Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
package com.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
spring:
application:
name: eureka-server # cAPP名称,在Eureka注册名称
profiles:
active: peer
eureka:
instance:
hostname: peer # 服务注册中心实例的主机名
client:
register-with-eureka: false # 是否注册自己
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false #关闭自我保护机制
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8760
至此一个Eureka-Server就搭建好了。
另外
说到高可用,就是要保证一个节点挂掉,不会影响整个系统的运行。解决办法就是多部署几个实例,搭建集群,那么一个实例节点挂掉,其他实例仍可提供服务。
spring:
application:
name: eureka-server
profiles:
active: peer1
eureka:
instance:
hostname: peer1 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8762/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #关闭自我保护
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8761
spring:
application:
name: eureka-server
profiles:
active: peer2
eureka:
instance:
hostname: peer2 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #关闭自我保护
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8762
spring:
application:
name: eureka-server
profiles:
active: peer3
eureka:
instance:
hostname: peer3 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8762/eureka
server:
enableSelfPreservation: false #关闭自我保护
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8763
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
package com.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
spring:
application:
name: gateway # 在eureka-server注册名称
eureka:
client:
service-url:
defaultZone: http://localhost:8760/eureka/
# #高可用配置
# defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
server:
port: 8081