内容简介:在之前的章节我们已经把服务注册到消费我们只需要创建一个
在之前的章节我们已经把服务注册到 Eureka Server
,那么我们该怎么调用已经注册后的服务呢?
我们本章来简单的介绍我们具体该怎么调用 服务节点
请求内容。
本章目标
消费 Eureka
注册的 服务节点
的请求信息。
构建项目
我们只需要创建一个 服务节点项目
即可,因为服务 提供者
也是 消费者
,然后将本项目注册到之前编写的 服务注册中心
,下载文章 SpringCloud组件:搭建Eureka服务注册中心
源码运行即可。
我们使用 idea
开发 工具 创建一个 SpringBoot
项目,对应的选择 spring-boot-starter-web
、 spring-cloud-starter-netflix-ribbon
、 spring-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
时会详细的说明。
源码位置
- SpringBoot配套源码地址:访问码云查看源码、 访问GitHub查看源码
-
SpringCloud配套源码地址(
本章源码在这): 访问码云查看源码 , 访问GitHub查看源码
有问题要问?
如果你有技术相关的问题想要咨询 恒宇少年
,请去博客首页左侧导航栏,点击 知识星球
微信扫码加入我的星球。
与恒宇少年面对面
如果你喜欢 恒宇少年
的相关文章,那么就去微信公众号( 恒宇少年
)关注我吧!!!
当然你也可以去SpringCloud码云源码 项目底部扫描微信公众号二维码关注我,感谢阅读!!!
学习目录推荐
- SpringCloud相关系列文章访问: www.jianshu.com/p/64e4aaada…
- SpringBoot相关系列文章请访问: www.jianshu.com/p/9a08417e4…
- QueryDSL相关系列文章请访问: www.jianshu.com/p/99a5ec5c3…
- SpringDataJPA相关系列文章请访问: www.jianshu.com/p/615ed9c1f…
开源信息
这段时间一直在编写开源的相关框架,致力于公司使用的框架升级以及开源计划,将公司使用到的 工具
以及 插件
进行升级重构并且开源。
-
代码生成器(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的服务发现与消费》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 服务发现组件Netflix Eureka
- Spring Cloud Alibaba教程:使用Nacos作为服务注册发现组件
- 分布式中几种服务注册与发现组件的原理与比较
- Kubernetes首个严重安全漏洞发现者,谈发现过程及原理机制
- 服务发现之Consul
- 服务发现-注册中心概述
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
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》 这本书的介绍吧!