1、创建EurekaServer
使用IDEA对EurekaServer项目进行创建。
点击new project,选择默认脚手架,开始创建
创建项目名称,可自行填写
选择创建Spring Cloud Discovery,选择Eureka Server,完成搭建。
搭建完成后,需要在启动类上增加注解:@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
}
}
完成搭建后,可以启动项目,访问默认地址:http://localhost:8080/
在启动时,可以看到项目在抛出错误,这是因为eureka在启动时,也会将自己注册到服务上,在没有指定地址时,肯定会因为找不到地址而无法连接。
在配置文件中添加服务名,服务端口,服务地址:
spring.application.name=spring-cloud-eureka-server
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8081/eureka
这样在Eureka就可以找到对应的服务名称
也可以禁止该服务注册到Eureka上:
eureka.client.register-with-eureka=false
不去EurekaServer获取服务地址
eureka.client.fetch-registry=false
2、其他服务注册与发现
其他服务想要注册到Eureka上,需要在服务的配置文件上配置Eureka地址。
在创造其他服务的时候,需要勾选web服务:
以及eureka client:
搭建好脚手架之后,需要将对应的eureka服务地址写到配置文件上:
spring.application.name=spring-cloud-provider-service
server.port=8084
eureka.client.service-url.defaultZone=http://localhost:8081/eureka
spring.application.name=spring-cloud-user-service
server.port=8083
eureka.client.service-url.defaultZone=http://localhost:8081/eureka
在product服务中增加方法:
@RestController
public class ProductService {
@GetMapping("/product/{id}")
public String findById(@PathVariable("id") int id) {
System.out.println(id);
return "SUCCESS";
}
}
在user服务中增加调用product的方法:
@RestController
public class UserService {
@Autowired
RestTemplate restTemplate;
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@GetMapping("/user/{id}")
public String findById(@PathVariable int id) {
System.out.println(id);
return restTemplate.getForObject("http://spring-cloud-product-service/product/" + id, String.class);
}
}
配置完成,先启动eureka服务,再启动其他服务,这样其他服务就会注册到对应的eureka服务上,并且输出日志:
此时调用http://localhost:8083/user/1
就会发现,product服务也会被一起调用,这样就完成了服务的注册与发现。
3、高可用集群搭建
创建一个和第一个Eureka一样的项目,更改配置文件:
spring.application.name=spring-cloud-eureka-server-replica
server.port=8080
eureka.client.service-url.defaultZone=http://localhost:8081/eureka
spring.application.name=spring-cloud-eureka-server
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8080/eureka
更改完成后,启动项目,就会发现这两个服务已经互相注册。
将其他两个项目的配置文件内的Eureka服务地址增加为两个,用逗号分隔开
eureka.client.service-url.defaultZone=http://localhost:8080/eureka,http://localhost:8081/eureka
启动两个服务,可以看到,所有服务都将被注册上去:
这样高可用集群就搭建完成了。
4、Eureka服务自我保护机制
Eureka服务在运行期间会将心跳失败比例在15分钟以内低于85%的客户端实例判断为网络故障,那么Eureka会将这些实例保护起来,使它们不会因为过期被剔除。
这样做的目的是为了减少网络不稳定或网络分区的情况下,Eureka Server会将健康服务踢下线的问题。
一旦开启自我保护机制,Eureka Server将不再从注册列表中移除因为长时间没有收到心跳导致应该被踢出的过期服务。Eureka Server仍然能够接收新服务的注册和查询请求,但是不会被同步到其他节点上,保证当前节点依然可用。
Renews threshold:eureka期待收到客户端实例总数
Renews (last min):eureka最后一分钟实际上收到的实例总数
当Renews (last min)小于Renews threshold的数量,也就是实际总数小于期待总数时,将会触发eureka的自我保护机制,即红字部分。
Renews threshold的计算方法是服务总是 * 每分钟续约数量(60s/客户端的续约间隔)* 自我保护续约百分比阈值因子
这个阈值是动态变化的:
- Eureka Server启动的时候
- registry 服务注册的时候
- 取消注册
- 每15分钟更新一次
自我保护机制可以被关闭,需要在配置文件中增加:
eureka.server.enable-self-preservation=false
也可以将对应的触发阈值调低:
eureka.server.renewal-percent-threshold=0.5
也可以让Eureka Server不再注册到自己上,降低服务个数,也就降低了自我保护机制的触发概率