当前位置:网站首页>Actual combat of microservices | discovery and invocation of original ecosystem implementation services
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
2022-07-02 09:10:00 【_ Time boiled the rain】
Catalog
Related to recommend
Last one Eureka Registration center and cluster construction
Preface
In the last chapter , We introduced Eureka Establishment of registration center and cluster , This section will introduce the discovery and invocation of services . Be careful , At this time, we only have a registration center , No other components have been introduced , So we need to use SpringCloud The original way of service discovery and invocation , Take you step by step into the world of micro Services .
In the last article, we have created a registration center , This time we need to create a service provider (provider) And a service consumer (consumer) Two projects .
One 、 Service providers
- newly build Maven project provider
- Introduce project dependencies
<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
- Create startup classes and service interfaces , For simplicity , Temporarily put the service interface in the startup class , In actual projects , It's better to put it in controller in .
/**
* @Author: official account : The programmer 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;
}
}
- Start to verify , Can return to .
Two 、 Serving consumers
- Reference resources provider Project creation consumer project
- Modify the port and application name in the configuration file to 8003、consumer
- Create startup class and service consumption code
/**
* @Author: official account : The programmer 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;
}
}
- Start to verify
You can see , We call 8003 Consumer services , The consumer service calls again 8002 Service provider's interface , And returned the result correctly .
summary
Let's analyze the consumer code , Let's create a RestTemplate Bean example , Then pour in , Inject at the same time discoveryClient object .
In the interface , adopt discoveryClient.getInstances("provider") Method to get all the provider Service information ServiceInstance aggregate ,ServiceInstance It's actually an interface , The real implementation class is EurekaServiceInstance, By looking at EurekaServiceInstance Source code , We found that It contains a wealth of detailed information about the services in the registry ( Such as host address 、 Port number 、 example id, apply name 、 Application group name, etc ).
Let's get the first service provider's ip And port ( In the case of cluster deployment , There may be multiple instances ), And then by calling restTemplate.getForObject() Method to call the interface and get the return information .
In this way, the discovery and invocation of services are realized in the original way .
Get the first instance and call the interface , Obviously, the goal of deploying multiple service instances is not achieved , The next article will Take you to implement a custom load balancer , Let's hope together !
边栏推荐
- gocv图片裁剪并展示
- C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- Judge whether it is Sudoku
- Leetcode sword finger offer brush questions - day 22
- Minecraft air Island service
- Introduction to the basic concept of queue and typical application examples
- Multi version concurrency control mvcc of MySQL
- How to realize asynchronous programming in a synchronous way?
- Image transformation, transpose
猜你喜欢
《统计学习方法》——第五章、决策树模型与学习(上)
微服务实战|手把手教你开发负载均衡组件
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
C language - Blue Bridge Cup - 7 segment code
Solution and analysis of Hanoi Tower problem
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
C4D quick start tutorial - Chamfer
查看was发布的应用程序的端口
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
随机推荐
【Go实战基础】如何安装和使用 gin
Talk about the secret of high performance of message queue -- zero copy technology
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
C4D quick start tutorial - Chamfer
Sentinel reports failed to fetch metric connection timeout and connection rejection
[go practical basis] how to bind and use URL parameters in gin
C# 调用系统声音 嘀~
CSDN Q & A_ Evaluation
AMQ6126问题解决思路
将一串数字顺序后移
Cartoon rendering - average normal stroke
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
"Redis source code series" learning and thinking about source code reading
[go practical basis] how to set the route in gin
Using recursive functions to solve the inverse problem of strings
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
队列的基本概念介绍以及典型应用示例
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”