Eureka的实操--中篇

Eureka的实操

2、安全连接方式

Eureka的安全连接可以通过以下步骤实现:

  1. 添加依赖:在项目的pom.xml文件中添加Eureka的依赖。
  2. 配置安全连接:在项目的application.yml或application.properties文件中添加Eureka的安全连接配置。具体包括设置安全基础URL、客户端ID、客户端密钥等。
  3. 关闭跨域攻击:在服务端增加配置类,关闭防止跨域攻击。如果需要跨域,可以在配置类中添加允许跨域的注解。
  4. 启用认证:在客户端项目中添加安全配置,设置账号密码,并启用Eureka的安全连接。

完成以上步骤后,即可实现Eureka的安全连接。注意,具体的配置方式可能因项目需求和环境而有所不同,需要结合实际情况进行调整。

架构图

1、Eureka-server

代码

java 复制代码
package com.ldzg.config;

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;

/**
 * 默认情况下SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,
 * Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
 * Created by macro on 2024/1/12.
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}
java 复制代码
package com.ldzg;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(EurekaServerApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path ="";// env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application Eureka is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "swagger-ui: \thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +
                "Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
    }

}

pom.xml文件

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-learning</artifactId>
        <groupId>com.ldzg</groupId>
        <version>1.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server1</artifactId>
    <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>
    </dependencies>

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

</project>

application.yml配置文件说明

yml 复制代码
server:
  port: 60004
spring:
  application:
    name: eureka-security-server
  security: #配置SpringSecurity登录用户名和密码
    basic:
      enabled: true
    user:
      name: ldzg
      password: 123456
eureka:
  # 关闭自我保护
  enable-self-preservation: false
  # 清理服务器
  eviction-interval-timer-in-ms: 5000
  instance:
    hostname: localhost
  client:
    healthcheck:
      enabled: true
    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
#    healthcheck:
#      #可以上报服务的真实健康状态
#      enabled: true
    service-url: #eureka的地址信息
        defaultZone: http://ldzg:123456@localhost:60004/eureka

运行结果:

访问eureka管理页面

2、springcloud-consumer

java代码

java 复制代码
package com.ldzg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserServiceApplication {

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

}

pom.xml文件

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-learning</artifactId>
        <groupId>com.ldzg</groupId>
        <version>1.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


    </dependencies>

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

application.yml配置文件说明

yml 复制代码
server:
  port: 61004
spring:
  application:
    name: eureka-consumer
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    #    healthcheck:
    #      #可以上报服务的真实健康状态
    #      enabled: true
    service-url:
      defaultZone: http://ldzg:123456@localhost:60004/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    appname: eureka-consumer

运行结果

eureka管理界面

相关推荐
slomay1 小时前
关于对比学习(简单整理
经验分享·深度学习·学习·机器学习
hengzhepa1 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
小小洋洋3 小时前
BLE MESH学习1-基于沁恒CH582学习
学习
网络研究院3 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
Ace'4 小时前
每日一题&&学习笔记
笔记·学习
tangdou3690986554 小时前
1分钟搞懂K8S中的NodeSelector
云原生·容器·kubernetes
IM_DALLA4 小时前
【Verilog学习日常】—牛客网刷题—Verilog进阶挑战—VL25
学习·fpga开发·verilog学习
丶Darling.4 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
Lansonli5 小时前
云原生(四十一) | 阿里云ECS服务器介绍
服务器·阿里云·云原生
Dylanioucn6 小时前
【分布式微服务云原生】掌握分布式缓存:Redis与Memcached的深入解析与实战指南
分布式·缓存·云原生