spring-cloud與netflixEureka整合(註冊中心)
基礎依賴
compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter')
eureka(單機)
服務端:
依賴
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
application.yml 配置
spring: application: name: dev eureka: client: service-url: defaultZone: http://本機ip地址:8761/eureka/ #註冊中心地址 register-with-eureka: false #表明該例項是否應該與尤里卡伺服器登記其資訊被別人發現。在某些情況下,您不希望您的例項被發現而你想發現其他例項。預設為true fetch-registry: false #表明這個客戶是否應該從尤里卡伺服器獲取尤里卡登錄檔資訊。預設為true server: port: 8761 View Code
啟動類新增 @EnableEurekaServer
客戶端:
主要依賴
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
application.yml 配置
eureka: client: service-url: defaultZone: http://eureka服務地址:8761/eureka/ View Code
啟動類新增 @EnableDiscoveryClient (註冊中心通用客戶端註解)或 @EnableEurekaClient (只有eureka客戶端可用)
eureka(叢集)
服務端
主要依賴
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 #使用自我保護,預設true peer-node-read-timeout-ms: 5000 #節點讀取超時時間,預設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 View Code
eureka.client.service-url.defaultZone 與 eureka.instance.hostsname eureka1 、 eureka2 、 eureka3 都再本機的 hosts 檔案裡事先寫好,
例如 eureka1 伺服器上的hosts檔案
127.0.0.1localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1eureka1 xxx.xxx.x.xx eureka2 xx.xxx.xxx.xx eureka3
啟動類同樣是新增 @EnableEurekaServer 注意:如果執行 jar 包時需要程式接收命令列引數,一定要再main方法的 SpringApplication.run() 方法 中傳入 args 引數
例如:
1 public static void main(String[] args) { 2SpringApplication.run(QnbbsConsumer.class,args); 3}
如果不傳入的話在命令列輸入 java -jar xxx.jar --引數名=引數值 引數將不起作用從而影響你無法選擇寫好的環境配置
客戶端:
依賴跟單機的一樣
application.yml 配置
eureka: instance: prefer-ip-address: true #是否顯示ip ip-address: 本機ip #填寫本機ip地址 client: service-url: defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
啟動類註解與單機版一樣
客戶端註冊一臺註冊中心,同時註冊到其他叢集中說明成功!
eureka新增 spring-security 許可權認證
依賴
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 配置
1 server: 2port: 8761 3 spring: 4application: 5name: eureka-server 6security: 7user: 8name: zyn 9password: zyn123... 10#使用者名稱與密碼若是不配置,系統會自動生成並列印在控制檯日誌上 11 12 eureka: 13client: 14register-with-eureka: false 15fetch-registry: false 16server: 17enable-self-preservation: false 18peer-node-read-timeout-ms: 5000 19 20 management: 21endpoints: 22web: 23base-path: / 24exposure: 25include: '*' 26endpoint: 27health: 28show-details: always 29restart: 30enabled: true 31server: 32port: 8761 33 --- 34 spring: 35profiles: eureka-jy 36 eureka: 37client: 38service-url: 39defaultZone: 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/ 40instance: 41hostname: eureka-A 42 --- 43 spring: 44profiles: eureka-lhn 45 eureka: 46client: 47service-url: 48defaultZone: 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/ 49instance: 50hostname: eureka-B 51 --- 52 spring: 53profiles: eureka-zsm 54 eureka: 55client: 56service-url: 57defaultZone: 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/ 58instance: 59hostname: eureka-C View Code
建立一個類並繼承 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(); // 關閉csrf http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 開啟認證 } }
啟動類添加註解
@EnableEurekaServer @EnableWebSecurity
下線服務
http://eureka地址:埠/eureka/apps/服務名/服務地址:服務埠號
例項如下:
erueka註冊中心ip: 10.100.1.100
埠: 12000
服務名: CPS-RISK-SERVER
例項id: 192.168.10.54:18883
則向下面的url通過http傳送delete命令,可將指定的服務例項刪除:
http://10.100.1.100:12000/eureka/apps/CPS-RISK-SERVER/192.168.10.54:18883
變更服務狀態
http://eureka地址:埠/eureka/apps/服務名/服務地址/status?value=${value}
其中${value}的取值為: OUT_OF_SERVICE , DOWN , UP