詳解spring cloud eureka注冊中心
注冊中心呢 就是springcloud的一個核心組件 所有微服務(wù)的基石 微服務(wù)的核心思想就是分布式 所有的服務(wù)分開管理 但這些服務(wù)分開后該如何協(xié)同呢 就需要注冊中心的介入
怎么使用注冊中心
首先在gradle引入它的依賴
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
這里再講一下 springcloud會分布很多模塊 很難管理 所以在整個項目的build.gradle中可以對所有模塊的build.gradle進(jìn)行管理
//插件 apply plugin: 'java' apply plugin: 'spring-boot' //jdk版本 sourceCompatibility = 1.8 dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' //項目版本 } //在編譯構(gòu)建時的配置 自動維護(hù)版本號 buildscript { ext{ springBootVersion='1.5.10.RELEASE' //ext中可以定義變量 里面寫的是springboot插件的版本 } repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} jcenter() mavenCentral() maven{ url "http://repo.spring.io/snapshot" } maven{ url "http://repo.spring.io/milestone" } maven{ url "http://repo.spring.io/release" } maven{ url 'http://repo.spring.io/plugins-snapshot' } } dependencies{ classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } //統(tǒng)一所有項目的配置 就是對所有的模塊進(jìn)行統(tǒng)一配置 所有以后的模塊都不用再配置 allprojects { group 'com.indi.wk' //分組 version '1.0-SNAPSHOT' //版本號 ext{ springCloudVersion='Edgware.SR2' } repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} jcenter() mavenCentral() maven{ url "http://repo.spring.io/snapshot" } maven{ url "http://repo.spring.io/milestone" } maven{ url "http://repo.spring.io/release" } maven{ url 'http://repo.spring.io/plugins-snapshot' } } } //統(tǒng)一所有子項目的配置 subprojects { apply plugin: 'java' apply plugin: 'idea' apply plugin: 'spring-boot' dependencies { compile('org.springframework.boot:spring-boot-starter-web'){ //移除tomcat 因為springboot嫌tomcat運(yùn)行慢 就使用undertow來代替 exclude module:"spring-boot-starter-tomcat" } //替代tomcat compile 'org.springframework.boot:spring-boot-starter-undertow' //健康檢查 compile 'org.springframework.boot:spring-boot-starter-actuator' dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' } } //版本控制插件 dependencyManagement{ imports{ mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } }
看下配置文件
server: port: 8888 #端口號 spring: application: name: register-center #項目名 eureka: client: register-with-eureka: false #啟動時不向注冊中心注冊自己 意思也就是證明自己是一個注冊中心 fetch-registry: false #啟動時是否檢索服務(wù) 不檢索 含義同上
還有一個
eureka.server.enable-self-preservation:
是否開啟自我保護(hù)模式,默認(rèn)為true 會在下一篇博客詳細(xì)說明[/code]
需要在啟動類上加上兩個注解
@SpringBootApplication //啟動項目 @EnableEurekaServer // 定義自己是一個注冊中心 public class RegisterCenterProvider { public static void main(String[] args) { SpringApplication.run(RegisterCenterProvider.class,args); } }
這時候就可以嘗試將一個服務(wù)加入注冊中心
先建一個新模塊
一、加入依賴
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
二、配置文件 幾乎一樣 唯一不同是加入如下兩點(diǎn)
eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ #注冊進(jìn)那個注冊中心 注冊中心的地址 instance: prefer-ip-address: true #是否用ip地址注冊進(jìn)注冊中心
三、在啟動類上加入注解
@EnableDiscoveryClient
之后啟動注冊中心的啟動類 再啟動服務(wù)端的啟動類 看看什么效果
一定要先啟動注冊中心 再啟動服務(wù)端 否則服務(wù)端找不到可以注冊的注冊中心就會報錯
這樣就是已經(jīng)注冊成功
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對Mybatis?Plus中@TableField的使用正解
這篇文章主要介紹了對Mybatis?Plus中@TableField的使用正解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01在springboot中如何使用filter設(shè)置要排除的URL
這篇文章主要介紹了在springboot中如何使用filter設(shè)置要排除的URL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java?通過手寫分布式雪花SnowFlake生成ID方法詳解
SnowFlake是twitter公司內(nèi)部分布式項目采用的ID生成算法,開源后廣受國內(nèi)大廠的好評。由這種算法生成的ID,我們就叫做SnowFlakeID,下面我們來詳細(xì)看看2022-04-04通過spring注解開發(fā),簡單測試單例和多例區(qū)別
這篇文章主要介紹了通過spring注解開發(fā),簡單測試單例和多例區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java實現(xiàn)添加,讀取和刪除Excel圖片的方法詳解
本文介紹在Java程序中如何添加圖片到excel表格,以及如何讀取、刪除excel表格中已有的圖片。文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-05-05