SpringCloud组件:Eureka的服务发现与消费

栏目: Java · 发布时间: 7年前

内容简介:在之前的章节我们已经把服务注册到消费我们只需要创建一个

在之前的章节我们已经把服务注册到 Eureka Server ,那么我们该怎么调用已经注册后的服务呢? 我们本章来简单的介绍我们具体该怎么调用 服务节点 请求内容。

本章目标

消费 Eureka 注册的 服务节点 的请求信息。

构建项目

我们只需要创建一个 服务节点项目 即可,因为服务 提供者 也是 消费者 ,然后将本项目注册到之前编写的 服务注册中心 ,下载文章 SpringCloud组件:搭建Eureka服务注册中心 源码运行即可。 我们使用 idea 开发 工具 创建一个 SpringBoot 项目,对应的选择 spring-boot-starter-webspring-cloud-starter-netflix-ribbonspring-cloud-starter-netflix-eureka-client 三个依赖, pom.xml 配置文件如下所示:

......
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>

<dependencies>
    <!--Web依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--ribbon-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
    <!--client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
......
复制代码

添加完依赖后我们需要对本项目进行配置,让本项目注册到服务中心,在之前的章节 SpringCloud组件:将微服务提供者注册到Eureka服务中心 有讲过,这里就不做过多的赘述。

配置Eureka客户端

打开 XxxApplication 入口类,添加 @EnableDiscoveryClient 注解,如下所示:

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudEurekaConsumerApplication {
 //...
}
复制代码

修改application.yml配置文件

下面我们修改 application.yml 配置文件,添加 Eureka Client 对应的配置信息,如下所示:

# 服务名称
spring:
  application:
    name: hengboy-spring-cloud-eureka-consumer
# 启动端口号
server:
  port: 20002
# Eureka 服务注册中心配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10000/eureka/
  # 配置优先使用IP地址注册服务
  instance:
    prefer-ip-address: true
复制代码

获取服务实例信息

如果你只是将服务注册到 服务注册中心 也就是 Eureka Server 上,到现在已经完全没有问题了,但是我们想要通过 服务名 ( spring.application.name )来获取 服务实例列表 该怎么操作呢?

本章内容涉及一点有关 Ribbon 的知识点,我们通过添加依赖 spring-cloud-starter-netflix-ribbon 就可以直接使用 RestTemplate 类进行发送 http请求 ,而且 RestTemnplate 可以直接使用 服务名 进行发送请求!!!

实例化RestTemplate

spring-cloud-starter-netflix-ribbon 依赖并没有为我们实例化 RestTemplate ,我们需要手动进行实例化,我采用 @Bean 方式进行实例化,在 XxxApplication 类内添加如下代码:

/**
 * 实例化RestTemplate对象实例
 *
 * @return
 */
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}
复制代码

在这里有个 @LoadBalanced 注解,我们后续章节会对它详细的讲解,博客搜索关键字 LoadBalanced 查询文章信息,不过如果你不添加并使用这个注解,你是没有办法通过 服务名 直接发送请求的,会出现错误信息。

了解DiscoveryClient

我们需要创建一个 发送请求 以及 请求消费Controller ,如下所示:

/**
 * 消费者控制器
 *
 * @author:于起宇 <p>
 * ================================
 * Created with IDEA.
 * Date:2018/9/29
 * Time:5:55 PM
 * 简书:http://www.jianshu.com/u/092df3f77bca
 * 码云:https://gitee.com/hengboy
 * GitHub:https://github.com/hengyuboy
 * ================================
 * </p>
 */
@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {
    /**
     * logger instance
     */
    static Logger logger = LoggerFactory.getLogger(ConsumerController.class);
    /**
     * 注入服务客户端实例
     */
    @Autowired
    private DiscoveryClient discoveryClient;
    /**
     * 注入restTemplate模板
     */
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 服务消费者业务逻辑方法
     * 该方法使用restTemplate访问获取返回数据
     *
     * @return
     */
    @RequestMapping(value = "/logic")
    public String home() {
        return "this is home page";
    }

    /**
     * 请求地址
     * 输出服务的基本信息
     */
    @RequestMapping(value = "/index")
    public void index() {
        discoveryClient.getInstances("hengboy-spring-cloud-eureka-consumer")
                .stream()
                .forEach(
                        instance -> {
                            logger.info("服务地址:{},服务端口号:{},服务实例编号:{},服务地址:{}", instance.getHost(), instance.getPort(), instance.getServiceId(), instance.getUri());
                            String response = restTemplate.getForEntity("http://" + instance.getServiceId() + "/consumer/logic", String.class).getBody();
                            logger.info("响应内容:{}", response);
                        }

                );
    }
}

复制代码

在上面代码中我们注入了 DiscoveryClient ,这是一个 接口类 ,具体该接口的实现类是什么要取决你使用的是什么 服务注册中心 ,我们本章采用的 Eureka 理所当然使用的是 Eureka 实现类,源码可以查看 org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient ,在 EurekaDiscoveryClient 内可以看到具体是怎么通过 服务名 获取实例的列表,部分源码如下所示:

@Override
public List<ServiceInstance> getInstances(String serviceId) {
    List<InstanceInfo> infos = this.eurekaClient.getInstancesByVipAddress(serviceId,
            false);
    List<ServiceInstance> instances = new ArrayList<>();
    for (InstanceInfo info : infos) {
        instances.add(new EurekaServiceInstance(info));
    }
    return instances;
}
复制代码

你如果对具体的源码执行流程感兴趣,可以使用断点来一步一步的观察。 在获取 ServiceInstance 服务实例后,可以得到实例的一些基本信息如:

  • serviceId :服务名称、服务的实例编号,也就是 spring.application.name 配置信息
  • host :注册该实例的 hostName
  • port :注册该实例的端口号,对应 server.port 配置信息
  • uri :服务地址
  • metadata :服务自定义的元数据 map 集合

请求转发流程

执行流程:我们在访问 /consumer/index 请求地址时,会通过 RestTemplate 转发请求访问 http://hengboy-spring-cloud-eureka-consumer/consumer/logic 地址并返回信息 this is home page

运行测试

我们的测试流程如下:

http://localhost:20002/consumer/index
this is home page

访问有多种形式,你可以浏览器直接访问地址,我通过 curl 命令来访问地址,打开 terminal 输入以下命令:

curl http://localhost:20002/consumer/index
复制代码

请求正常,查看控制台输出内容如下所示:

2018-10-04 15:23:36.333  INFO 29075 --- [io-20002-exec-5] c.y.c.h.s.e.consumer.ConsumerController  : 服务地址:192.168.1.75,服务端口号:20002,服务实例编号:HENGBOY-SPRING-CLOUD-EUREKA-CONSUMER,服务地址:http://192.168.1.75:20002
......
2018-10-04 15:23:36.748  INFO 29075 --- [io-20002-exec-5] c.y.c.h.s.e.consumer.ConsumerController  : 响应内容:this is home page
复制代码

总结

本章通过 Ribbon 简单的实现了服务节点的消费,通过 RestTemplate 发送请求来获取响应内容,需要注意的是我们并不是通过 IP : Port 的形式,而是通过 服务名 的形式发送请求,这都归功于 @LoadBalanced 这个注解,这个注解在讲解 Ribbon 时会详细的说明。

源码位置

有问题要问?

如果你有技术相关的问题想要咨询 恒宇少年 ,请去博客首页左侧导航栏,点击 知识星球 微信扫码加入我的星球。

与恒宇少年面对面

如果你喜欢 恒宇少年 的相关文章,那么就去微信公众号( 恒宇少年 )关注我吧!!! 当然你也可以去SpringCloud码云源码 项目底部扫描微信公众号二维码关注我,感谢阅读!!!

学习目录推荐

开源信息

这段时间一直在编写开源的相关框架,致力于公司使用的框架升级以及开源计划,将公司使用到的 工具 以及 插件 进行升级重构并且开源。

  • 代码生成器(Code-Builder) code-builder 代码生成器根据你提供的模板文件(目前支持 freemarker )自动生成实体类,可以很大很有效的提高开发效率。 Gitee地址gitee.com/hengboy/cod… Github地址github.com/hengyuboy/c…
  • 持久化框架(MyBatis-Enhance) mybatis-enhance 是一个对 mybatis 框架的增强封装,提供一系列的内部方法来完成单表数据的操作,多表数据提供 DSL 方式进行操作。 Gitee地址gitee.com/hengboy/myb… Github地址github.com/hengyuboy/m…
  • 自动分页插件 MyBatis-Pageable 是一款自动化分页的插件,基于 MyBatis 内部的插件 Interceptor 拦截器编写完成,拦截 Executor.query 的两个重载方法计算出分页的信息以及根据配置的数据库 Dialect 自动执行不同的查询语句完成总数量的统计。 Gitee地址gitee.com/hengboy/myb…

以上所述就是小编给大家介绍的《SpringCloud组件:Eureka的服务发现与消费》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Solve It

How to Solve It

Zbigniew Michalewicz、David B. Fogel / Springer / 2004-03-01 / USD 59.95

This book is the only source that provides comprehensive, current, and detailed information on problem solving using modern heuristics. It covers classic methods of optimization, including dynamic pro......一起来看看 《How to Solve It》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具