Eureka2.0集羣 高可用的認證服務實現與搭建

        Springboot-2.0.2.RELEASE Eureka認證後,服務註冊失敗問題。java

        隨着近幾年微服務架構和Docker容器概念的火爆,也會讓Spring Cloud在將來愈來愈「雲」化的軟件開發風格中立有一席之地,尤爲是在目前五花八門的分佈式解決方案中提供了標準化的、全站式的技術方案,意義可能會堪比當年Servlet規範的誕生,有效推動服務端軟件系統技術水平的進步。git

        SpringCloud Eureka是SpringCloud Netflix服務套件中的一部分,它基於Netflix Eureka作了二次封裝,主要負責完成微服務架構中的服務治理功能。今天就來說講Eureka的高可用實現與搭建web

MAVEN相關配置

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.2.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
	<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>
	<!-- 用於服務注入驗證 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
</dependencies>

若是找不到包版本配置文件中加

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

application.yml 相關配置

spring:
  application:
    name: EUREKA
--- #注意這裏是三個"減號"
spring:
  profiles: eureka1
  security:
    user:
      name: admin
      password: 123123
server:
  port: 8001
eureka:
  instance:
    hostname: eureka1
  client:
    serviceUrl:
      defaultZone: http://admin:123123@eureka2:8002/eureka/,http://admin:123123@eureka3:8003/eureka/
    fetch-registry: true
    register-with-eureka: true
---
spring:
  profiles: eureka2
  security:
    user:
      name: admin
      password: 123123
server:
  port: 8002
eureka:
  instance:
    hostname: eureka2
  client:
    serviceUrl:
      defaultZone: http://admin:123123@eureka1:8001/eureka/,http://admin:123123@eureka3:8003/eureka/
    fetch-registry: true
    register-with-eureka: true
---
spring:
  profiles: eureka3
  security:
    user:
      name: admin
      password: 123123
server:
  port: 8003
eureka:
  instance:
    hostname: eureka3
  client:
    serviceUrl:
      defaultZone: http://admin:123123@eureka1:8001/eureka/,http://admin:123123@eureka2:8002/eureka/
    fetch-registry: true
    register-with-eureka: true

從上面的配置能夠看出咱們配置了3個Euerka服務,端口號分別是8001和8002與8003。
驗證的用戶名和密碼是:admin:123123spring

啓動類代碼

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

到這代碼就基本完了,本地已經能夠運行了。瀏覽器

啓動前先在hosts文件添加內容以下:架構

127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3
app

先本地運行一下:run configurations

分別啓動3個配置eureka1,eureka2,eureka3,啓動後到瀏覽器輸入:http://eureka1:8001/ 輸入你的用戶名和密碼。分佈式

敲黑板: 頁面中Instances currently registered with Eureka下面並沒得注入的別的服務,各類搜索引擎各類收,沒得個因此然,去掉Spring Security後問題解決,能夠知道問題是Spring Security引發的,查看源碼發現CSRF保護默認是開啓的,能夠禁用掉便可。ide

老版本代碼

security:   
  basic:
    enabled: true
  user:
    name: admin
    password: 123123

新版本解決方案

添加一個配置類禁用csrf以下:(可是你會發現,注入服務確不須要密碼了,說明失去了驗證。)spring-boot

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
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

完整的代碼以下:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    	http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

完美的結果

再次啓動三個eureka服務,若是一切都正確的話,結果入圖下:

在Centos上運行的腳本

啓動腳本:

#!/bin/sh
#啓動服務
APP_NAME=eureka-0.0.1-SNAPSHOT
rm -f tpid
nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka1> /data/apps/eureka/eureka1.log
nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka2> /data/apps/eureka/eureka2.log
nohup java -jar /data/apps/eureka/$APP_NAME --spring.profiles.active=eureka3> /data/apps/eureka/eureka3.log
echo $! > tpid
echo Start Success!

中止腳本:

#!/bin/sh
#中止服務
APP_NAME=eureka-0.0.1-SNAPSHOT
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Stop Process...'
    kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Kill Process!'
    kill -9 $tpid
else
    echo 'Stop Success!'
fi

後面的腳本我本身沒驗證,我也不怎麼會寫腳本,若是那個大神提供更好的腳本,小編感激涕零

源碼地址:https://gitee.com/bianxin.com/earn_knife/tree/master/eureka

歡迎加入技術討論羣:340697945      

相關文章
相關標籤/搜索