加上 spring-cloud-starter-eureka 依賴
gradle build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
記得更新依賴後在起動類別加上 @EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationServiceApplication {
//起動的時候預先塞測試資料
@Bean
CommandLineRunner runner(ReservationRepository rr){
return args -> {
Arrays.asList("Dr. rod,Dr. Syer,Juergen,ALL THE COMMUNITY,Josh".split(","))
.forEach( x -> rr.save(new Reservation(x)));;
rr.findAll().forEach( System.out::println);
};
}
public static void main(String[] args) {
SpringApplication.run(ReservationServiceApplication.class, args);
}
}
起動後再回去 http://localhost:8761/ 觀察,就可以發現在 DS Replicas Instances currently registered with Eureka 列表中多了一個 RESERVATION-SERVICE 的應用名稱