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