简介 为初次接触Spring Cloud的同窗准备的系列教程,从应用场景、编码实战等方面介绍其中的各个组件。web
Spring Cloud是一套完整的微服务解决方案,是一系列不一样功能的微服务框架的集合。spring
Spring Cloud基于Spring Boot,简化了分布式系统的开发,集成了服务发现、配置管理、消息总线、负载均衡、断路器、数据监控等各类服务治理能力。好比sleuth提供了全链路追踪能力,Netflix套件提供了hystrix熔断器、zuul网关等众多的治理组件。config组件提供了动态配置能力,bus组件支持使用RabbitMQ、kafka、Activemq等消息队列,实现分布式服务之间的事件通讯。tomcat
基础组件1-Eureka server 这个是微服务的通信录(注册中心),既然是微服务,那么在调用别的微服务的时候确定须要其余微服务的地址、端口等信息,而这些信息都有Eureka Server来管理。安全
而Eureka Server的启动比较简单,做为一个Spring boot类型的项目来启动。服务器
对于习惯了Dubbo开发的同窗来讲,在使用Spring Cloud时遇到的第一个不习惯的地方就是,注册中心Eureka不是一个像Zookeeper那样独立运行的中间件,而是能够用Springboot来启动运行,有点相似于嵌入式的tomcat,这是Spring Cloud体系的一大特色,除了注册中心还有网关Zuul也是相似的启动方式。架构
基本的serverapp
引入pom文件 org.springframework.cloud spring-cloud-starter-netflix-eureka-server 增长启动类注解 @EnableEurekaServer @SpringBootApplication public class PlatformEurekaApplication { public static void main(String[] args) { SpringApplication.run(PlatformEurekaApplication.class, args); } } 配置yml文件 默认yml server: port: 7001 #(eureka 默认端口为:8761) spring: application: name: platform-eureka #服务名 eureka: instance: #服务失效时间,Eureka多长时间没收到服务的renew操做,就剔除该服务,默认90秒 leaseExpirationDurationInSeconds: 15 ip-address: {eureka.instance.ip-address} instanceId:
{server.port} preferIpAddress: true #将IP注册到Eureka Server上 client: #是否注册自身到eureka服务器,由于当前这个应用就是eureka服务器,不必注册自身,因此这里是false registerWithEureka: false fetchRegistry: false #表示是否从eureka服务器获取注册信息 serviceUrl: #是设置eureka服务器所在的地址,查询服务和注册服务都须要依赖这个地址(注意:地址最后面的 /eureka/ 这个是固定值) defaultZone: http://
{server.port}/eureka/ server: #设为false,关闭自我保护,开发测试环境须要频繁启动注册实例,须要关闭自我保护功能,以避免请求跑到旧实例中,生成环境须要开启自我保护功能 enableSelfPreservation: false #eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒 eviction-interval-timer-in-ms: 5000负载均衡
eureka.server.evictionIntervalTimerInMs: 20000 dev.yml eureka: instance: ip-address: 127.0.0.1框架
management: endpoints: enabled-by-default: true web: exposure: include: "*" 增长安全监控 http://xxx/Actuator分布式
修改pom org.springframework.boot spring-boot-starter-actuator 修改yml
management: endpoints: enabled-by-default: true web: exposure: include: "*" 增长安全认证
引入安全模块的pom org.springframework.boot spring-boot-starter-security 修改yml文件 security: basic: enabled: true # 开启基于HTTP basic的认证 user: name: user # 配置登陆的帐号是user password: password123 #配置登陆的密码是password123 基础组件2-Eureka client之服务提供者 服务提供者要做为Eureka的客户端在注册中心注册为服务提供者,这里重点是注册本身的名字和服务地址。
引入pom org.springframework.cloud spring-cloud-starter-netflix-eureka-client 默认yml server: port: {spring.cloud.client.ip-address} hostname:
{eureka.instance.ip-address}:
{eureka.instance.registry.default-open-for-traffic-count} registry.expected-number-of-renews-per-min:
{eureka.client.serviceUrl.defaultZone} dev.yml server: port: 7081 spring: profiles: dev eureka: instance: ip-address: 127.0.0.1 registry.default-open-for-traffic-count: 1 registry.expected-number-of-renews-per-min: 1 client: serviceUrl: defaultZone: http://127.0.0.1:7001/eureka/ management: endpoints: enabled-by-default: true web: exposure: include: "*" 增长注解 @EnableDiscoveryClient @SpringBootApplication @EnableTransactionManagement @MapperScan("com.itcast.mapper") public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } } 注意:
从Spring Cloud Edgware开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,便可将微服务注册到服务发现组件上。
@EnableDiscoveryClient和@EnableEurekaClient共同点就是:都是可以让注册中心可以发现,扫描到改服务。
不一样点:@EnableEurekaClient只适用于Eureka做为注册中心,@EnableDiscoveryClient 能够是其余注册中心。
基础组件3-Eureka client之服务消费者 经过注册中心查找本身须要的服务地址,就想提供名字查电话同样。
与服务提供者的编写方式基本一致。为了后面增长网关支持的方便,这里面的服务消费者自己也是服务提供者。
基础组件4-Feign 在消费者中使用,简化Http API的调用,使消费者调用服务提供者就想调用本地接口同样方便。
Spring Cloud对原生Feign进行了整合。
引入pom org.springframework.cloud spring-cloud-starter-openfeign 增长注解 @EnableFeignClients 按照服务提供者对应服务协议编写对应接口(不用实现) @FeignClient(name = "PLATFORM-USER") public interface UserService { /***
增长fallback参数 @FeignClient(name = "PLATFORM-USER", fallback = UserServiceFallbackImpl.class) 实现备胎代码 @Component //@Service public class UserServiceFallbackImpl implements UserService { private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceFallbackImpl.class); @Override public String searchUser(String userName){ LOGGER.error("用户信息查询接口调用异常:searchUser"); return JsonUtils.toText(ResponseUtils.failure("调用用户信息查询接口服务异常!")); } } 居然都看到最后了,给小编点个关注吧,小编还会持续更新的,只收藏不点关注的都是在耍流氓!