Spring 1.5.9微服務註冊
阿新 • • 發佈:2018-12-20
1、新建maven專案microservice-simple-provider-user,由於後續使用JpaRepostory,spring-boot選擇的是1.5.9。
若依賴下載比較慢,建議映象換成國內的。本人是http://maven.aliyun.com/nexus/content/groups/public/<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、配置application.yml
server:
port: 8080
servlet-path: /user-api/*
eureka:
client:
service-url:
defaultZone: http://localhost:8764/eureka/
instance:
prefer-ip-address: true
3、編寫啟動類,在啟動類上增加@EnableEurekaClient註解,宣告為Eureka client。
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
此處
可以用@EnableDiscoveryClient註解,後者對Zookeeper和Consul等也使用。兩者的區別可以參考:
https://blog.csdn.net/u012734441/article/details/78256256?locationNum=1&fps=14、啟動上一篇中的microservice-discovery-eureka專案。(https://blog.csdn.net/m_sophia/article/details/80867514)
5、啟動本專案,執行結果如圖
由圖可以看出:微服務註冊成功了。
原始碼路徑:https://github.com/nieshankun/microservice-simple-provider-user.git