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管理界面

相关推荐
牛奶咖啡138 分钟前
学习设计模式《十八》——备忘录模式
学习·设计模式·备忘录模式·认识备忘录模式·备忘录模式的优缺点·何时使用备忘录模式·备忘录模式的使用示例
大龄门外汉35 分钟前
CPP学习之list使用及模拟实现
windows·学习·list
码字的字节39 分钟前
Hadoop与云原生集成:弹性扩缩容与OSS存储分离架构深度解析
hadoop·云原生·架构·oss存储
超浪的晨42 分钟前
Java List 集合详解:从基础到实战,掌握 Java 列表操作全貌
java·开发语言·后端·学习·个人开发
超浪的晨1 小时前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·开发语言·后端·学习·个人开发
香蕉可乐荷包蛋1 小时前
Python学习之路(十三)-常用函数的使用,及优化
开发语言·python·学习
许白掰2 小时前
Linux入门篇学习——借助 U 盘或 TF 卡拷贝程序到开发板上
linux·学习·借助 u 盘拷贝程序到开发板上·借助 tf卡拷贝程序到开发板上
David爱编程2 小时前
K8s 的 Namespace 到底解决了什么问题?
云原生·容器·kubernetes
小马爱打代码2 小时前
微服务中token鉴权设计的4种方式
微服务·云原生·架构
曾经的三心草2 小时前
微服务的编程测评系统3-加密-日志-apifox-nacos-全局异常
微服务·云原生·架构