微服务注册与发现——Eureka

文章目录

服务发现(注册)机制

nodejs的Eureka Client开源实现

服务发现组件具备功能:

服务注册表

服务注册与服务发现

服务检查

Eureka架构图

Eureka使用

引用

java 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

配置

java 复制代码
server:
  port: 8761

eureka:
  client:
    registerWithEureka: false # 是否将自己注册到Eureka Server
    fetch-registry: false # 是否从Eureka Server获取注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/ # 设置与Eureka Server交互的地址,多个地址用,分隔

启动类标记

java 复制代码
@SpringBootApplication
@EnableEurekaServer // 声明这是一个Eureka Server
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

访问

http://localhost:8761/

微服务注册

微服务工程添加引用

注意添加版本号,否则会下载不下来

java 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

增加配置

java 复制代码
spring:
  application:
    name: microservice-provider-user # 用于指定注册到Eureka Server上的应用名称

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    instance:
      prefer-ip-address: true # 表示将自己的IP注册到Eureka Server

启动类增加注解

java 复制代码
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient // 声明这是一个Eureka Client

启动服务注册

Eureka Server集群部署

防止因Eureka Server宕机导致微服务不可用

通过运行多个实例并相互注册的方式实现高可用部署,实例间彼此增量地同步信息,确保所有节点数据一致。

修改配置文件

java 复制代码
spring:
  application:
    name: microservice-discovery-eureka
---
spring:
  config:
    activate:
      on-profile: peer1 # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1 # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/ # 将自己注册到peer2这个Eureka上面去
---
spring:
  config:
    activate:
      on-profile: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

启动多个eureka实例

java -jar microservice-discovery-eureka-3.0.2.jar --spring.profiles.active=peer1

java -jar microservice-discovery-eureka-3.0.2.jar --spring.profiles.active=peer2


微服务注册到多个eureka实例

java 复制代码
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
    instance:
      prefer-ip-address: true # 表示将自己的IP注册到Eureka Server

为Eureka Server添加用户认证

前面的示例均可以匿名访问,可以通过spring-security先登录之后在访问

引入spring-security

java 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置

java 复制代码
server:
  port: 8761

eureka:
  client:
    registerWithEureka: false # 是否将自己注册到Eureka Server
    fetch-registry: false # 是否从Eureka Server获取注册信息
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka/
spring:
  security:
    user:
      name: user # 配置登录的账号
      password: password123 # 配置登录的密码

关闭security的csrf,否则client无法注册

未设置,client注册会报Cannot execute request on any known server

java 复制代码
/**
 * 高版本的丢弃了
 *
 * security:
 *   basic:
 *    enabled: true
 * 配置,应该使用以下方式开启
 * @param http
 * @throws Exception
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(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();
        return http.build();
    }
}

client注册

仅需修改注册地址即可,注意和server保持一致

java 复制代码
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka/ # 需要这种格式 http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/

Eureka自我保护模式

https://blog.csdn.net/fengzelun/article/details/117718784

常见问题

1、Cannot execute request on any known server

https://juejin.cn/post/6995434651862958087

相关推荐
leonkay2 小时前
到底应不应该写注释?
性能优化·架构·个人开发·注释·代码规范·设计·规格说明书
奋斗tree2 小时前
EulerOS 2.0 等保三级版(ARM 架构)是什么?
arm开发·架构
heimeiyingwang2 小时前
【架构实战】JVM调优:GC日志分析与参数调优
jvm·架构
无忧智库2 小时前
集团数据资产管理平台全栈实战:从“打破孤岛”到“价值变现”的架构演进(PPT)
架构
ASKED_20193 小时前
Claude Code:架构、治理与工程实践
人工智能·架构
沐雪轻挽萤3 小时前
ROS架构中的RViz可视化框架与Gazebo动力学仿真器
架构
skilllite作者3 小时前
Spec + Task 作为「开发协议层」:Rust 大模型辅助的标准化、harness 化与可回滚
开发语言·人工智能·后端·安全·架构·rust·rust沙箱
不懂的浪漫4 小时前
# mqtt-plus 架构解析(八):Spring Boot 自动装配,这些零件是怎么被粘合起来的
spring boot·后端·物联网·mqtt·架构
却话巴山夜雨时i4 小时前
互联网大厂Java面试场景:Spring Boot、微服务与Redis实战解析
spring boot·redis·微服务·kafka·prometheus·java面试·电商场景
互联网散修5 小时前
零基础鸿蒙应用开发第三十四节:MVVM架构下的商品管理登录页
架构·harmonyos·mvvm·登录