新建一个springboot项目,命名为eureka-server,spring-cloud版本为:Hoxton.SR5
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.0.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
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
server:port: 8761
eureka:instance:# eureka注册中心的实例名称hostname: localhostclient:# false表示不想注册中心注册自己register-with-eureka: false# false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:# 设置与Eureka Server交互的地址(查询和注册服务)defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:application:name: eureka-server
只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上添加。
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
server:port: 8080servlet:context-path: /provider
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:# 自定义服务名称信息instance-id: eureka-provider:8080#访问路径可以显示Ip地址prefer-ip-address: true
spring:application:# 对应的微服务名字(eureka会自动改为全大写)name: eureka-provider
info:app.name: study-springcloudcompany.name: www.xxx.combuild.artifactId: @project.artifactId@build.version: @project.version@
org.springframework.boot spring-boot-starter-actuator
study-springcloud src/main/resources true org.apache.maven.plugins maven-resources-plugin
info:app.name: study-springcloudcompany.name: www.fengfan.combuild.artifactId: @project.artifactId@build.version: @project.version@
@RestController
@RequestMapping("/api")
public class ProviderController {@Value("${server.port}")private String serverPort;@PostMapping("/sayHello")public String sayHello(String name) {return "我是服务端" + serverPort + ",你好" + name;}
}
@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
public class EurekaProviderApplication {public static void main(String[] args) {SpringApplication.run(EurekaProviderApplication.class, args);}
}
@Configuration
public class RestTemplateConfig {/*** @return org.springframework.web.client.RestTemplate* @description 注入一个可以进行负载均衡的Rest Temple用于服务问调用* @author fengfan* @date 2022/5/18 14:43*/@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}}
@RestController
@RequestMapping("/api")
public class ConsumerController {@Resourceprivate RestTemplate restTemplate;@PostMapping("/askHello")public String askHello(){MultiValueMap body = new LinkedMultiValueMap<>();body.add("name", "consumer8090");ResponseEntity responseEntity = restTemplate.postForEntity("http://EUREKA-PROVIDER/provider/api/sayHello", body, String.class);return responseEntity.getBody();}
}
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息,修改ProviderController内容:
@PostMapping("/discovery")public Object discovery(){List list = client.getServices();System.out.println(list);List serviceInstances = client.getInstances("EUREKA-PROVIDER");for (ServiceInstance serviceInstance: serviceInstances){System.out.println(serviceInstance);}return client;}@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient
public class EurekaProviderApplication {public static void main(String[] args) {SpringApplication.run(EurekaProviderApplication.class, args);}}
做一个本机的域名映射
修改defaultZone参数,添加多台eureka地址(注意不要将自己加入,分别添加其他两台)
eureka:instance:# eureka注册中心的实例名称hostname: localhostclient:# false表示不想注册中心注册自己register-with-eureka: false# false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务fetch-registry: falseservice-url:# 设置与Eureka Server交互的地址(查询和注册服务)defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
更改启动参数,启动不同的端口,进行模拟
Zookeeper保证了CP,Eureka保证了AP。
A:高可用
C:一致性
P:分区容错性
1.当向注册中心查询服务列表时,我们可以容忍注册中心返回的是几分钟以前的信息,但不能容忍直接down掉不可用。也就是说,服务注册功能对高可用性要求比较高,但zk会出现这样一种情况,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新选leader。问题在于,选取leader时间过长,30 ~ 120s,且选取期间zk集群都不可用,这样就会导致选取期间注册服务瘫痪。在云部署的环境下,因网络问题使得zk集群失去master节点是较大概率会发生的事,虽然服务能够恢复,但是漫长的选取时间导致的注册长期不可用是不能容忍的。
2.Eureka保证了可用性,Eureka各个节点是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点仍然可以提供注册和查询服务。而Eureka的客户端向某个Eureka注册或发现时发生连接失败,则会自动切换到其他节点,只要有一台Eureka还在,就能保证注册服务可用,只是查到的信息可能不是最新的。除此之外,Eureka还有自我保护机制,默认情况下,如果Eureka Server在一定时间内(默认90秒)没有接收到某个微服务实例的心跳,Eureka Server将会移除该实例。如果在15分钟内超过85%的实例没有正常的心跳,那么Eureka就认为客户端与注册中心发生了网络故障,此时会出现以下几种情况: