介绍:注册中心是微服务的重要组成之一,目前常用的有Eureka/Consule/Nacos。
模块一 组件对比:
- Eureka是netflix公司的,也是较早出现作为注册中心的组件,2.X已不再更新维护,最新稳定版为v1.9.12,查看spring-cloud-starter-netflix-eureka-client的Maven依赖,eureka-client和eureka-core版本就是1.9.12。
- Consul组件也是可以作为注册中心来使用的,其版本是跟着SpringCloud版本版本进行维护。
- Nacos组件是阿里巴巴的产品,是与springcloud-alibaba进行的集成,可作为注册中心使用。
模块二 组件使用:
I. Eureka:服务端和客户端都是使用容器启动的。
①服务端
a 引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
b 使用注解注释启动类:
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
c 配置不要自己注册自己配置文件:
server:
port: 9090
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
server:
enableSelfPreservation: false
# serviceUrl:
# defaultZone: http://localhost:9091/eureka/
②客户端
a 引入依赖,注意这里artifactId是【spring-cloud-starter-netflix-eureka-client】,低版本是【spring-cloud-starter-eureka-server】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</a