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

猜你喜欢

转载自www.cnblogs.com/wps54213/p/12716846.html