在微服务架构中,注册中心是核心的基础服务。
它主要记录各个微服务和微服务地址的映射关系,各个微服务都将自己注册到这个注册中心上面,当微服务之间需要互相调用时,就可以从注册中心上面去发现微服务和进行调用。
Spring Cloud是一个开箱即用的微服务框架,秉承了微服务的真正理念而设计。
Spring Cloud中,谁是服务的注册中心 ? Eureka Server即服务的注册中心,核心配置如下
1. 加入依赖,pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
2. 程序入口类添加@EnableEurekaServer,启用Eureka Server。例如:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
}
}
3. 单机版Eureka Server,配置application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost # 服务注册中心实例的主机名
lease-renewal-interval-in-seconds: 30 # 客户端向Eureka发送心跳周期(s)
lease-expiration-duration-in-seconds: 90 # Eureka Server接收实例的最后一次发出的心跳后,删除需要等待时间(s)
server:
enable-self-preservation: true # Eureka自我保护模式
client:
register-with-eureka: false # 是否向服务注册中心注册自己
fetch-registry: false # 是否检索发现服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 指定服务注册中心的位置
4. Eureka Server集群配置(HA,高可用),实际上就是将自己作为服务向其他服务注册中心注册自已,这样就可以形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。多个Eureka Server的实例互相注册。配置application.yml
Eureka Server 实例1 配置
server:
port: 8761
eureka:
instance:
hostname: localhost # 服务注册中心实例的主机名
server:
enable-self-preservation: true # Eureka自我保护模式
client:
register-with-eureka: true # 是否注册到Eureka Server
fetch-registry: true # 是否检索发现服务
service-url:
defaultZone: http://${eureka.instance.hostname}:8762/eureka/,http://${eureka.instance.hostname}:8763/eureka/
#指定多个服务注册中心的位置,并向服务注册中心注册自己
Eureka Server 实例2 配置
server:
port: 8762
eureka:
instance:
hostname: localhost # 服务注册中心实例的主机名
server:
enable-self-preservation: true # Eureka自我保护模式
client:
register-with-eureka: true # 是否注册到Eureka Server
fetch-registry: true # 是否检索发现服务
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/,http://${eureka.instance.hostname}:8763/eureka/
#指定多个服务注册中心的位置,并向服务注册中心注册自己
== 谁注册到Eureka Server ? Eureka Client 即注册的服务,基本配置如下==
1. 加入依赖,pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. 主类上面加入@EnableDiscoveryClient ,开启该注解使该服务能够被注册中心发现,例如:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
}
}
3. 客户端的最基本的配置,application.yml ,例如:
server:
port: 8090
spring:
application:
name: spring-cloud-eureka-client-application
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4. 启动Eureka Server,访问http://localhost:8761 可以进入到Eureka Server的查看页面 :