微服务:整合 Spring cloud Eureka - 服务发现 DiscoveryClient

一、前言

  本文主要是向大家简单介绍如何使用 DiscoveryClient发现Eureka微服务注册中心的服务信息。

  前面几篇文章向大家介绍了如何搭建Eureka Server 注册中心,以及如何将服务注册到Eureka微服务注册中心进行管理。现在只差一个服务消费者,就可以构建出一个简单的微服务框架。

二、服务发现

1、项目结构

 2、父pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>spring-cloud-register</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>demo-register</module>
        <module>demo-service-provider</module>
        <module>demo-service-consumer</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.2.4.RELEASE</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>
</project>

2、demo-service-consumer的pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-register</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- eureka-client 服务注册与发现 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- eureka-client 服务注册与发现 -->

        <!-- ribbon 负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <!-- ribbon 负载均衡 -->

    </dependencies>

</project>

3、demo-service-consumer的启动类:ServiceConsumerApplication

package com.demo.service.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

4、demo-service-consumer : application.yml

server:
  port: 8201

spring:
  application:
    name: demo-service-consumer

eureka:
  instance:
    lease-renewal-interval-in-seconds: 20
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    registry-fetch-interval-seconds: 10
    serviceUrl:
      defaultZone: http://localhost:8001/register/eureka/

上述配置指定了应用端口号为8201,服务名称为demo-service-consumer(别的微服务可以通过这个名称从注册中心获取demo-service-consumer提供的服务),剩下的为Eureka相关配置,含义如下:

  1、eureka.instance.lease-renewal-interval-in-seconds,向Eureka 服务端发送心跳的间隔时间,单位为秒,用于服务续约。这里配置为20秒,即每隔20秒向febs-register发送心跳,表明当前服务没有宕机;
  2、eureka.client.register-with-eureka, 为true时表示将当前服务注册到Eureak服务端;
  3、eureka.client.fetch-registry,为true时表示从Eureka 服务端获取注册的服务信息;
  4、eureka.client.instance-info-replication-interval-seconds,新实例信息的变化到Eureka服务端的间隔时间,单位为秒;
  5、eureka.client.registry-fetch-interval-seconds,默认值为30秒,即每30秒去Eureka服务端上获取服务并缓存,这里指定为10秒的原因是方便开发时测试,实际可以指定为默认值即可;
  6、eureka.client.serviceUrl.defaultZone,指定Eureka服务端地址。

5、demo-service-consumer : DiscoveryServiceController

package com.demo.service.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DiscoveryServiceController {

    @Autowired
    private DiscoveryClient discoveryClient;


    @ResponseBody
    @GetMapping("/getInstances/{serviceId}")
    public List<ServiceInstance> getInstances(@PathVariable("serviceId") String serviceId) {

        List<ServiceInstance> list = discoveryClient.getInstances(serviceId);

        System.out.println("------------------------------------");
        if (list != null && list.size() > 0) {
            for (ServiceInstance instance : list) {
                System.out.println("");
                System.out.println("******************************");
                System.out.println("服务实例信息:");
                System.out.println("服务 ServiceId:" + instance.getServiceId());
                System.out.println("服务 Host:" + instance.getHost());
                System.out.println("服务 Port:" + instance.getPort());
                System.out.println("服务 Uri:" + instance.getUri().toString());
                System.out.println("服务 Metadata:" + instance.getMetadata().toString());
                System.out.println("******************************");
                System.out.println("");
            }
        } else {
            System.out.println("未找到serviceId:" + serviceId + "的实例");
        }

        System.out.println("------------------------------------");
        return list;
    }


    @ResponseBody
    @GetMapping("/getServices")
    public List<String> getServices() {
        List<String> list = discoveryClient.getServices();
        System.out.println("------------------------------------");
        if (list != null && list.size() > 0) {
            for (String serviceId : list) {
                System.out.println("服务Id : " + serviceId);
            }
        } else {
            System.out.println("注册中心无服务实例");
        }

        System.out.println("------------------------------------");
        return list;
    }
}

三、运行分析 

1、第一步启动Eureka注册中心 - demo-register,可以参考《微服务:整合 Spring Cloud Eureka - 注册中心 Eureka Server

2、第二步启动服务提供者 - demo-service-provider,可以参考 《微服务:整合 Spring cloud Eureka - 服务注册 Eureka Client 》

     我们可以启动两个 demo-service-provider实例,以便观察效果

3、第三步启动服务消费者  - demo-service-consumer。

4、打开Eureka注册中心:http://localhost:8001/register/

 我们可以开到Eureka注册中心现在已经注册了三个实例,其中demo-service-provider两个,demo-service-consumer一个。

  localhost:demo-service-provider:8101

  localhost:demo-service-provider:8102

  localhost:demo-service-consumer:8201

6、打印服务列表,在浏览器中打开:http://localhost:8201/getServices

  

  我们可以在浏览器中查看到返回的是一段Json数据:

[
  "demo-service-provider",
  "demo-service-consumer"
]

Idea的控制台输出为:

 

 也就是说,Eureka注册中心只有两个ServiceId,但是却有三个服务实例。因为demo-service-provider启动了两个实例,那么ServiceId=demo-service-provider的实例就有两个。

3、打印Eureka注册中心中ServiceId=demo-service-provider的服务实例信息,打开浏览器输入:http://localhost:8201/getInstances/demo-service-provider

idea的控制台输出为:

 

四、总结

  以上就是DiscoveryClient的用法,我们可以通过DiscoveryClient从Eureka注册中心获取服务提供者的服务实例信息。例如我们通过DiscoveryClient发现demo-service-provider的实例有两个,也就是说有两个url地址:localhost:8101、localhost:8102。通过这两个地址,服务消费者可以通过负载均衡策略选择其中一个服务地址进行调用。

  下一篇将会介绍服务消费以及微服务中间件:负载均衡- ribbon

猜你喜欢

转载自www.cnblogs.com/yansg/p/12535655.html