【springcloud】eureka服务治理入门

简介: 【springcloud】eureka服务治理入门

一、简介



在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理起来由于复杂,所以需要服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡和容错等实现服务注册与发现。640.png


eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心,而系统中的其它微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。


当客户端向 Eureka Server, 注册时,它会提供有关自身的元数据,例如主机和端口 , 健康指标 URL, 主页等。Eureka 从属于服务的每个实例接收心跳消息。如果心跳故障超过可配置的时间表,,则该实例通常会从注册表中删除。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC远程调用框架核心设计思想:在于注册中心,因为注册中心会维护一个服务列表(管理每个服务与服务之间的一个依赖关系),客户端可以通过注册中心获取服务列表。


二、Eureka Server单机部署



1. pom依赖引入


<!--服务发现客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--web相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>


2. 配置主启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}


3. yml配置


server:
  port: 7001
spring:
  application:
    name:  cloud-eureka-service
eureka:
  instance:
    hostname: eureka7001
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


4. 测试


640.png


三、Eureka Server集群部署



1. pom依赖引用(和单机版一致)


<!--服务发现客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--web相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>


2. 配置启动类


//eureka-7001
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}
//eureka-7002
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//表示服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}


3. yml配置


# 7001
server:
  port: 7001
spring:
  application:
    name:  cloud-eureka-service
eureka:
  instance:
    hostname: eureka7001
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
      defaultZone: http://eureka7002:7002/eureka/
# 7002
server:
  port: 7002
spring:
  application:
    name:  cloud-eureka-service
eureka:
  instance:
    hostname: eureka7002
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址
      defaultZone: http://eureka7001:7001/eureka/


4. 测试


640.png640.png


四、Eureka client部署(服务消费者和服务提供者)



1. pom依赖引入


<!--服务发现客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


2. 配置主启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class PaymentApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentApplication.class,args);
    }
}


3. yml配置


server:
  port: 8888
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
  instance:
    prefer-ip-address: true
    instance-id: payment-8888


4. 测试


640.png


五、配置actuator信息



eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka
  instance:
    prefer-ip-address: true #显示主机ip名称
    instance-id: payment-8888 #实例id
management:
  endpoint:
    health:
      show-details: always #显示详细信息
  endpoints:
    web:
      exposure:
        exclude: env #排除actuator暴露的接口
        include: ["health","info","mappings"] #设置暴露的actuator接口

640.png


温馨提示


点击如果/info获取空信息获取到的数据为空,需要做如下配置:


  1. pom文件配置,添加springboot插件
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>${project.name}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
  1. 执行springboot:build-info

640.png


  1. 执行成功后,重启项目即可获取info的build信息
{
  "build": {
    "version": "1.0-SNAPSHOT",
    "artifact": "payment",
    "name": "payment",
    "group": "com.yuyue.online.springcloud",
    "time": "2021-07-24T15:21:12.701Z"
  }
}


  1. yml配置文件配置服务相关信息
info: # info后字段随意配置
  author: javacfox
  desc: 支付服务
# 结果展示
{
  "author": "javacfox",
  "desc": "支付服务",
  "build": {
    "version": "1.0-SNAPSHOT",
    "artifact": "payment",
    "name": "payment",
    "group": "com.yuyue.online.springcloud",
    "time": "2021-07-24T15:21:12.701Z"
  }
}


原理解析

使用了springboot插件生成了build信息(build-info.properties),actuator会读取文件中信息


640.png

目录
相关文章
|
6天前
SpringCloud服务已经关但是Eureka还是显示up
SpringCloud服务已经关但是Eureka还是显示up
27 0
|
6天前
|
缓存 负载均衡 监控
SpringCloud&Eureka理论与入门
SpringCloud&Eureka理论与入门
22 0
|
6天前
|
负载均衡 监控 算法
【微服务 SpringCloud】实用篇 · Eureka注册中心
【微服务 SpringCloud】实用篇 · Eureka注册中心
21 1
|
6天前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
207 0
|
6天前
|
负载均衡 监控 容灾
【SpringCloud】详解Eureka注册中心
【SpringCloud】详解Eureka注册中心
24 0
|
6天前
|
Java Maven 微服务
第四章 Spring Cloud Netflix 之 Eureka
第四章 Spring Cloud Netflix 之 Eureka
15 0
|
6天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
6天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
158 0
|
6天前
|
Java 微服务 Spring
Spring Cloud Eureka详细介绍
Spring Cloud Eureka详细介绍
24 1
|
6天前
|
Java Maven Nacos
Spring Cloud Eureka 服务注册和服务发现超详细(附加--源码实现案例--及实现逻辑图)
Spring Cloud Eureka 服务注册和服务发现超详细(附加--源码实现案例--及实现逻辑图)
32 0