使用 Spring Cloud 构建微服务之二 Eureka Client

feng qijun
2 min readApr 14, 2018

--

上一片构建了一个Eureka Server,服务注册发现服务, 现在就来注册一个服务,作为服务的提供者。

添加 Eureka 依赖

还是建一个Spring Boot应用,需要添加的依赖有

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

配置 Eureka 属性

需要给应用起个名字,作为自己的唯一 ID 在 Eureka Server上注册自己,并且需要声明 Eureka Server 的地址:

spring.application.name=service-provider
eureka.client.serviceUrl.defaultZone
=http://localhost:8000/eureka
eureka.instance.prefer-ip-address
=true

prefer-ip-address 看自己偏好选择。

同时给 Spring Boot 应用添加 EnableEurekaClient 注解,Spring Boot 应用就会想指定的 server 注册自己。

@EnableEurekaClient
@SpringBootApplication
public class ServiceProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}

在启动 Eureka Server 的同时,启动 Service Provider 应用,应该就可以在 Server 的页面上看到这个服务已经注册上去了。

Demo 地址

--

--

feng qijun

I program, use java, ruby, javascript and other web technoligies. I used to write iPhone apps.