当前位置: 代码网 > it编程>编程语言>Java > 【SpringCloud】深入探究Eureka:构建微服务架构中的高效服务发现系统

【SpringCloud】深入探究Eureka:构建微服务架构中的高效服务发现系统

2024年08月02日 Java 我要评论
Eureka是Netflix开源的一款用于构建弹性、高可用的服务发现系统的工具。在微服务架构中,服务数量庞大,每个服务可能会有多个实例。这时,需要一种机制来让服务能够自动地找到其他服务,以便进行通信。这就是服务发现的任务,而Eureka便是为此而生。Eureka服务器和Eureka客户端。Eureka服务器负责维护所有服务的注册信息,而Eureka客户端则负责向Eureka服务器注册自己,并从服务器获取其他服务的信息,以便进行通信。

在这里插入图片描述

什么是eureka?

基础知识:

服务注册与发现

两个组件:

img

eureka实战:

单机eureka构建步骤:

  • 1.建module
    cloud-eureka-server7001
  • 改pom:引入eureka的相关依赖
<dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
</dependency>

  • 3.写yml
server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 主启动
@springbootapplication
@enableeurekaserver
public class eurekamain7001 {
    public static void main(string[] args) {
        springapplication.run(eurekamain7001.class, args);
    }
}
  • 4.测试
http://localhost:7001/
  • 5.将服务注册进eureka成为提供者
dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
</dependency>
  • 6.改yml
server:
  port: 80

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #表示是否将自己注册进eurekaserver默认为true。
    register-with-eureka: true
    #是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchregistry: true
    service-url:
      defaultzone: http://localhost:7001/eureka
  • 7.主启动
@springbootapplication
@enableeurekaclient
public class mainapp80 {
    public static void main(string[] args) {
        springapplication.run(mainapp80.class, args);
    }
}
  • 8.注意事项:

image-20220123122714466

集群eureka构建步骤:

在这里插入图片描述

  • 127.0.0.1 eureka7001.com
  • 127.0.0.1 eureka7002.com
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com  #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己。
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultzone: http://eureka7002.com:7002/eureka/

7002中也将7001注册进去

4.主启动

package com.atguigu.springcloud;

@springbootapplication
@enableeurekaserver
public class eurekamain7002 {
    public static void main(string[] args) {
        springapplication.run(eurekamain7002.class, args);
    }
}

5.服务如集群

# 集群版
defaultzone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  
@value("${server.port}")
private string serverport;
///不要把地址写死
//public static final string paymentsrv_url = "http://localhost:8001";
// 通过在eureka上注册过的微服务名称调用
public static final string payment_srv = "http://cloud-payment-service";

package com.atguigu.springcloud.config;

@configuration
public class applicationcontextconfig {

    @bean
    @loadbalanced //使用@loadbalanced注解赋予resttemplate负载均衡的能力
    public resttemplate resttemplate() {
        return new resttemplate();
    }
}

instance:
  instance-id: payment8001

访问信息有ip信息提示:

prefer-ip-address: true     #访问路径可以显示ip地址
@resource
private discoveryclient discoveryclient;

@getmapping(value = "/discovery")
public object discovery() {
    list<string> services = discoveryclient.getservices();
    for (string service : services) {
        log.info("service:{}", service);
    }
    list<serviceinstance> instances = discoveryclient.getinstances("cloud-payment-service");
    for (serviceinstance instance : instances) {
        log.info(instance.getserviceid() + "\t" + instance.gethost() + "\t" + instance.getport() + "\t"
                 + instance.geturi());
    }
    return this.discoveryclient;
}
@springbootapplication
@enableeurekaclient
@enablediscoveryclient //服务发现
public class paymentmain8001 {
    public static void main(string[] args) {
        springapplication.run(paymentmain8001.class,args);
    }
}

总结

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com