在使用微服务模块中,我们没办法保证任何服务不会挂掉,在服务一旦挂掉的时候其他服务能够继续工作,保持整个系统的高可用。注册中心提供了服务注册,发现,管理等功能,eureka是Netflix开发的服务发现框架,他能够与springCloud快速集成,使用极少的配置就可以搭建一个可用的注册中心;
能够提供注册中心服务的不只有eureka,springCloud也对其他的服务发现框架提供了良好的支持:
上面是部分服务发现框架的对比,springCloud对于consul,zookeeper都有支持;
一:搭建eureka:
创建一个普通的springboot项目
引入eureka的jar,作为eureka注册中心只需要引入server的jar就可以了
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在启动类里面加入注解:
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
接下来是配置文件:
server:
port: 8010
spring:
application:
name: eureka
eureka:
instance:
hostname: eureka
client:
# 表示是否注册自身到eureka服务器
register-with-eureka: false
# 是否从eureka上获取注册信息
fetch-registry: false
#注册地址
serviceUrl.defaultZone: http://localhost:8010/eureka/
server:
#自我保护
enable-self-preservation: false
#eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
eviction-interval-timer-in-ms: 1000
启动项目,访问localhost:8010
下面是我注册实例,如果你没有注册实例是没有的:
二:注册服务:
注册中心的主要作用就是用来发现服务和管理服务,下面我们创建一个服务客户端,将他注册到注册中心:
创建一个springboot项目
映入客户端的jar:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在启动类上面开启客户端:
@SpringCloudApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
springCloud在注解上面做了简化,所以我们只需要加入这个一个注解就有了三个注解的功能,我们看一下注解里面:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
这个注解本身整合了三个注解,
配置项目的配置文件:
server:
port: 8021
spring:
application:
name: consumer
eureka:
client:
serviceUrl.defaultZone: http://localhost:8010/eureka/
instance:
#实例显示格式,默认按照ip:名称:端口显示,
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
# 发呆时间,即服务续约到期时间(缺省为90s)
lease-expiration-duration-in-seconds: 10
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
启动项目就可以在注册中心看见这个客户端的实例了;
三健康检查
注册中心为每个微服务提供了健康检查,确保每一个微服务是可用的,在客户端加入配置就可以了
引入jar:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在配置中开启健康检查:
server:
port: 8021
spring:
application:
name: consumer
eureka:
client:
healthcheck:
enabled: true
serviceUrl.defaultZone: http://localhost:8010/eureka/
instance:
#实例显示格式,默认按照ip:名称:端口显示,
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
# 发呆时间,即服务续约到期时间(缺省为90s)
lease-expiration-duration-in-seconds: 10
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
开启健康检查的配置就是这么简单。
四eureka的高可用
正常生产中,一台eureka'服务器无法保障服务的可用,需要几台eureka服务器同时工作,当一台挂掉以后,其他的服务器可以快速顶上完全不影响生产中服务的使用。可以使用springboot的多环境配置,只需要增加配置文件,启动的时候指定不同的配置文件就可以了;我这里另外创建了一个项目:一模一样
修改配置:
server:
port: 8010
spring:
application:
name: eureka
eureka:
instance:
hostname: eureka
client:
# 表示是否注册自身到eureka服务器
register-with-eureka: true
# 是否从eureka上获取注册信息
fetch-registry: true
#注册地址
serviceUrl.defaultZone: http://eurekatwo:8011/eureka/
server:
#自我保护
enable-self-preservation: true
#eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
eviction-interval-timer-in-ms: 1000
server:
port: 8011
spring:
application:
name: eurekatwo
eureka:
instance:
hostname: eurekatwo
client:
register-with-eureka: true
fetch-registry: true
serviceUrl.defaultZone: http://eureka:8010/eureka/
server:
enable-self-preservation: true
eviction-interval-timer-in-ms: 1000 #eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
其实就是两个注册中心互相注册,因为我是本地使用所以配置了一个域名:直接在host里面增加配置
127.0.0.1 eureka
127.0.0.1 eurekatwo
分别启动两个项目:
可能因为我的网络原因,另一个注册中心显示不可用。
客户端注册时就直接向两个中心注册就好了:
后面有什么优化自己修改参数就好了。