当前位置:网站首页>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 !
边栏推荐
- Count the number of various characters in the string
- oracle修改数据库字符集
- commands out of sync. did you run multiple statements at once
- Function ‘ngram‘ is not defined
- 查看was发布的应用程序的端口
- Finishing the interview essentials of secsha system!!!
- 微服务实战|手把手教你开发负载均衡组件
- There is a problem with MySQL installation (the service already exists)
- Openshift container platform community okd 4.10.0 deployment
- 【Go实战基础】gin 如何设置路由
猜你喜欢
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
汉诺塔问题的求解与分析
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Avoid breaking changes caused by modifying constructor input parameters
Cloud computing in my eyes - PAAS (platform as a service)
Matplotlib剑客行——初相识Matplotlib
Minecraft air Island service
查看was发布的应用程序的端口
WSL安装、美化、网络代理和远程开发
随机推荐
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
QT qtimer class
Mirror protocol of synthetic asset track
C# 调用系统声音 嘀~
Openshift container platform community okd 4.10.0 deployment
【Go实战基础】如何安装和使用 gin
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Analysis and solution of a classical Joseph problem
CSDN Q & A_ Evaluation
Minecraft install resource pack
C# 将网页保存为图片(利用WebBrowser)
Select sort and insert sort
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
Finishing the interview essentials of secsha system!!!
Npoi export word font size correspondence
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
gocv opencv exit status 3221225785
C nail development: obtain all employee address books and send work notices