iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 18
0
Software Development

30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構系列 第 18

Day 18 - 服務發現客戶端 Eureka Discovery

新增自動發現的客戶端

加上 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 的應用名稱


上一篇
Day 17 - 服務發現中心 Eureka Server
下一篇
Day 19 - 線上服務組態變更 Cloud Bootstrap & Cloud Bus AMQP
系列文
30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言