服务注册中心的简单介绍和使用
服务注册中心一般原理说明:
- 服务提供者启动
- 服务提供者将相关服务信息主动注册到注册中心
- 服务消费者采用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
第二步

#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


服务提供者集群
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
创建消费者
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
<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;
}
}
消费者从注册中心获取实例

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;
}
}
父类依赖
<?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>
服务器提供者
<?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;
}
}


以上就是注册中心集群模式,服务提供者集群模式搭建,消费者从注册中心获取服务提供者
浙公网安备 33010602011771号