詳解spring-cloud與netflixEureka整合(注冊(cè)中心)
基礎(chǔ)依賴(lài)
compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter')
eureka(單機(jī))
服務(wù)端:
依賴(lài)
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
application.yml 配置
spring: application: name: dev eureka: client: service-url: defaultZone: http://本機(jī)ip地址:8761/eureka/ #注冊(cè)中心地址 register-with-eureka: false #表明該實(shí)例是否應(yīng)該與尤里卡服務(wù)器登記其信息被別人發(fā)現(xiàn)。在某些情況下,您不希望您的實(shí)例被發(fā)現(xiàn)而你想發(fā)現(xiàn)其他實(shí)例。默認(rèn)為true fetch-registry: false #表明這個(gè)客戶(hù)是否應(yīng)該從尤里卡服務(wù)器獲取尤里卡注冊(cè)表信息。默認(rèn)為true server: port: 8761
啟動(dòng)類(lèi)添加 @EnableEurekaServer
客戶(hù)端:
主要依賴(lài)
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
application.yml 配置
eureka: client: service-url: defaultZone: http://eureka服務(wù)地址:8761/eureka/
啟動(dòng)類(lèi)添加 @EnableDiscoveryClient (注冊(cè)中心通用客戶(hù)端注解)或 @EnableEurekaClient (只有eureka客戶(hù)端可用)
eureka(集群)
服務(wù)端
主要依賴(lài)
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
application.yml 配置
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false server: enable-self-preservation: false #使用自我保護(hù),默認(rèn)true peer-node-read-timeout-ms: 5000 #節(jié)點(diǎn)讀取超時(shí)時(shí)間,默認(rèn)200毫秒 --- spring: application: name: eureka1 profiles: eureka1 eureka: client: service-url: defaultZone: http://eureka2:8761/eureka/,http://eureka3:8761/eureka/ instance: hostname: eureka1 --- spring: application: name: eureka2 profiles: eureka2 eureka: client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka3:8761/eureka/ instance: hostname: eureka2 --- spring: application: name: eureka3 profiles: eureka3 eureka: client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/ instance: hostname: eureka3
eureka.client.service-url.defaultZone 與 eureka.instance.hostsname eureka1、eureka2、eureka3 都再本機(jī)的 hosts 文件里事先寫(xiě)好,
例如 eureka1 服務(wù)器上的hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 eureka1
xxx.xxx.x.xx eureka2
xx.xxx.xxx.xx eureka3
啟動(dòng)類(lèi)同樣是添加 @EnableEurekaServer 注意:如果運(yùn)行 jar 包時(shí)需要程序接收命令行參數(shù),一定要再main方法的 SpringApplication.run() 方法 中傳入 args 參數(shù)
例如:
public static void main(String[] args) { SpringApplication.run(QnbbsConsumer.class,args); }
如果不傳入的話(huà)在命令行輸入 java -jar xxx.jar --參數(shù)名=參數(shù)值 參數(shù)將不起作用從而影響你無(wú)法選擇寫(xiě)好的環(huán)境配置
客戶(hù)端:
依賴(lài)跟單機(jī)的一樣
application.yml 配置
eureka: instance: prefer-ip-address: true #是否顯示ip ip-address: 本機(jī)ip #填寫(xiě)本機(jī)ip地址 client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
啟動(dòng)類(lèi)注解與單機(jī)版一樣
客戶(hù)端注冊(cè)一臺(tái)注冊(cè)中心,同時(shí)注冊(cè)到其他集群中說(shuō)明成功!
eureka添加 spring-security 權(quán)限認(rèn)證
依賴(lài)
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
application.yml 配置
server: port: 8761 spring: application: name: eureka-server security: user: name: zyn password: zyn123... #用戶(hù)名與密碼若是不配置,系統(tǒng)會(huì)自動(dòng)生成并打印在控制臺(tái)日志上 eureka: client: register-with-eureka: false fetch-registry: false server: enable-self-preservation: false peer-node-read-timeout-ms: 5000 management: endpoints: web: base-path: / exposure: include: '*' endpoint: health: show-details: always restart: enabled: true server: port: 8761 --- spring: profiles: eureka-jy eureka: client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-B:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-C:8761/eureka/ instance: hostname: eureka-A --- spring: profiles: eureka-lhn eureka: client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-A:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-C:8761/eureka/ instance: hostname: eureka-B --- spring: profiles: eureka-zsm eureka: client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-A:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-B:8761/eureka/ instance: hostname: eureka-C
創(chuàng)建一個(gè)類(lèi)并繼承 WebSecurityConfigurerAdapter 類(lèi),并在此類(lèi)上添加 @EnableWebSecurity和@Configuration 注解,重寫(xiě) configure 方法
package com.iteng.eureka.security; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); // 關(guān)閉csrf http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 開(kāi)啟認(rèn)證 } }
啟動(dòng)類(lèi)添加注解
@EnableEurekaServer @EnableWebSecurity
下線(xiàn)服務(wù)
http://eureka地址:端口/eureka/apps/服務(wù)名/服務(wù)地址:服務(wù)端口號(hào)
實(shí)例如下:
erueka注冊(cè)中心ip: 10.100.1.100
端口: 12000
服務(wù)名: CPS-RISK-SERVER
實(shí)例id: 192.168.10.54:18883
則向下面的url通過(guò)http發(fā)送delete命令,可將指定的服務(wù)實(shí)例刪除:
http://10.100.1.100:12000/eureka/apps/CPS-RISK-SERVER/192.168.10.54:18883
變更服務(wù)狀態(tài)
http://eureka地址:端口/eureka/apps/服務(wù)名/服務(wù)地址/status?value=${value}
其中${value}的取值為:OUT_OF_SERVICE , DOWN , UP
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring?Cloud?Eureka服務(wù)注冊(cè)中心入門(mén)流程分析
- springcloud client指定注冊(cè)到eureka的ip與端口號(hào)方式
- springCloud服務(wù)注冊(cè)Eureka實(shí)現(xiàn)過(guò)程圖解
- springboot2.0和springcloud Finchley版項(xiàng)目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix)
- Spring-cloud-eureka使用feign調(diào)用服務(wù)接口
- Spring Eureka 未授權(quán)訪(fǎng)問(wèn)漏洞修復(fù)問(wèn)題小結(jié)
相關(guān)文章
spring boot @PathVariable傳遞帶反斜杠參數(shù) / 的處理
這篇文章主要介紹了spring boot @PathVariable傳遞帶反斜杠參數(shù) / 的處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02java 字符串相減(很簡(jiǎn)單的一個(gè)方法)
本篇文章是對(duì)java中關(guān)于字符串相減的一個(gè)簡(jiǎn)單的方法進(jìn)行了介紹,需要的朋友參考下2013-07-07springboot基于IDEA環(huán)境熱加載與熱部署教程
這篇文章主要為大家介紹了springboot在IDEA環(huán)境下的熱加載與熱部署教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03IntelliJ IDEA 部署 Web 項(xiàng)目,看這一篇夠了!
這篇文章主要介紹了IntelliJ IDEA 部署 Web 項(xiàng)目的圖文教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Java Swing實(shí)現(xiàn)坦克大戰(zhàn)游戲
這篇文章主要介紹了Java Swing實(shí)現(xiàn)坦克大戰(zhàn)游戲,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很大的幫助喲,需要的朋友可以參考下2021-05-05解析spring-boot-starter-parent簡(jiǎn)介
本文通過(guò)代碼的形式給大家介紹了spring-boot-starter-parent的基礎(chǔ)知識(shí),需要的朋友可以參考下2018-09-09JavaScript中HTML元素操作的實(shí)現(xiàn)
本文主要介紹了JavaScript中HTML元素操作的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java實(shí)戰(zhàn)之實(shí)現(xiàn)在線(xiàn)小說(shuō)閱讀系統(tǒng)
本文主要介紹了一個(gè)通過(guò)Java實(shí)現(xiàn)的在線(xiàn)電子書(shū)小說(shuō)閱讀系統(tǒng),文中用到的技術(shù)有Layui、Springboot、SpringMVC、HTML、FTP、JavaScript、JQuery等,感興趣的可以試試2022-01-01