- Eureka是一个基于REST的服务,主要在AWS云中使用,定位服务来进行中间层服务器的负载均衡和故障转移。
- SpringCloud封装了Netflix公司开发的Eureka模块来实现服务注册和发现。Eureka采用了C-S的设计架构。EurekaServer作为服务注册功能的服务器, 它是服务注册中心。而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server,并维持心跳连接。这样系统的维护人员就可以通过Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud的一些其他模块(比如Zuul)就可以通过Eureka Server来发现系统中的其他微服务,并执行相关的逻辑。
- Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
新建一个springboot项目,命名为eureka-server,spring-cloud版本为:Hoxton.SR5
添加依赖- parent的pom配置
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.xx study-springcloud 0.0.1-SNAPSHOT study-springcloud pom Demo project for Spring Boot 1.8 Hoxton.SR5 eureka org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
- Eurake Server依赖pom 。主要依赖如下:
yml配置org.springframework.cloud spring-cloud-starter-netflix-eureka-server
server:
port: 8080
servlet:
context-path: /provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-provider
启动类
只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上添加。
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- Eureka Server既可以独立部署,也可以集群部署。在集群部署的情况下,Eureka Server间会进行注册表信息同步的操作,这时被同步注册表信息的Eureka Server将会被其他同步注册表信息的Eureka Server称为peer。
- 上述配置中的service-url指向的注册中心为实例本身,通常来讲,一个Eureka Server也是Eureka Client,它会尝试注册自己,所以需要至少一个注册中心的URL来定位对等点peer。如果不提供这样一个注册端点,注册中心也能工作,但是会在日志中打印无法向peer注册自己的信息。在独立(Standalone)Eureka Server的模式下,Eureka Server一般会关闭作为客户端注册自己的行为。
- Eureka Server和Eureka Client之间的联系主要通过心跳的方式实现,心跳(Heartbeat)即Eureka Client定时向Eureka Server汇报本服务实例当前的状态,维护本服务实例在注册表中有效性。
- Eureka Serve需要随时维持最新的服务实例信息,所以在注册表中的每个服务实例都需要定期发送心跳到Server中以使自己的注册保持最新的状态(数据一般直接保存在内存中)。为了避免Eureka Client在每次服务间调用都向注册中心请求依赖服务实例的信息,Eureka Client将定时从Eureka Server中拉取注册表中的信息,并将这些信息缓存到本地,用于服务发现。
yml配置org.springframework.cloud spring-cloud-starter-netflix-eureka-client
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-provider
提供服务接口
@RestController
@RequestMapping("/api")
public class PoroviderController {
@PostMapping("/sayHello")
public String sayHello(String name){
return "hello:" + name;
}
}
启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
搭建Eureka服务调用者
添加依赖
yml配置org.springframework.cloud spring-cloud-starter-netflix-eureka-client
server:
port: 8081
servlet:
context-path: /consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eureka-consumer
消费者接口
@RestController
@RequestMapping("/api")
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@PostMapping("/askHello")
public String askHello(){
MultiValueMap body = new LinkedMultiValueMap<>();
body.add("name", "consumer8081");
ResponseEntity responseEntity = restTemplate.postForEntity("http://EUREKA-PROVIDER/provider/api/sayHello", body,String.class);
return responseEntity.getBody();
}
}
RestTemplate配置
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
测试
项目地址:git@github.com:fengfangithub/study-springcloud.git



