搭建eureka叢集環境以及客戶端配置

不忘初心2021發表於2024-12-03

先來效果圖

eureka叢集節點一

eureka叢集節點二

節點一顯示的註冊服務

節點二顯示的註冊服務

eureka服務端配置如下所示

現在將eureka服務端放到了測試環境 192.168.31.50 192.168.31.60

服務資訊

最後給出全部的eureka程式碼

pom檔案

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.eureka</groupId>
    <artifactId>pingan-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

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

    <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>
<!--        <dependency>-->
<!--            <groupId>org.springframework.security</groupId>-->
<!--            <artifactId>spring-security-web</artifactId>-->
<!--            <version>5.4.11</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.20</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.5.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <finalName>eureka</finalName>
    </build>

</project>

  application.properties

#埠號
server.port=8764
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.60:8763/eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#關閉自動保護機制
eureka.server.enable-self-preservation=false
#eureka清理無效節點的時間間隔,單位毫秒
eureka.server.eviction-interval-timer-in-ms=60000

spring.application.name=eureka_8764

#日誌列印級別
#logging.level.com.eureka=info
#logging.level.web=info
#spring.devtools.add-properties=false


logging.level.com.eureka=debug
logging.level.org.springframework.web.client.RestTemplate=info
logging.level.org.springframework.web.HttpLogging=info
logging.level.web=debug
spring.devtools.add-properties=false

spring.security.user.name=qiuxie
spring.security.user.password=E13301455191qiuxie

spring.security.basic.enable=true

  程式碼部分

package com.eureka.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;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登入,所以必須是httpBasic,
        // 如果是form方式,不能使用url格式登入
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

  

package com.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * @author yourheart
 * @Description
 * @create 2022-04-20 21:17
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}

  專案結構展示

客戶端配置為

eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.60:8763/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.50:8764/eureka/

spring.security.user.name=aa
spring.security.user.password=123456

  

相關文章