服务注册中心的简单介绍和使用
服务注册中心一般原理说明:
- 服务提供者启动
- 服务提供者将相关服务信息主动注册到注册中心
- 服务消费者采用poll模式主动拉取(定时拉取)可用的服务提供者清单
- 服务消费者也可以直接调用服务提供者
注册中心需要完成服务提供者的健康监控,当发现服务提供者失效时需要及时剔除
注册中心对比
C:数据一致性
A:高可用性
P:分区容错性(必须满足的)
服务注册中心组件eureka
eureka基础架构
eureka的服务端:eureka注册中心
eureka的客户端:服务消费者,服务提供者
eureka项目启动出现如下问题
Caused by: java.lang.NoClassDefFoundError: org/springframework/cloud/context/environment/EnvironmentChangeEvent
子项目中缺少如下
使用https://start.spring.io/actuator/info,找到springboot和springcloud对应的版本
eureka配置集群方案
第一步修改本地电脑host
127.0.0.1 LagouCloudEurekaServerA
127.0.0.1 LagouCloudEurekaServerB
第二步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #eureka server服务端口号 server.port= 8761 #应用名称 spring.application.name=eureka-service #当前eureka的实例名 eureka.instance.hostname=LagouCloudEurekaServerA #配置eureka客户端交互的地址 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerB:8762/eureka #是否注册到eureka eureka.client.register-with-eureka= true #不需要获取服务提供者信息 eureka.client.fetch-registry= true spring.main.allow-bean-definition-overriding= true logging.level.com.eureka=info logging.level.web=info spring.devtools.add-properties= false #eureka server服务端口号 server.port= 8762 #应用名称 spring.application.name=first-eureka #当前eureka的实例名 eureka.instance.hostname=LagouCloudEurekaServerB #配置eureka客户端交互的地址 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerA:8761/eureka #是否注册到eureka eureka.client.register-with-eureka= true #不需要获取服务提供者信息 eureka.client.fetch-registry= true spring.main.allow-bean-definition-overriding= true logging.level.com.eureka=info logging.level.web=info spring.devtools.add-properties= false |
服务提供者集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server.port= 1000 spring.application.name=first-provider #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka logging.level.first.provider=debug logging.level.web=debug spring.devtools.add-properties= false server.port= 1001 spring.application.name=first-provider #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka logging.level.second.provider=debug logging.level.web=debug spring.devtools.add-properties= false |
创建消费者
1 2 3 4 5 6 7 8 9 10 | server.port= 2000 spring.application.name=one-consumer #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka logging.level.one.consumer=debug logging.level.web=debug spring.devtools.add-properties= false |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <dependencies> <!--客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> package one.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author yourheart * @Description * @create 2022-04-04 14:40 */ @SpringBootApplication @EnableDiscoveryClient public class OneConsumerApplication { public static void main(String[] args) { SpringApplication.run(OneConsumerApplication. class ,args); } } package one.consumer.config; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; /** * @author yourheart * @Description * @create 2022-04-04 15:05 */ @Component public class RestTemplateConfig { @Bean public RestTemplate getRestTemplste(){ return new RestTemplate(); } } package one.consumer.controller.front; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; /** * @author yourheart * @Description * @create 2022-04-05 22:38 */ @Controller public class ConsumerController { @Autowired private RestTemplate restTemplate; @RequestMapping ( "/queryIpCity/{ip}" ) @ResponseBody public String queryIpCity( @PathVariable String ip){ String forObject = restTemplate.getForObject( "http://localhost:1000/queryIpCity/" + ip, String. class ); return "消费调用返回结果:" +forObject; } } |
消费者从注册中心获取实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package one.consumer.controller.front; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; import java.util.List; /** * @author yourheart * @Description * @create 2022-04-05 22:38 */ @Controller public class ConsumerController { @Autowired private DiscoveryClient discoveryClient; @Autowired private RestTemplate restTemplate; /** * 从注册中心获取服务实例 * @param ip * @return */ @RequestMapping ( "/queryIpCityFromEureka/{ip}" ) @ResponseBody public String queryIpCityFromEureka( @PathVariable String ip){ List<ServiceInstance> instances = discoveryClient.getInstances( "FIRST-PROVIDER" ); ServiceInstance instance = instances.get( 0 ); String host = instance.getHost(); int port = instance.getPort(); String url= "http://" +host+ ":" +port+ "/queryIpCity/" + ip; String forObject = restTemplate.getForObject(url, String. class ); return "从注册中心获取服务实例,消费调用返回结果:" +forObject; } } |
父类依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>pingan.parent</groupId> <artifactId>pingan-parent</artifactId> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <modules> <module>eureka-service</module> <module>first-eureka</module> <module>first-provider</module> <module>second-provider</module> <module>one-consumer</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 0 .RELEASE</version> <relativePath/> </parent> <!--这里是为了统一控制版本--> <dependencyManagement> <dependencies> <!--引入springcloud版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> </project> |
服务器提供者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>pingan-parent</artifactId> <groupId>pingan.parent</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <groupId>first.provider</groupId> <artifactId>first-provider</artifactId> <dependencies> <!--客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> server.port= 1000 spring.application.name=first-provider #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http: //LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka logging.level.first.provider=debug logging.level.web=debug spring.devtools.add-properties= false package first.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author yourheart * @Description * @create 2022-04-02 23:05 */ @SpringBootApplication @EnableDiscoveryClient public class FirstProviderApplication { public static void main(String[] args) { SpringApplication.run(FirstProviderApplication. class ,args); } } package first.provider.controller.front; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author yourheart * @Description * @create 2022-04-02 23:08 */ @Controller public class FirstController { @RequestMapping ( "/queryIpCity/{ip}" ) @ResponseBody public String queryIpCity( @PathVariable String ip){ return ip; } } |
以上就是注册中心集群模式,服务提供者集群模式搭建,消费者从注册中心获取服务提供者
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异