eureka的运行机制
注册-一次次反复连接eureka,直到连接成功
拉取-每隔30秒从eureka拉取一次注册表,更新注册表信息
心跳-每30秒向eureka发送一次心跳,3次收不到心跳eureka会删除这个服务
自我保护模式-特殊情况,由于网络不稳定15分钟内85%服务出现异常
-保护模式不会删除注册信息
-网络恢复后,自动退出保护模式
-开发测试期间,建议关闭保护模式
eureka
服务端
依赖:eureka server
#eureka-yml
spring:
application:
name: order-service
server:
port: 2001
eureka:
server:
enable-self-preservation: false
instance:
hostname: eureka1 #集群服务器靠主机名区别
#不注册,不拉取
client:
register-with-eureka: false
fetch-registry: false
修改host
127.0.0.1 eureka1
客户端
依赖:eureka discovery client
#eureka-yml
spring:
application:
name: item-service
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka
<===
eureka 高可用
–spring.profiles.active=eureka2 --server.port=2002
–spring.profiles.active=eureka1 --server.port=2001
application-eureka1.yml
eureka:
instance:
hostname: eureka1 #集群服务器靠主机名区别
#不注册,不拉取 --spring.profiles.active=eureka2 --server.prot=2002
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka2:2002/eureka
其它客户端修改 defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
===>
RestTemplate
RestTemplate 是SpringBoot提供的一个Rest远程调用工具,类似httpclient
ribbon
SpringCloud提供工具ribbon 提供了负载均衡和重试功能
—RestTemplate-提供远程调用
启动类加
/*
可以放在启动类中,
或者也可以放在自定义的自动配置类
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
controller----
@Autowired
private RestTemplate rt;
@GetMapping("/item-service/{orderId}")
public JsonResult<List> getItems(@PathVariable String orderId){
return rt.getForObject(“http://localhost:8001/{1}”,JsonResult.class,orderId);
}
@PostMapping("/item-service/decreaseNumber")
public JsonResult<?> decreaseNumber(@RequestBody List items){
return rt.postForObject(“http://localhost:8001/decreaseNumber”,items,JsonResult.class);
}
dubbo 默认端口20880
dubbo直接连接service
springcloud 直接连接controller
-------------------ribbon增强功能–
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
“http://user-service/{1}/score?score={2}”(路径中填写名称)
------spring-retry(重试)
org.springframework.retry
spring-retry
ribbon:
MaxAutoRetriesNextServer: 2 //切换两次
MaxAutoRetries: 1 //重试一次
OkToRetryOnAllOperations: true
添加降级
1.添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.启动类添加注解
@SpringBootApplication
3.使用
在指定方法上添加
@HystrixCommand(fallbackMethod = “getOrderFB”)
hystrix超时 > ribbon超时
#hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 500 #默认降级时间 1000
springboot 提供了actuator工具
management:
endpoints:
web:
exposure:
include: hystrix.stream #"*"
http://localhost:3001/actuator
搭建hystrix dashboard(独立)
1.hystrix dashboard依赖
2.yml
3.添加
访问 http://localhost:3001/actuator/hystrix.stream
Apache24\bin>ab -n 20000 -c 1 http://localhost:3001/user-service/35
—vm–
1.关闭网络
systemctl stop NetworkManager
systemctl disable NetworkManager
2.自动获取ip
修改网卡配置文件
/etc/sysconfig/network-scripts/ifcfg-ens33