SpringCloud(Hoxton.SR3)基礎篇:第三章、Eureka集群 高可用的認證服務實現與搭建


一、Eureka Server高可用搭建(服務注冊中心)

1.1MAVEN相關依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.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>
        <!-- jdk版本 -->
        <java.version>1.8</java.version>
        <!-- SpringCloud版本號,官方最新穩定版本 -->
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <!--依賴管理,用於管理spring-cloud的依賴 -->
    <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>

    <dependencies>
        <!-- eureka服務端依賴jar包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- eureka安全組件jar包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

1.2 application.yml 相關配置

eureka服務一配置

server:
   port: 8761
#安全認證配置
spring:
   application:
      name: EUREKA-HA
   security:
      basic:
         enabled: true
      user:
         name: user
         password: password123
eureka:
   #Eureka實例名,集群中根據這里相互識別
   instance:
      hostname: peer1
   client:
      serviceUrl:
         #注冊中心地址
         defaultZone: http://user:password123@peer2:8762/eureka/,http://user:password123@peer3:8763/eureka/

 

eureka服務二配置

server:
   port: 8762
#安全認證配置
spring:
   application:
      name: EUREKA-HA
   security:
      basic:
         enabled: true
      user:
         name: user
         password: password123
eureka:
   #Eureka實例名,集群中根據這里相互識別
   instance:
      hostname: peer2
   client:
      serviceUrl:
         defaultZone: http://user:password123@peer1:8761/eureka/,http://user:password123@peer3:8763/eureka/

 

eureka服務三配置

server:
   port: 8763
#安全認證配置
spring:
   application:
      name: EUREKA-HA
   security:
      basic:
         enabled: true
      user:
         name: user
         password: password123
eureka:
   #Eureka實例名,集群中根據這里相互識別
   instance:
      hostname: peer3
   client:
      serviceUrl:
         defaultZone: http://user:password123@peer1:8761/eureka/,http://user:password123@peer2:8762/eureka/

 

1.2.2 配置hosts文件,識別peer1,peer2,peer3地址

 

 

  windows系統hosts文件路徑:C:\Windows\System32\drivers\etc

 

 

 

  修改步驟:以管理員的身份運行CMD,輸入notepad打開記事本。用記事本往hosts文件內添加內容

  hosts文件加入peer1,peer2,peer3內容

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
127.0.0.1        peer1
127.0.0.1        peer2
127.0.0.1        peer3

 

 

1.3 啟動類代碼

 

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

@SpringBootApplication
//該注解表明應用為eureka服務,有可以聯合多個服務作為集群,對外提供服務注冊以及發現功能
@EnableEurekaServer
public class EurekaHaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaHaApplication.class, args);
    }
}

 

 

1.4 解決頁面中Instances currently registered with Eureka下面並沒得注入的別的服務

禁用Spring Security的CSRF保護,添加一個配置類禁用csrf

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;

/**
 * eureka開啟服務無法連接注冊中心
 * spring Cloud 2.0 以上 security默認啟用了csrf檢驗,要在eurekaServer端配置security的csrf檢驗為false
 * @author computer
 *
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
    
}

 

1.5 eureka頁面出現unavailable-replicas(不可用分片)問題

原因一:prefer-ip-address 配置項設置錯誤
比如,8761服務器設置了prefer-ip-address: true,那么它注冊到 8762和 8763服務器時應該使用 defaultZone:http://yourIP:8761/eureka/ ,但此時可以發現使用的仍然是 hostname 名,導致錯誤發生。
另一種原因是,三個8761、8762和 8763都設置了prefer-ip-address: true,導致最后解析出來的 hostname 都是相同的IP,使副本不可用。
 
原因二:register-with-eureka 配置項設置錯誤
看網上很多博客和資料都把此項設置成了 false,此時 eureka 不會注冊到其他服務器上,所以出現錯誤。
 
原因三:其他原因
還有一些其他原因大家可以參考這里: Eureka高可用,節點均出現在unavailable-replicas下

 

 

 

最后成功啟動三個eureka服務,結果如圖

 

 

 

參考文獻:https://blog.csdn.net/longguo321/article/details/80493618

     https://blog.csdn.net/liupeifeng3514/java/article/details/85273961

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM