三、SpringCloud之服务注册与发现组件--Eureka

1、Euraka介绍

Euraka是Netflix开发的一个服务注册与发现的子模块,是基于REST的服务框架,遵循AP原则

Eureka包含两个组件:Eureka Server和Eureka Client

Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册 。

Eureka github 地址: https://github.com/Netflix/eureka

官方文档:

http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-eureka-server

http://projects.spring.io/spring-cloud/spring-cloud.html#_service_discovery_eureka_clients

这里写图片描述

以下内容是基于上一节的工程,将服务注册到Eureka注册中心

源码下载:https://github.com/hnyydp/microservice

2、搭建Eureka服务注册中心

(1)创建maven module项目 microservice-eureka-server-5001,带上端口号,便于区分

pom.xml

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

(2)配置application.yml

server:
  context-path: /
  port: 5001       #eureka服务端的端口

eureka:
  instance:
    appname: localhost #eureka-server-5001.com             #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false     #false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址(单机)。
      defaultZone: http://${eureka.instance.appname}:${server.port}/eureka/

(3)编写启动类

//SpringCloud中,如果要开启某个组件,使用@EnableXXXX注解即可
@EnableEurekaServer   //开启Eureka服务,使得这个应用变为Euraka服务器
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3、将服务microservice-dept-provider-8001注册进Eureka注册中心

(1)在pom.xml中添加Eureka客户端依赖

        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

(2)配置application.yml

spring:
  application:
    name: microservice-dept  #服务名,便于区分
eureka:
  client: #将客户端服务注册到Eureka服务列表中
    service-url:
      defaultZone: http://localhost:5001/eureka
  #主机名称修改,不加默认localhost
  instance:
    instance-id: microservice-dept8001  #自定义服务名称信息
    prefer-ip-address: true #访问路径上会显示IP,当应用程序向eureka注册时,将使用其IP地址而不是其主机名。

(3)配置启动类DeptApplication.java

@EnableEurekaClient  //服务启动后会自动注册到Eureka注册中心中
@SpringBootApplication
public class DeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplication.class, args);
    }
}

这里写图片描述

4、info微服务信息完善

使用SpringCloud重要组件-Spring Boot Actuator来完善服务的定制信息

参考文档:
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#production-ready
https://cloud.tencent.com/developer/article/1059807

在服务提供者microservice-dept-provider-8001中加入Actuator依赖

    <!--actuator监控组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

microservice下加入解析配置插件(如果不需要自己获取可以不用配置)

<!-- 解析resources下配置信息插件 -->
    <build>
        <finalName>microservicecloud</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>$</delimit>  <!--以$分割-->
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

在服务提供者microservice-dept-provider-8001中配置yml

#定制微服务信息
info:
  app.name: microservice
  app.company: www.xxx.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

运行效果:

这里写图片描述

5、Eureka自我保护机制

服务注册到Eureka Server后,会维护一个心跳连接,告诉Eureka Server自己还活着。默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。如果出现低于的情况,Eureka Server会将当前实例注册信息保护起来,让这些实例不会过期。这样做会使客户端很容易拿到实际已经不存在的服务实例,会出现调用失败的情况。因此客户端要有容错机制,比如请求重试、断路器。

eureka.server.enableSelfPreservation=false. 关闭保护机制,会将不可用的实例删除

6、Euraka服务发现

对于注册进Eureka的微服务,可以通过服务发现来获取该服务的信息。对服务提供者来说,可以提供一个接口给消费者能获取该服务的信息。

代码示例:

在服务提供者microservice-dept-provider-8001中增加接口:

    @Autowired
    private DiscoveryClient discoveryClient;  //服务发现接口

    @GetMapping("/dept/discovery")
    public List<ServiceInstance> discovery(){
        List<String> list = discoveryClient.getServices();  //获取服务列表
        System.out.println("***获取服务列表:"+list);
        //返回当前实例信息
        return discoveryClient.getInstances("MICROSERVICE-DEPT");
    }   

7、Eureka集群配置

官方文档:

http://projects.spring.io/spring-cloud/spring-cloud.html#_high_availability_zones_and_regions

官方文档提供了一个两个实例的集群方法,通过配置文件增加端口,然后分别通过配置启动Eureka

java -jar microservice-eureka-server.jar --spring.profiles.active=peer1
java -jar microservice-eureka-server.jar --spring.profiles.active=peer2

application.yml (Two Peer Aware Eureka Servers)

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

这里为了更好的方便操作,直接复制3个Eureka服务端microservice-eureka-server-5001microservice-eureka-server-5002microservice-eureka-server-5003,然后分别修改对应的配置文件。

eureka:
  instance:
    appname: eureka-server-5001.com #localhost             #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己,因为本身应用就是注册中心
    fetch-registry: false     #false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址(单机)。
      #defaultZone: http://${eureka.instance.appname}:${server.port}/eureka/
      #集群配置,相互注册
      defaultZone: http://eureka-server-5002.com:5002/eureka/,http://eureka-server-5003.com:5003/eureka/

修改hosts配置文件,进行主机名称映射:
127.0.0.1 eureka-server-5001.com
127.0.0.1 eureka-server-5002.com
127.0.0.1 eureka-server-5003.com

修改服务提供者microservice-dept-provider-8001配置:

eureka:
  client: #将客户端服务注册到Eureka服务列表中
    service-url:
      #defaultZone: http://eureka-server-5001.com:5001/eureka  #单机
      defaultZone: http://eureka-server-5001.com:5001/eureka,http://eureka-server-5002.com:5002/eureka,http://eureka-server-5003.com:5003/eureka

分别启动Eureka服务:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值