zoukankan      html  css  js  c++  java
  • spring cloud Eureka注册中心集群搭建

    1.创建springcloud-eureka maven项目

     pom.xml

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <!-- 父项目yilai -->
        <parent>
            <groupId>com.pupeiyuan.springcloud</groupId>
            <artifactId>spring-Cloud</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>springcloud-eureka</artifactId>
        <packaging>jar</packaging>
        <dependencies>
            <!-- eureka server 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
        </dependencies>
    </project>

    2.启动类EurekaServerApplication.java

    package com.spring.pupeiyuan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
             SpringApplication.run(EurekaServerApplication.class,args);
        }
    }

    3.创建application-peer1.properties向peer2注册中心注册,把peer1注册中心当做一个client注册上去

    application-peer1.properties

    spring.application.name=spring-cloud-eureka
    server.port=8000
    eureka.instance.hostname=peer1
    eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@peer2:8001/eureka/
    eureka.environment=eureka-test1
    eureka.datacenter=eureka-data1
    eureka.server.enable-self-preservation=false
    eureka.server.eviction-interval-timer-in-ms=10000
    
    security.basic.enabled=true
    security.user.name=root
    security.user.password=123456

    创建application-peer2.properties向peer1注册中心注册,把peer2注册中心当做一个client注册上去

    application-peer2.properties

    spring.application.name=spring-cloud-eureka
    server.port=8001
    eureka.instance.hostname=peer2
    eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@peer1:8000/eureka/
    eureka.environment=eureka-test2
    eureka.datacenter=eureka-data2
    eureka.server.enable-self-preservation=false
    eureka.server.eviction-interval-timer-in-ms=4000
    
    security.basic.enabled=true
    security.user.name=root
    security.user.password=123456

    创建application.properties指定启动时应用的配置文件

    application.properties

    spring.profiles.active=peer2

    4.目前版本加入安全验证spring-boot-starter-security时,会发生注册中心无法注册的问题,解决如下

    添加WebSecurityConfig.java配置文件到项目中,关闭csrf

    WebSecurityConfig.java

    package com.spring.pupeiyuan.config;
    
    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;
    import org.springframework.security.config.http.SessionCreationPolicy;
    
    @EnableWebSecurity
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
            http.csrf().disable(); // 关闭csrf
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
        }
    }

    结果演示

  • 相关阅读:
    html5 video标签如何禁止视频下载
    Redis源代码-数据结构Adlist双端列表
    HTML5分析实战WebSockets基本介绍
    Chromium on Android: Android在系统Chromium为了实现主消息循环分析
    Android AIDL使用特定的解释
    [LeetCode]Maximum Product Subarray
    OC省字典的数组摘要集
    CocoaChina 第四个测试
    Java在的时候,类定义HashSet初始化方法
    WSHPSRS-匹克选择列表生成器-SRS(R12.2.3)
  • 原文地址:https://www.cnblogs.com/pypua/p/10114954.html
Copyright © 2011-2022 走看看