在Spring Cloud中,创建一个Eureka非常简单。只需要以下几步:
1. 首先添加Eureka Server依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
2. 编写启动类,使用@EnableEurekaServer声明一个Eureka Server
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3. 编写配置文件
eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
每个Eureka Server同时也是一个Eureka Client。registerWithEureka、fetchRegistry表示要不要将自己注册到Eureka Server中,因为目前是单机模式,所以配置false
代码地址:
http://git.oschina.net/itmuch/microservice-spring-cloud-teach/tree/master/microservice-discovery-eureka
Spring Cloud视频教程:http://edu.51cto.com/course/course_id-7348.html
Spring Cloud Docker交流QQ群: 157525002