Spring Cloud Eureka:服务注册与发现

ゞ 浴缸里的玫瑰 2024-04-22 19:03 154阅读 0赞

在这里插入图片描述

?wei_shuo的个人主页

?wei_shuo的学习社区

?Hello World !


Spring Cloud Eureka:服务注册与发现

Spring Cloud Eureka是Spring Cloud生态系统中的一个组件,它是用于实现服务注册与发现的服务治理组件。在微服务架构中,服务之间存在复杂的依赖关系,而Spring Cloud Eureka可以帮助解决服务之间相互查找和通信的问题

Eureka简介

Eureka是Netflix开源的服务发现组件,用于在分布式系统中实现服务注册与发现。它是Netflix公司在构建微服务架构时开发的核心组件之一,后来成为了Spring Cloud生态系统中的一部分

Eureka注册中心搭建

Eureka服务端搭建

  • eureka-server依赖导入


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
  • 启动类添加注解@EnableEurekaServer

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(EurekaServerApplication.class, args);
    3. }

    }

  • 配置文件application.yml配置

    server:
    port: 8001 #指定运行端口
    spring:
    application:

    1. name: eureka-server #指定服务名称

    eureka:
    instance:

    1. hostname: localhost #指定主机地址

    client:

    1. fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
    2. register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)

    server:

    1. enable-self-preservation: false #关闭保护模式

Eureka客户端搭建

  • eureka-client依赖导入


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

    org.springframework.boot
    spring-boot-starter-web
  • 启动类添加注解@EnableDiscoveryClient

    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaClientApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(EurekaClientApplication.class, args);
    3. }

    }

  • 配置文件application.yml配置

    server:
    port: 8101 #运行端口号
    spring:
    application:

    1. name: eureka-client #服务名称

    eureka:
    client:

    1. register-with-eureka: true #注册到Eureka的注册中心
    2. fetch-registry: true #获取注册实例列表
    3. service-url:
    4. defaultZone: http://localhost:8001/eureka/ #配置注册中心地址

Eureka集群搭建

  • eureka-sever添加配置文件application-replica1.yml配置第一个注册中心

    server:
    port: 8002
    spring:
    application:

    1. name: eureka-server

    eureka:
    instance:

    1. hostname: replica1

    client:

    1. serviceUrl:
    2. defaultZone: http://replica2:8003/eureka/ #注册到另一个Eureka注册中心
    3. fetch-registry: true
    4. register-with-eureka: true
  • 给eureka-sever添加配置文件application-replica2.yml配置第二个注册中心

    server:
    port: 8003
    spring:
    application:

    1. name: eureka-server

    eureka:
    instance:

    1. hostname: replica2

    client:

    1. serviceUrl:
    2. defaultZone: http://replica1:8002/eureka/ #注册到另一个Eureka注册中心
    3. fetch-registry: true
    4. register-with-eureka: true
  • 修改Eureka-client,连接到集群

    server:
    port: 8102
    spring:
    application:

    1. name: eureka-client

    eureka:
    client:

    1. register-with-eureka: true
    2. fetch-registry: true
    3. service-url:
    4. defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/ #同时注册到两个注册中心

Eureka添加认证

  • 添加SpringSecurity依赖


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

    org.springframework.boot
    spring-boot-starter-security
  • 添加application.yml配置文件(配置用户名、密码)

    server:
    port: 8004
    spring:
    application:

    1. name: eureka-security-server

    security: #配置SpringSecurity登录用户名和密码

    1. user:
    2. name: macro
    3. password: 123456

    eureka:
    instance:

    1. hostname: localhost

    client:

    1. fetch-registry: false
    2. register-with-eureka: false

添加 Java 配置WebSecurityConfig

默认情况下,Spring Security会开启CSRF(Cross-Site Request Forgery)保护,这是一种用于防止跨站点请求伪造攻击的安全机制。当你添加了Spring Security依赖到应用程序中时,每个POST、PUT、DELETE等修改类请求都需要在请求头中包含CSRF token才能被服务器接受

默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token

  1. @EnableWebSecurity
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http.csrf().ignoringAntMatchers("/eureka/**");
  6. super.configure(http);
  7. }
  8. }

? 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞?收藏⭐️评论?


在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读