springcloud學習-eureka服務中心
1 新建一個空的工程,然後右鍵工程,進入“New Module”視圖,如下:
2 選擇jdk版本,一般情況默認自己的jdk即可。
3 點擊“Next”,輸入對應的工程名稱(eurekaserver),選擇對應的工程構建方式,這裏選擇“Gradle Project”,如果gradle不是太熟悉,可以自行百度或者參考後續的博文。
4 點擊“Next”,選擇左側的Cloud Discovery,並勾選Eureka Server
5 點擊“Next”,然後Finish即可,這時idea已經幫助你完成一些基本的操作。展開對應的eurekaserver module,找到resources目錄,添加application.yml,用來配置工程,配置如下:
server: port: 8001 sessionTimeout: 15 tomcat: max-threads: 800 uri-encoding: UTF-8 spring: application: name: eurekaServer security: basic: enabled: true user: name: root password: melo eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://root:melo@localhost:8001/eureka/
以上主要配置的爲eureka節點:
對應registerWitheureka 表示是否將本應用註冊到eureka服務中心
對應fetchRegistry 表示是否爲對應的服務提供者(默認是true)
6 對應build.gradle文件如下:
buildscript { ext { springBootVersion = '1.5.10.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } ext { springCloudVersion = 'Edgware.SR1' } dependencies { compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" compile('org.springframework.cloud:spring-cloud-starter-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
7 對應的主程序類代碼如下:
package com.example.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
主要新增的:@EnableEurekaServer註釋,然後正常啓動springboot,在瀏覽器輸入http://localhost:8001
如上圖表示已經成功完成基本eurekaserver的註冊中心功能。
以上是個人學習過程,如有錯誤,歡迎指正,謝謝大家!