服務註冊到Eureka,須要提供服務自身的元信息(如:主機,端口等),Eureka經過一個服務從各個實例接受心跳信息。若是心跳接受失敗或超過配置時間,實例會從註冊中心移除。git
@Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @RestController public class Application { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
@EnableEurekaClient 聲明一個註冊實例,@EnableDiscoveryClient能夠將實例在Eureka上生效。github
application.ymlweb
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
defaultZone 將註冊中心鏈接提供給客戶端,能夠理解爲一個心跳url。spring
服務元信息:緩存
監控的目的是當服務提供者不可用時,服務會從註冊中心下掉。當服務可用時可進行註冊提供服務;springboot
服務提供者配置:app
spring.application.name=compute-service server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/ eureka.client.healthcheck.enabled=true //表明開放服務健康檢查功能 eureka.instance.statusPageUrlPath=/info eureka.instance.healthCheckUrlPath=/health
同時須要引用actuator模塊,此模塊在涉及到健康健康,狀態信息都須要引用依賴。spring-boot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
註冊中心配置:fetch
server.port=5000 eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.server.enableSelfPreservation=false //不開放自身緩存,開放的話,會將服務列表在本地緩存 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
Eureka服務端沒有後臺存儲,但註冊的服務實例都須要發送心跳,保持自身在註冊中心可用(可在內存存儲狀態),客戶端一樣保持一份備份,沒必要每次請求都獲取。ui
Eureka自己仍是太新了,在真實項目中服務發現自己是一個很是重要的組件模塊,市面上也少見商用的文檔,建議仍是實用比較傳統的,有更好保證的解決方案。推薦使用Zookeeper。