Spring Cloud Eureka为服务发现组件。
1.创建eureka-server项目
1.1 修改pom文件,添加添加依赖:
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
1.2 启动类添加注解@EnableEurekaServer
详解代码如下:
@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
1.3 修改application文件
server: port: 8761eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone : http://${eureka.instance.hostname}:${server.port}/eureka/ server: wait-time-in-ms-when-sync-empty: 0 enable-self-preservation: false
2 创建eureka-client项目
2.1 修改pom文件,添加依赖
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
2.2 修改启动类,添加注解 @EnableDiscoveryClient
详细代码如下:
@SpringBootApplication@EnableDiscoveryClientpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}
2.3 修改application文件
server: port: 8081spring: application: name: demo-client-1eureka: client: service-url: defaultZone : http://localhost:8761/eureka/
分别依次启动eureka-server 和 eureka-client项目。然后访问http://localhost:8761/ ,会发现DEMO-CLIENT-1已经注册到了eureka-server上,状态为UP。