当前位置:网站首页>微服务实战|原生态实现服务的发现与调用
微服务实战|原生态实现服务的发现与调用
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()方法进行接口的调用并获取返回信息。
这样就通过原生态的方式实现了服务的发现和调用。

拿到第一个实例进行接口的调用,显然没有达到部署多服务实例的目的,下一篇文章将带你实现一个自定义的负载均衡器,一起期待吧!
边栏推荐
- Solution and analysis of Hanoi Tower problem
- Sentinel reports failed to fetch metric connection timeout and connection rejection
- C# 调用系统声音 嘀~
- 【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
- AMQ6126问题解决思路
- Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
- Gocv image cutting and display
- Synchronize files using unison
- History of Web Technology
- 【Go实战基础】gin 如何绑定与使用 url 参数
猜你喜欢

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

Move a string of numbers backward in sequence

Synchronize files using unison

知识点很细(代码有注释)数构(C语言)——第三章、栈和队列

Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`

Introduction to the basic concept of queue and typical application examples

C4D quick start tutorial - Chamfer
![[go practical basis] how can gin get the request parameters of get and post](/img/fd/66074d157d93bcf20a5d3b37da9b3e.png)
[go practical basis] how can gin get the request parameters of get and post

一篇详解带你再次重现《统计学习方法》——第二章、感知机模型

Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
随机推荐
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
【Go实战基础】如何安装和使用 gin
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Qt的connect函数和disconnect函数
Flink - use the streaming batch API to count the number of words
【Go实战基础】gin 如何绑定与使用 url 参数
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
C#钉钉开发:取得所有员工通讯录和发送工作通知
Gocv split color channel
libusb的使用
微服务实战|熔断器Hystrix初体验
Tensorflow2 keras classification model
Use of libusb
Mirror protocol of synthetic asset track
C4D quick start tutorial - Chamfer
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
CSDN Q & A_ Evaluation
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
Redis zadd导致的一次线上问题排查和处理