【Spring Cloud】 总结
一.Spring Cloud简介
简单来说,Spring Cloud 就是致力于分布式系统、微服务等的一套在目前非常火热的框架。但它的本身也是一系列框架的有序集合(由多个模块组成)。
相比较于Dubbo还是有很多不同之处的:例如Dubbo是基于RPC的服务框架,而Spring Cloud是基于RESTful API的服务框架。Dubbo是基于二进制的传输,而Spring Cloud是http协议传输等等。
二.Spring Cloud的Eureka组件
1.Eureka的作用
Eureka和zookeeper类似,能起到一个服务注册中心的作用
2.使用Eureka
在Spring Boot主类中添加@EnableEurekaServer注解即可开启Eureka(注意,需要引入相关的依赖,依赖如下)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3.Eureka配置
在application.properties中配置如下
spring.application.name=spring-cloud-eureka
server.port=8060
eureka.instance.hostname=localhost
#配置服务中心 应将下列属性设为false
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
#访问路径
eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
4.测试服务中心
启动Spring Boot项目,访问端口后可以看到如下界面

5.服务提供者
在项目下新建一个Spring Boot项目的Module,并在主类中添加@EnableEurekaClient注解
在配置文件中添加如下配置
spring.application.name=spring-cloud-provider
server.port=8061
eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
启动项目后发现服务中心多了一个服务提供者,如下图所示

6.服务消费者
Spring Cloud创建服务消费者有两种方式,分别是使用Ribbon和使用Feign
1.Ribbon的方式创建服务消费者
(1)首先引入相关的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
(2)在主类中添加@EnableDiscoveryClient注解
(3)最后在配置文件中配置如下
spring.application.name=spring-cloud-consumer
server.port=8063
eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
(4)服务消费者调用服务提供者中的方法
创建RestTemplate对象,并调用该对象的getForObject方法,例如下列代码
service层
@Service
public class MyService {
@Autowired
private RestTemplate restTemplate; public String getPort(){
//调用服务提供方的方法
return restTemplate.getForObject("http://spring-cloud-provider:getPort",String.class);
}
}
controller层
@RestController
public class MyController {
@Autowired
private MyService myService;
@RequestMapping("/getPort")
public String getPort(){
return myService.getPort();
}
}
测试结果如下

如果需要配置负载均衡策略,可以在服务消费者中添加一个配置类,再开启一个其他端口的服务提供者即可
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.Feign的方式创建服务消费者
(1)首先引入相关的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(2)然后在主类中添加 @EnableFeignClients 和 @EnableDiscoveryClient 注解,配置文件与Ribbon的方式类似
(3)服务消费者调用服务提供者中的方法
在service层中的代码如下
@FeignClient(value = "spring-cloud-provider")
public interface MyService {
@RequestMapping(value = "getPort" , method = RequestMethod.GET)
public String getPort(); }
在controller层中的代码如下
@RestController
public class MyController {
@Autowired
private MyService myService; @RequestMapping(value = "getPort" , method = RequestMethod.GET)
public String getPort(){
return myService.getPort();
}
}
由于Feign的方式本身包含有了Ribbon,也实现了负载均衡,面向接口编程,写起来思路更清晰方便,实际开发中应用更广
三.Spring Cloud 熔断器
1.Ribbon的方式下添加熔断器
(1)导入相关的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
(2)在主类中添加 @EnableHystrix 注解开启熔断器
(3)修改service层中的方法,在方法上添加 @HystrixCommand 注解,例如
@HystrixCommand(fallbackMethod = "getError")//如果getPort方法执行失败,会直接熔断该方法并执行getError方法(避免浏览器一直去加载)
public String getPort(){
//调用服务提供方的方法
return restTemplate.getForObject("http://spring-cloud-provider:getPort",String.class);
}
public String getError(){
return String.format("获取失败");
}
2.Feign的方式下添加熔断器
(1)Feign自带熔断器,但默认是关闭的,可以在application.properties中开启
feign.hystrix.enabled=true
(2)在service层的接口中添加熔断回调方法(在上面的代码的基础上,在@FeignClient 中添加熔断回调如下,如果出现熔断,则执行接口的实现类中的方法)
@FeignClient(value = "spring-cloud-provider",fallback = MyServiceImpl.class)//如果下面的方法执行失败,会熔断并去执行MyServiceImpl中的方法
(3)创建接口实现类,例如
@Component
public class MyServiceImpl implements MyService { @Override
public String getPort() {
return String.format("获取失败");
}
}
3熔断监控
如果需要监控熔断信息,可以做如下步骤
(1)导入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
(2)在主类中添加注解 @EnableHystrixDashboard 开启熔断监控,启动项目,并访问/hystrix,页面如下

(3)添加配置类
@Configuration
public class MyConfig {
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/myhystrix");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
(4)在页面中输入路径即(ip+端口号+配置类中的addUrlMappings)可查看熔断信息,例如下图

四.Spring Cloud 网关服务
1.开启Zuul网关服务步骤如下
(1)引入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
(2)在主类中添加 @EnableEurekaClient 和 @EnableZuulProxy 注解开启相应的服务
(3)在配置文件中配置如下
spring.application.name=spring-cloud-zuul
server.port=8065
eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
#指定网关路径,并设置其映射路径
zuul.routes.api-a.path=/api/ribbon/**
zuul.routes.api-a.serviceId=spring-cloud-consumer-ribbon
zuul.routes.api-b.path=/api/feign/**
zuul.routes.api-b.serviceId=spring-cloud-consumer-feign
(4)启动项目,可以通过网关来访问项目,例如下图

2.网关路由失败回调
编写一个回调实体类,并实现 FallbackProvider 接口即可,详细可以参考官方api文档
3.zuul过滤功能
编写一个过滤器,并实现ZuulFilter接口即可
五.Spring Cloud 配置中心
1.服务端配置
(1)导入相关依赖并在主类中开启@EnableEurekaClient 和 @EnableConfigServer注解
(2)在配置文件中配置如下(以github作为配置文件存放中心)
spring.application.name=spring-cloud-config
spring.cloud.config.label=master
#gitHub仓库地址
spring.cloud.config.server.git.uri=git@github.com:RetimeFor/spring-cloud-config-test.git
#存放配置文件的文件夹(相对以上仓库地址)
spring.cloud.config.server.git.search-paths=spring-cloud-config/others
spring.cloud.config.server.username=gitHub账号
spring.cloud.config.server.password=gitHub密码
#端口号配置为8888(不要在这里修改)
server.port=8888
eureka.client.serviceUrl.defaultZone=http://localhost:8060/eureka/
(3)测试结果如下(端口后直接跟 /文件的名字/master)

2.客户端配置
以Feign的服务端为例,只需要将配置文件中的内容用如下代码替换即可
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.name=other
spring.cloud.config.label=master
【Spring Cloud】 总结的更多相关文章
- spring/spring boot/spring cloud开发总结
背景 针对RPC远程调用,都在使用dubbo.dubbox等,我们也是如此.由于社区暂停维护.应对未来发展,我们准备尝试新技术(或许这时候也不算什么新技术了吧),选择使用了spring ...
- 转 Netflix OSS、Spring Cloud还是Kubernetes? 都要吧!
Netflix OSS.Spring Cloud还是Kubernetes? 都要吧! http://www.infoq.com/cn/articles/netflix-oss-spring-cloud ...
- spring cloud 学习研究- spring-cloud-microservice-example
spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...
- Spring Cloud集成相关优质项目推荐
Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...
- spring boot分布式技术,spring cloud,负载均衡,配置管理器
spring boot分布式的实现,使用spring cloud技术. 下边是我理解的spring cloud的核心技术: 1.配置服务器 2.注册发现服务器eureka(spring boot默认使 ...
- Spring Cloud 配置服务
Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...
- Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转
原文地址:https://www.linkedin.com/pulse/microservices-reference-architecture-spring-boot-cloud-anil-alle ...
- 综合使用spring cloud技术实现微服务应用
在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...
- Spring cloud实现服务注册及发现
服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...
- 使用spring cloud实现分布式配置管理
<7天学会spring cloud系列>之创建配置管理服务器及实现分布式配置管理应用. 本文涉及到的项目: 开源项目:http://git.oschina.net/zhou666/spri ...
随机推荐
- css清除浮动的几种方式,哪种最合适?
细心的人可能发现了,写的导航条中存在一个问题,那就是使用了float之后,父级盒子的高度变为0了. 我们来写一个例子来看一下,创建一个父级div,并设置border属性,然后下边创建两个子元素span ...
- 跨平台编程相关技术资料及笔记.md
目录 跨平台编程技术选型 ## 需求 最终选定的技术方案:uni-app 混合或跨平台编程相关资料 ## uni-app 官网 相关资料 个人笔记 个人经验 ## taro 官网 相关资料 ## Ch ...
- maskrcnn_benchmark 理解
转载与参考: 1,https://zhuanlan.zhihu.com/p/58101945 2,https://blog.csdn.net/linolzhang/article/details/71 ...
- Performance Analysis of Logs (PAL) Tool
Performance Analysis of Logs (PAL) Tool 背景 在众多的独立项目中,我们如何快速了解数据库(SQL Server)服务器的性能,以及数据库的基线情况是怎样的,或者 ...
- confilicts
confilicts 矛盾; 冲突 安装软件提升这个表示安装软件之间的冲突,需要卸载冲突的软件再安装
- How to remove duplicate lines in a large text file?
How would you remove duplicate lines from a file that is much too large to fit in memory? The dupli ...
- 20190722 - Windows 下使用 move 命令和通配符移动多个文件
这是个标题党,实际上 move 命令无法移动通配符匹配的多个文件 比如,我有四个文件: c:\a\a1.txt c:\a\a2.txt c:\a\b1.txt c:\a\b2.txt 想移动后两个文件 ...
- 记一次django学习1.0和2.0区别
依据学习课程的教学,在项目实战学习过程中教学使用django1.0,获取ManytoMany关联字段,源码使用的是 即django使用 models.Customer.tags.rel.to.obje ...
- Mysql——通配符和正则表达式的使用
1.like操作符和百分号通配符 %表示任何字符出现任意次数. 查询出表TABLE中NAME字段中任意位置包含i的行: select * from TABLE where NAME like '%i% ...
- union和in哪个效率高
一直都认为是in的效率要高,但是这次确有点蒙圈. SELECT * FROM runinfo WHERE status in (0,2,1,3,4,7,9,10); 这个查询的效率是,经常是1秒多. ...