在 Vim 中搭建 NERDTree 文件树导航的完整教程
2026/10/30 7:51:39
在上一篇环境与工程搭建,我们远程调用时候写url写死了。
Stringurl="http://127.0.0.1:9090/product/"+orderInfo.getProductId();当更换服务器的时候,这个url是需要跟着变的。我们这里就需要使用注册中心来解决这个问题。
注册中心:维护⼀个服务列表,哪个机器上线了,哪个机器宕机了,这些信息都会⾃动更新到服务列表上,客⼾端拿到这个列表,直接进⾏服务调⽤即可。这个就是注册中⼼
注册中⼼主要有三种⻆⾊(以租房为例):
关系与工作内容:
分布式系统CAP三个属性是不可能同时满足的,系统间的⽹络又不能100%保证健康,服务⼜必须对外保证服务,因此Partition Tolerance(分区容错性)不可避免。那就只能在C和A中选择⼀个。也就是CP或者AP架构。
Eureka主要分为两个部分:
我们就使用上一篇文章环境与工程搭建的案例代码来搭建。
创建Eureka-server ⼦模块
pom中引⼊eureka-server依赖与项目构建插件
<!-- eureka-server依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- 项目构建插件 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>packagecom.cloud.eureka;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(EurekaServerApplication.class,args);}}server:port:10010spring:application:name:eureka-servereureka:instance:hostname:localhostclient:fetch-registry:false# 表⽰是否从Eureka Server获取注册信息,默认为true.因为这是⼀个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这⾥设置为falseregister-with-eureka:false# 表⽰是否将⾃⼰注册到Eureka Server,默认为true.由于当前应⽤就是Eureka Server,故⽽设置为false.service-url:# 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/把product-service 注册到eureka-server中。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>spring:application:name:product-serviceeureka:client:service-url:defaultZone:http://127.0.0.1:10010/eureka修改order-service,在远程调⽤时,从eureka-server拉取product-service的服务信息,实现服务发现
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>spring:application:name:order-serviceeureka:client:service-url:defaultZone:http://127.0.0.1:10010/eurekapackagecom.cloud.order.service;importcom.cloud.order.mapper.OrderMapper;importcom.cloud.order.model.OrderInfo;importcom.cloud.order.model.ProductInfo;importjakarta.annotation.Resource;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cloud.client.ServiceInstance;importorg.springframework.cloud.client.discovery.DiscoveryClient;importorg.springframework.cloud.netflix.eureka.EurekaServiceInstance;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;importjava.util.List;@Service@Slf4jpublicclassOrderService{@AutowiredprivateOrderMapperorderMapper;@AutowiredprivateRestTemplaterestTemplate;@ResourceprivateDiscoveryClientdiscoveryClient;publicOrderInfoselectOrderById(IntegerorderId){OrderInfoorderInfo=orderMapper.selectOrderById(orderId);//String url = "http://127.0.0.1:9090/product/"+ orderInfo.getProductId();//根据应⽤名称获取服务列表List<ServiceInstance>instances=discoveryClient.getInstances("product-service");//服务可能有多个, 获取第⼀个EurekaServiceInstanceinstance=(EurekaServiceInstance)instances.get(0);log.info(instance.getInstanceId());//拼接urlStringurl=instance.getUri()+"/product/"+orderInfo.getProductId();ProductInfoproductInfo=restTemplate.getForObject(url,ProductInfo.class);orderInfo.setProductInfo(productInfo);returnorderInfo;}}