Eurek进行服务的注册与发现(请看之前的笔记[Spring Cloud Eureka搭建注册中心])
ribbon进行RestTemplate负载均衡策略(下期写ribbon实现负载均衡以及手写负责均衡)
hystrix 实现熔断机制以及通过dashboard查看熔断信息(有时间写hystrix dashboard详解)
项目结构如下(不包含Eureka服务注册与发现),另外部署
image.pngspring-cloud-study-provider 作为服务提供者将服务注册到Eureka集群
spring-cloud-study-api 作为项目api提供基础类库支持
spring-cloud-study-consumer 作为服务消费者从Eureka集群获取提供者信息,并进行消费,集成了Eureka,ribbon, hystrix, hystrix dashboard
Eureka主要实现服务的注册与发现(请看之前的笔记[Spring Cloud Eureka搭建注册中心]),这里不在重复
消费端eureka配置
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client1.com:7002/eureka/,http://eureka-client2.com:7003/eureka/
服务提供方eureka配置
eureka:
client:
service-url:
defaultZone: http://eureka-server.com:7001/eureka/,http://eureka-client1.com:7002/eureka/,http://eureka-client2.com:7003/eureka/
register-with-eureka: true
fetch-registry: false
instance:
instance-id: spring-cloud-study-provider # 调用服务时需要此名称(全部大写)
prefer-ip-address: true
ribbon实现负载均衡,默认采用:轮询。
引用jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
在RestTemplat加入LoanBalance注释即可
@Configuration
public class RestConfigBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
hystrix 实现熔断机制以及通过dashboard进行监控
引入jar依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>
启动hystrix有及hystrix dashboard
@SpringBootApplication
@EnableDiscoveryClient #启用eureak服务发现
@EnableHystrix # 启用hystrix熔断
@EnableHystrixDashboard # 启用hystrix dashboard服务监控
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
重要步骤
先启动eureka服务器,这边启动三台,模拟集群, 访问
http://eureka-server.com:7001/
http://eureka-client1.com:7002/
http://eureka-client2.com:7003/
如果访问地址出现下图,表示eureka启动成功
image.png启动服务提供者,将服务注册到eureka服务器
[http://eureka-server.com:7001/](http://eureka-server.com:7001/)
[http://eureka-client1.com:7002/](http://eureka-client1.com:7002/)
[http://eureka-client2.com:7003/](http://eureka-client2.com:7003/)
访问以上地址,出现下图,表示服务提供者注册服务到eureka集群成功
image.png启动服务提供者,从eureka集群获取服务提供者信息,并进行服务消费,启动成功后,进行测试
image.pngimage.png
http://localhost:9001/dept/get/2 访问这个地址时,出现RuntimeException异常,将进行熔断,将返回getIdError方法的内容
查看熔断信息(访问地址:http://localhost:9001/hystrix)
image.pngimage.png image.png地址栏输入:localhost:9001/hystrix.stream
title:随便输入
点击 按钮提交
访问:http://localhost:9001/dept/get/2, 服务提供者控制台将出现异常
查询hystrix dashboard页面,刷新
------------------------ 完毕 -------------------------
代码已提交到码云:https://gitee.com/liwuyin/SpringCloudStudy/tree/master/spring-cloud-study
如有疑问,请添加QQ:2646409029, 备注:简书,谢谢!
网友评论