SpringCloud学习2 - 组件:Eureka微服务发现

38 阅读3分钟

文章目录

1. 概念

下图:即是Eureka_Server中介的用处
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2PnIEwiD-1587806443363)(en-resource://database/24612:1)]


Eureka_Serve原理:

服务向中介Eureka_Serve发送 (心跳包),用来证明服务是有效,可供给其他微服务使用的 – 默认30s发送一次。如果中介Sureka_Server在90s内没有收到该微服务的心跳包,则会从中介移除该服务的联系信息

Eureka_Serve信息中介集群

复制方式完成集群内中介信息的同步

Eureka_Client:具有缓存机制,即可以缓存曾经使用到的其他服务的联系信息

2. 使用

2.1 Eureka_Server服务

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BeKsqOcj-1587806443382)(en-resource://database/24620:1)]

步骤1 - 添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>


步骤2 - 启动类中添加注解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
   
   public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}


步骤3 - 设置全局配置文件application.properties

#----配置服务名称及端口----
spring.application.name=eurekaServer
server.port=1111


#----服务注册中心配置----
#服务注册中心实例的主机名
eureka.instance.hostname=localhost


#是否向服务注册中心注册自己,默认为true
eureka.client.register-with-eureka=false


#由于注册中心的职责是维护服务实例,它并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false


#服务注册中心的配置内容,指定服务注册中心的位置 - 使用逗号分隔配置多个中介进行信息集群
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/



spring.cloud.inetutils.timeout-seconds=60

spring.profiles.active=product


#关闭中介的 服务联系信息自我保护功能 --  只要服务没在90s内发送心跳包则直接从中介删除该服务联系信息
#eureka.server.enable-self-preservation=false

#注册中心检查服务的存活时间
#eureka.server.eviction-interval-timer-in-ms=1000


步骤4 - 启动Eureka_Server中介服务,访问http://localhost:1111/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LwWQhwDX-1587806443387)(en-resource://database/24618:1)]


2.2 Eureka_Client服务

创建一个Product_Server微服务为例

步骤1 - 添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


步骤2 - 启动类中添加注解@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

}


步骤3 - 设置全局配置文件application.properties

server:
  port: 4040

eureka:
  instance:
    instance-id: productService
    prefer-ip-address: true
  client:
    service-url:
       #用来指定注册服务中心地址
      defaultZone: http://127.0.0.1:1111/eureka/


#配置数据库连接信息
spring:
  application:
    name: productService
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/springcloudproject?serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver


#mybatis配置信息
mybatis:
  type-aliases-package: top.linruchang.product_service.domain
  mapper-locations: classpath:mapper/*Mapper.xml
  configuration:
    cache-enabled: true
    lazy-loading-enabled: true
    
    
#pagehelper配置信息
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
    returnPageInfo: check


#下列3个属性可不用设置,直接使用默认即可  -- 设置在服务客户端配置中的
#服务失效时间 -- 90s内没接收到心跳包,则该服务进入自我保护功能 -- 服务90s后自动删除(接收心跳包从重新刷新时间)
#eureka.instance.lease-expiration-duration-in-seconds=90

#服务每隔几秒发送心跳包进行验证该服务还能进行提供使用 -- (服务30s内发送心跳包证明自己存活)
#eurek.instance.lease-renewal-interval-in-seconds=30

#服务会缓存中介的服务联系信息,每隔几秒服务会从中介进拉取信息更新服务的联系信息
#eureka.client.registry-fetch-interval-seconds=30


步骤4 - 启动Eureka_Server中介、以及多个Eureka_Client微服务,访问http://localhost:1111/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-10nn1K6y-1587806443394)(en-resource://database/24728:1)]

2.3 IDEA模拟集群,即在多个端口号配置服务

在配置文件中,用逗号分隔即可

# 配置在Eureka_Server即集群中介,他们之间的信息是同步的
# 配置在Eureka_Client则他们的联系信息会同时发送心跳包向这几个中介进行注册联系信息
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/,http://127.0.0.1:1112/eureka/


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMiGEZvX-1587806443400)(en-resource://database/24752:1)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nNtrflxf-1587806443405)(en-resource://database/24748:1)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vLF5erbG-1587806443408)(en-resource://database/24750:1)]