当前位置: 代码网 > it编程>编程语言>Java > Spring Cloud学习笔记:Eureka简介,Eureka简单样例

Spring Cloud学习笔记:Eureka简介,Eureka简单样例

2024年08月01日 Java 我要评论
这是本人学习的总结,主要学习资料如下马士兵教育@[TOC](目录)

这是本人学习的总结,主要学习资料如下
- 马士兵教育

1、eureka

1.1、架构

eurekaspringcloud nexflix的核心子模块,其中包含serverclient

server提供服务注册,存储所有可用服务节点。

client用于简化和server的通讯复杂度。

下面是eureka的简单架构图

在这里插入图片描述

每一个服务节点需要在eureka server中注册,如果需要其他节点的服务,则需要远程调用service providerprovider会访问server,由server找到一个合适的节点提供服务给cumsumer



1.2、核心特性

  1. 服务注册:这是最核心的功能,其余的特性都是对这个功能的加强。
  2. 服务续约:client每隔30s就会向server发送一次心跳来续约,超过90s没有续约就会被server删除这个服务节点。
  3. 服务下线:client可以主动向server发送cancel命令优雅下线。
  4. 缓存注册列表:client会缓存从server获取的注册列表,并且每30s更新一次。

2、建立spring cloud项目

2.1、项目结构和父项目依赖

接下来就是代码展示如何配置启动serverclient,以及client之间获取信息。

这是项目结构,两个子module,分别是serverorder-clientuser-clientserver提供注册服务,另外两个作为client则是到server注册然后互相调用对方的服务。

在这里插入图片描述

这是根目录的dependency

<properties>
    <java.version>1.8</java.version>
    <spring-boot.version>2.3.7.release</spring-boot.version>
    <spring-cloud.version>hoxton.sr12</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
        <groupid>org.projectlombok</groupid>
        <artifactid>lombok</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-security</artifactid>
    </dependency>
</dependencies>

<dependencymanagement>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-dependencies</artifactid>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-dependencies</artifactid>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencymanagement>

2.2、启动server

2.2.1、dependency

<dependency>
   <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
</dependency>

2.2.2、配置文件

resources/application.yml

spring:
  application:
    name: msb-eureka-server
server:
  port: 8761

eureka:
  instance:
    #注册实例名称
    hostname: localhost
    #是否将自己的ip注册到eureka中,默认false 注册 主机名
    prefer-ip-address: true
    # eureka客户端需要多长时间发送心跳给eureka,表明他仍然或者,默认是30
    # 通过下面方式我们可以设置,默认单位是秒
    lease-renewal-interval-in-seconds: 10
    # eurkea服务器在接受到实例最后一次发送的心跳后,需要等待多久可以将次实例删除
    # 默认值是90
    # 通过下面方式我们可以设置,默认单位是秒
    lease-expiration-duration-in-seconds: 30
  client:
    #是否注册到eureka服务中
    register-with-eureka: false
    #是否拉取其他服务
    fetch-registry: false
    

2.2.3、server端启动代码

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

启动以后打开网页检查。localhost:8761
请添加图片描述


2.3、启动client

2.3.1、dependency

<dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
</dependency>

2.3.2、配置文件

order-client和```user-client````都一样,因为是单机模拟所以监听的端口号不同。

# 节点在server中注册的名字
spring:
  application:
  	# user-client则用user-client
    name: order-client
server:
# order-client 监听9002, user-client监听9003
  port: 9002

  
eureka:
  client:
  # 这个一定要配对,server地址后面默认要加一个上下文eureka
    service-url:
      defaultzone: http://localhost:8761/eureka


management:
  endpoints:
    web:
      exposure:
        include: shutdown #暴露shutdown端点
  endpoint:
    shutdown:
      enabled: true #再次确认暴露shutdown端点

feign:
  tokenid: 11111111111111111111

2.3.3、client端启动代码

注意有两个注解可以将其标注为client,分别是@enablediscoveryclient@enableeurekaclient

这里推荐使用@enablediscoveryclient,因为后者是netfliex提供的,如果使用后者,后期要更换其它注册中心就需要更换注解,比较麻烦。

这是order-client的代码

@enablediscoveryclient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@enableeurekaclient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@springbootapplication
public class eurekaorderclientapplication {
    public static void main(string[] args) {
        springapplication.run(eurekaorderclientapplication.class);
    }
}

这时user-client的代码

@enablediscoveryclient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@enableeurekaclient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@springbootapplication
public class eurekauserclientapplication {
    public static void main(string[] args) {
        springapplication.run(eurekauserclientapplication.class);
    }
}

到server的页面查看,两个client都注册成功。
在这里插入图片描述

2.3.4、提供rpc服务

因为rpc是基于http实现的协议,所以我们提供rpc服务时就像写一个controller的服务一样。

这里设定order模块会调用user提供的服务。

@slf4j
@restcontroller
public class usercontroller {
    @requestmapping("/getuserinfo")
    public string getuser(string userid) {
        log.info(userid);	
        return "userinfo: {userid: "+ userid +"}";;
    }
}

接下来就看order模块如何通过eureka调用user提供的服务。

2.4、服务之间获取信息

引入loadbalancerclient,从这个bean中可以获得其他注册的client元数据,比如地址,端口号等。

获取到这些信息后就可以组成请求地址,然后获取数据。

下面这个例子展示了如何获取其他client的元信息并且调用其它client的服务。

@configuration
public class restconfig {
    @bean
    public resttemplate resttemplate(){
        return new resttemplate();
    }
}

@service
public class orderservice {
	@autowired
    private loadbalancerclient eurekaclient;
    @autowired
    private resttemplate resttemplate;
	
	public void getuser() {
		serviceinstance instance = eurekaclient.choose("user-client");
        string hostname = instance.gethost();
        int port = instance.getport();
        string uri = "/getuserinfo?userid=" + userid;
        string url = "http://" + hostname + ":" + port + uri;
        return resttemplate.getforobject(url, string.class);
	}
	
}
(0)

相关文章:

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

发表评论

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