当前位置:网站首页>微服务实战|原生态实现服务的发现与调用
微服务实战|原生态实现服务的发现与调用
2022-07-02 06:33:00 【_时光煮雨】
目录
相关推荐
前言
上一章中,我们介绍了Eureka注册中心及集群的搭建,这一节将介绍服务的发现和调用。注意,这个时候我们只有注册中心,并没有引入其他的组件,所以需要使用SpringCloud原生态的服务发现和调用的方式实现,循序渐进的带你走入微服务的世界。
上篇文章我们已经创建好了注册中心,这次我们需要创建一个服务提供者(provider)和一个服务消费者(consumer)两个项目。
一、服务提供者
- 新建Maven项目provider
- 引入项目依赖
<parent>
<groupId>com.cxy965</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>server:
port: 8002
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
fetch-registry: true- 创建启动类和服务接口,为了简便,暂时将服务接口放在了启动类,实际项目中,最好还是要放在controller中。
/**
* @Author:公众号:程序员965
* @create 2022-06-06
**/
@EnableEurekaClient
@SpringBootApplication
@RestController
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@GetMapping("/hello")
public String hello(String name) {
return "Hello "+name;
}
}- 启动验证一下,可以正常返回。

二、服务消费者
- 参考provider项目创建consumer项目
- 修改配置文件中的端口和应用名称为8003、consumer
- 创建启动类和服务消费代码
/**
* @Author:公众号:程序员965
* @create 2022-06-06
**/
@EnableEurekaClient
@SpringBootApplication
@RestController
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
DiscoveryClient discoveryClient;
@Autowired
RestTemplate restTemplate;
@GetMapping("/hello")
public String hello(String name) {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String returnInfo = restTemplate.getForObject("http://" + host + ":" + port + "/hello?name={1}", String.class, name);
return returnInfo;
}
}- 启动验证一下

可以看到,我们调用8003消费者服务,消费者服务又调用了8002服务提供者的接口,并正确返回了结果。
总结
我们来分析一下消费者代码,我们先创建了一个RestTemplate Bean实例,然后注入进来,同时注入discoveryClient对象。
在接口中,通过discoveryClient.getInstances("provider")方法获取注册到注册中心中的所有provider服务信息ServiceInstance集合,ServiceInstance其实是一个接口,真正的实现类是EurekaServiceInstance,通过查看EurekaServiceInstance的源码,我们发现它里面包含了注册中心中各服务的丰富的详细信息(如主机地址、端口号、实例id,应用名称、应用组名称等)。
我们先拿到第一个服务提供者的的ip和端口(集群部署情况下,可能会有多个实例),然后通过调用restTemplate.getForObject()方法进行接口的调用并获取返回信息。
这样就通过原生态的方式实现了服务的发现和调用。

拿到第一个实例进行接口的调用,显然没有达到部署多服务实例的目的,下一篇文章将带你实现一个自定义的负载均衡器,一起期待吧!
边栏推荐
- Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
- 分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
- Matplotlib剑客行——布局指南与多图实现(更新)
- WSL安装、美化、网络代理和远程开发
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
- Gocv boundary fill
- Qt的connect函数和disconnect函数
- Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
- Qt的右键菜单
- gocv图片裁剪并展示
猜你喜欢
![[go practical basis] how to install and use gin](/img/0d/3e899bf69abf4e8cb7e6a0afa075a9.png)
[go practical basis] how to install and use gin

以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化

ORA-12514问题解决方法
![[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)](/img/bf/2b0b9c640bdad2c55293f905a22055.jpg)
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案

查看was发布的应用程序的端口

Introduction to the basic concept of queue and typical application examples

整理秒杀系统的面试必备!!!

Qt——如何在QWidget中设置阴影效果

队列的基本概念介绍以及典型应用示例
随机推荐
gocv边界填充
Flink - use the streaming batch API to count the number of words
C nail development: obtain all employee address books and send work notices
Function ‘ngram‘ is not defined
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Matplotlib剑客行——容纳百川的艺术家教程
[go practical basis] how to install and use gin
Installing Oracle database 19C for Linux
libusb的使用
Minecraft air Island service
Find the node with the smallest value range in the linked list and move it to the front of the linked list
图像变换,转置
数构(C语言)——第四章、矩阵的压缩存储(下)
Kubernetes deploys Loki logging system
[go practical basis] how to set the route in gin
Leetcode sword finger offer brush questions - day 22
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
1、 QT's core class QObject
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Loadbalancer dynamically refreshes Nacos server