SpringCloud简单入门——写给自己
来源:互联网 发布:楼月软件 编辑:程序博客网 时间:2024/05/29 03:11
Eureka实现原理
概念
Spring Cloud利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。
SpringCloud主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等,在此提供一个SpringCloud相关的中文网址https://springcloud.cc/。
Spring Cloud Eureka
我们使用Spring Cloud Eureka来实现服务治理,Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。
1、创建服务注册中心
在Spring Tool Suite工具中,右键点击项目视图空白处,选择菜单New Spring Starter Project后,填写项目信息,选择Eureka Server。
项目创建完成后,自动创建RegistryCenterApplication类,在类前增加注解@EnableEurekaServer
在application.properties文件中,增加配置信息
spring.application.name=eureka-serverserver.port=1001eureka.instance.hostname=127.0.0.1eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
2、增加会员服务
创建会员服务,在注册中心中进行注册。
在Spring Tool Suite工具中,右键点击项目视图空白处,选择菜单New Spring Starter Project后,填写项目信息。
项目创建完成后,自动创建MemberServiceServerApplication类,在类前增加注解@EnableDiscoveryClient
在application.properties文件中,增加配置信息
spring.application.name=eureka-member-serviceserver.port=2001eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1001/eureka/
如果eureka服务页面增加了eureka-member-service服务,那么会员服务注册成功。
在MemberServiceServer项目中,添加业务类DispatcherController
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DispatcherController { @RequestMapping("/test") public String test() { return "member service !"; }}
重启项目后,访问http://127.0.0.1:2001/test。页面中显示member service !表示成功。
3、访问会员服务
当在注册中心注册了会员服务后,其他的任何应用都可以通过feign方式从注册中心中获取会员服务。
在Spring Tool Suite工具中,右键点击项目视图空白处,选择菜单New Spring Starter Project后,填写项目信息。
项目创建完成后,自动创建MemberServiceClientApplication类,在类前增加注解@EnableDiscoveryClient, @EnableFeignClients
在application.properties文件中,增加配置信息
spring.application.name=eureka-member-clientserver.port=80eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
在MemberServiceClient项目中,添加服务访问类MemberClient
import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;@FeignClient("eureka-member-service")public interface MemberClient { @GetMapping("/test") String test();}
@FeignClient(“eureka-member-service”)中的字符串内容应该和会员服务在注册中心注册的名称相同eureka-member-service
在MemberServiceClient项目中,添加业务类DispatcherController调用会员服务MemberClient。
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.atguigu.crowdfunding.client.MemberClient;@RestControllerpublic class DispatcherController { @Autowired private MemberClient memberClient; @RequestMapping("/test") public String test() { return memberClient.test(); }}
访问http://127.0.0.1/test。页面中显示member service !表示客户端访问会员服务成功。
Spring Cloud Ribbon
在服务发现Eureka中。我们使用了Feign方式读取Service应用中的数据,那如果多个Service应用提供相同的服务,这时候feign服务客户端访问哪一个Service应用就不确定了,所以此时需要一个类似于Nginx负载均衡器一样的软件或服务来进行调度。
Ribbon就是这样的一种技术,不过和Nginx区别在于,Ribbon是在feign客户端获取Eureka注册服务列表后进行的负载均衡(软负载均衡)。
feign服务中已经内置了Ribbon, 所以不需要额外的操作。
Spring Cloud Hystrix
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
在微服务架构中,一个请求需要调用多个服务是非常常见的;较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开;断路打开后,可用避免连锁故障。
代码实现时,首先需要在pom.xml文件中增加依赖关系
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId></dependency>
在程序的启动类中增加@EnableHystrix注解开启Hystrix。
在调用feign服务的Controller方法前增加注解@HystrixCommand(fallbackMethod=”服务失败时调用的方法名”)
服务调用失败时回调的方法声明应该和feign服务调用的方法声明保持一致
正确启动应用后,再关闭服务,测试断路器是否工作正常。
- SpringCloud简单入门——写给自己
- 入门伊始1写给自己
- ——写给迷途中的自己
- 写给自己——回信篇
- 写给自己——信息增益
- 写给自己——卡方检测
- Java 重温基础——写给自己
- Android进阶—写给自己的计划
- Windows SDK入门浅谈 — 写给初学者
- Windows SDK入门浅谈 — 写给初学者
- Windows SDK入门浅谈 — 写给初学者
- Windows SDK入门浅谈 — 写给初学者
- Windows SDK入门浅谈 — 写给初学者
- Windows SDK入门浅谈 — 写给初学者
- SpringBoot,SpringCloud入门到精通最简单教程
- SpringCloud微服务系列笔记(1)-SpringBoot简单入门
- 写给自己,写给大家
- 写给时间、写给自己
- JDBC、JdbcTemplate、SpringData对比学习
- 显式锁(二)--- Lock接口与显示锁
- [Leetcode][python]Binary Tree Maximum Path Sum
- 21. Merge Two Sorted Lists
- 前端学习_04.内联/块级元素
- SpringCloud简单入门——写给自己
- [Unity 热更新]tolua集成
- Python 代码运行时间的3种计算方法
- netcat
- 经典编程书籍大全
- Spring-Cloud-Config 共享公共配置
- nginx配置负载均衡
- 编写DAO(Data Access Object)
- js生成Excel表格