Eureka 注册中心是入门Spring Cloud微服务架构的必学组件,是学习所有其他组件的基础和基石。
微服务的核心思想是分而治之,各司其职,而如何将这些各司其职的微服务实例协同起来,则需服务注册中心的介入。在Spring Cloud微服务体系中,这个第三方协作者的角色由Eureka完成,注意发音为([juˈriːkə])。
一. 添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
二. 启动类中加入@EnableEurekaServer注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
三. application.properties中添加对应的配置
#注册中心名
spring.application.name=eureka-server
#注册中心端口号
server.port=8761
#注册中心ip地址
eureka.instance.hostname=localhost
#是否注册自己,没有必要注册自己
eureka.client.register-with-eureka=false
#是否从eureka服务器获取注册
eureka.client.fetchRegistry=false
#注册中心访问路径
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/