spring-cloud與netflixEureka整合的示例分析

這篇文章主要為大家展示了“spring-cloud與netflixEureka整合的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“spring-cloud與netflixEureka整合的示例分析”這篇文章吧。

專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)建甌免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

基礎(chǔ)依賴

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ù)端:

依賴

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è)客戶是否應(yīng)該從尤里卡服務(wù)器獲取尤里卡注冊(cè)表信息。默認(rèn)為true
server:
 port: 8761

啟動(dòng)類添加 @EnableEurekaServer

客戶端:

主要依賴

compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

application.yml 配置

eureka:
 client:
  service-url:
   defaultZone: http://eureka服務(wù)地址:8761/eureka/

啟動(dòng)類添加 @EnableDiscoveryClient (注冊(cè)中心通用客戶端注解)或 @EnableEurekaClient (只有eureka客戶端可用)

eureka(集群)

服務(wù)端

主要依賴

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 文件里事先寫好,

例如 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)類同樣是添加 @EnableEurekaServer 注意:如果運(yùn)行 jar 包時(shí)需要程序接收命令行參數(shù),一定要再main方法的 SpringApplication.run() 方法 中傳入 args 參數(shù)

例如:

public static void main(String[] args) {
   SpringApplication.run(QnbbsConsumer.class,args);
}

如果不傳入的話在命令行輸入 java -jar xxx.jar --參數(shù)名=參數(shù)值 參數(shù)將不起作用從而影響你無法選擇寫好的環(huán)境配置

客戶端:

依賴跟單機(jī)的一樣

application.yml 配置

eureka:
 instance:
  prefer-ip-address: true #是否顯示ip
  ip-address: 本機(jī)ip #填寫本機(jī)ip地址
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/

啟動(dòng)類注解與單機(jī)版一樣

客戶端注冊(cè)一臺(tái)注冊(cè)中心,同時(shí)注冊(cè)到其他集群中說明成功!

eureka添加 spring-security 權(quán)限認(rèn)證

依賴

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... 
  #用戶名與密碼若是不配置,系統(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è)類并繼承 WebSecurityConfigurerAdapter 類,并在此類上添加 @EnableWebSecurity和@Configuration 注解,重寫 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(); // 開啟認(rèn)證
  }
}

啟動(dòng)類添加注解

@EnableEurekaServer
@EnableWebSecurity

下線服務(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通過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

以上是“spring-cloud與netflixEureka整合的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文題目:spring-cloud與netflixEureka整合的示例分析
文章URL:http://muchs.cn/article14/jcphge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)ChatGPT、搜索引擎優(yōu)化、App開發(fā)、網(wǎng)站制作網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司