当前位置:网站首页>微服务实战|原生态实现服务的发现与调用
微服务实战|原生态实现服务的发现与调用
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()方法进行接口的调用并获取返回信息。
这样就通过原生态的方式实现了服务的发现和调用。
拿到第一个实例进行接口的调用,显然没有达到部署多服务实例的目的,下一篇文章将带你实现一个自定义的负载均衡器,一起期待吧!
边栏推荐
- Qt的拖动事件
- kubernetes部署loki日志系统
- Tensorflow2 keras 分类模型
- MYSQL安装出现问题(The service already exists)
- Minecraft air Island service
- Hengyuan cloud_ Can aiphacode replace programmers?
- Redis sorted set data type API and application scenario analysis
- 以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
- QT drag event
- [staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the
猜你喜欢
WSL installation, beautification, network agent and remote development
队列管理器running状态下无法查看通道
微服务实战|熔断器Hystrix初体验
Talk about the secret of high performance of message queue -- zero copy technology
Flink - use the streaming batch API to count the number of words
Minecraft air Island service
Flink-使用流批一体API统计单词数量
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
队列的基本概念介绍以及典型应用示例
Installing Oracle database 19C RAC on Linux
随机推荐
Essay: RGB image color separation (with code)
C nail development: obtain all employee address books and send work notices
【Go实战基础】gin 如何设置路由
C# 调用系统声音 嘀~
Redis安装部署(Windows/Linux)
Avoid breaking changes caused by modifying constructor input parameters
Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
Pyspark de duplication dropduplicates, distinct; withColumn、lit、col; unionByName、groupBy
Analysis and solution of a classical Joseph problem
选择排序和插入排序
CSDN Q & A_ Evaluation
Redis sorted set data type API and application scenario analysis
【Go实战基础】gin 如何验证请求参数
Loadbalancer dynamically refreshes Nacos server
Hengyuan cloud_ Can aiphacode replace programmers?
【Go实战基础】gin 如何绑定与使用 url 参数
Select sort and insert sort
History of Web Technology
MYSQL安装出现问题(The service already exists)
Image transformation, transpose