Eureka Server集群服务搭建(包含权限认证)

目录

一、配置无权限认证的eureka server.

        1.1、使用官网上新建脚手架的的工具,创建项目。

        1.2、进入创建项目的界面。

        1.3、根据官网文档配置。

        1.4、进入技术文档​

        1.5、集群部署服务​

        1.6、idea导入解压后的项目,并根据文档进行配置。

        1.7、访问服务

                1.7.1、eureka-server1的注册信息结果查看。

                1.7.2、eureka-server2的注册信息结果查看。

二、配置有权限认证的eureka服务

        2.1、pom.xml新增

        2.2、eureka-server1的application.yml配置权限认证:

        2.3、eureka-server2的application.yml配置权限认证:

        2.4、重写WebSecurityConfigurerAdapter的configure方法配置csrf过滤路径

        2.5、访问eureka-server2界面,出现登录对话框表示权限配置生效

        2.6、登录后

        2.7、 访问eureka-server1界面,出现登录对话框表示权限配置生效

        2.8、登录后

三、扩展点:eureka自我保护机制的告警.

        3.1、原因分析——这个是Eureka的自我保护机制

        3.2、Euraka自我保护机制开关

四、参考资料:Spring | Home。


一、配置无权限认证的eureka server.

        1.1、使用官网上新建脚手架的的工具,创建项目。

        1.2、进入创建项目的界面。

        1.3、根据官网文档配置。

 

        1.4、进入技术文档

        1.5、集群部署服务

        1.6、idea导入解压后的项目,并根据文档进行配置。

                 3.1.1、maven的pom.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>spring-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-eureka</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>2020.0.3</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-test</artifactId>
			<scope>test</scope>
		</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

                3.1.2、启动类配置(@EnableEurekaServer)

package com.example.springeureka;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringEurekaApplication {

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

}

                3.1.3、eureka-server1的application.yml配置,并将eureka-server1服务注册到eureka-server2。

spring:
  application:
    name: eureka-server1

server:
  port: 8671

eureka:
  instance:
    hostname: localhost
  client:
    #是否将自己作为一个服务注册(这里是集群部署互相注册,所以需要作为一个服务注册), 默认为true
    registerWithEureka: true
    #是否从其他eureka服务拉取已注册的服务信息, 默认为true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8672/eureka

                 3.1.4、eureka-server2的application.yml配置,,并将eureka-server2服务注册到eureka-server1。

spring:
  application:
    name: eureka-server2

server:
  port: 8672

eureka:
  instance:
    hostname: localhost
  client:
    #是否将自己作为一个服务注册(这里是集群部署互相注册,所以需要作为一个服务注册), 默认为true
    registerWithEureka: true
    #是否从其他eureka服务拉取已注册的服务信息, 默认为true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8671/eureka

        1.7、访问服务

                1.7.1、eureka-server1的注册信息结果查看。

                1.7.2、eureka-server2的注册信息结果查看。

二、配置有权限认证的eureka服务

        2.1、pom.xml新增

		<!--加入服务认证(密码),需要引入security-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

        2.2、eureka-server1的application.yml配置权限认证:

spring:
  application:
    name: eureka-server1
  #配置权限认证的用户名和密码
  security:
    user:
      name: eureka
      password: eureka
server:
  port: 8671

eureka:
  instance:
    hostname: localhost
  client:
    #是否将自己作为一个服务注册(这里是集群部署互相注册,所以需要作为一个服务注册), 默认为true
    registerWithEureka: true
    #是否从其他eureka服务拉取已注册的服务信息, 默认为true
    fetchRegistry: false
    #注册已经设置了权限认证的eureka,路径上需要带上密码和账号,否则服务注册。格式:http://username:password@ip:端口/eureka
    serviceUrl:
      defaultZone: http://eureka:eureka@localhost:8672/eureka

        2.3、eureka-server2的application.yml配置权限认证:

spring:
  application:
    name: eureka-server2
  #配置权限认证的用户名和密码
  security:
    user:
      name: eureka
      password: eureka
server:
  port: 8672

eureka:
  instance:
    hostname: localhost
  client:
    #是否将自己作为一个服务注册(这里是集群部署互相注册,所以需要作为一个服务注册), 默认为true
    registerWithEureka: true
    #是否从其他eureka服务拉取已注册的服务信息, 默认为true
    fetchRegistry: false
    #注册已经设置了权限认证的eureka,路径上需要带上密码和账号,否则服务注册。格式:http://username:password@ip:端口/eureka
    serviceUrl:
      defaultZone: http://eureka:eureka@localhost:8671/eureka

        2.4、重写WebSecurityConfigurerAdapter的configure方法配置csrf过滤路径

org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter的configur方法,忽略eureka的注册路径。

httpBasic的知识点:详解Spring Security的HttpBasic登录验证模式

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;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //csrf忽略eureka的注册路径,否则注册eureka服务会被拦截
        http.csrf().ignoringAntMatchers("/eureka/**");
        //支持httpBasic
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

        2.5、访问eureka-server2界面,出现登录对话框表示权限配置生效

        2.6、登录后

        2.7、 访问eureka-server1界面,出现登录对话框表示权限配置生效

        2.8、登录后

 

三、扩展点:eureka自我保护机制的告警.

出现EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

        3.1、原因分析——这个是Eureka的自我保护机制

Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。如果多机集群部署一般不会出现该问题。

        3.2、Euraka自我保护机制开关

可以通过配置eureka.server.enable-self-preservation=false;参数来关闭保护机制,以确保注册中心可以将不可用的实例正确剔除,默认为true。
 

四、参考资料:Spring | Home

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值