j360开源博客之
java
----------------------------------------------------------git
spring-cloud快速入门工程之j360-cloud-all:(欢迎star、fork)
github
https://github.com/xuminwlt/j360-cloud-allweb
spring cloud系列博客redis
(oschina首页推荐)J360-cloud SpringCloud系列一:分布式配置服务器ConfigServerspring
Discovery Service 是另外一个重要的微服务架构的组件.Discovery Service管理运行在容器中的众多服务实例,而这些实例工做在集群环境下.在这些应用中,咱们使用客户端的方式称之为从服务到服务shell
discovery service细分为3个部分:bootstrap
EurekaServer 服务注册服务器api
EurekaService 服务提供方,服务启动时会向注册服务器server注册该服务,服务经过rest形式提供api接口springboot
EurekaClient 服务客户端,经过restTemplate、Feign完成调用
Spring Cloud整合Ribbon和Eureka提供负载均衡的http client时使用Feign.
【注】:本案例中使用了第一节的configserver。固然能够把这个环节的配置去掉,能够分别独立使用
一、注解:
@EnableEurekaServer
二、bootstrap.yml/application.yml
--- server: port: 8761 spring: application: name:eurekaserver cloud: config: uri:http://localhost:8888 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/ spring.cloud.config.discovery.enabled: true
一、注解
@EnableEurekaClient
二、bootstrap.yml/application.yml
--- server: port: 8080 spring: application: name: eurekaclient cloud: config: enabled: true uri: http://localhost:8888 eureka: instance: leaseRenewalIntervalInSeconds: 10 metadataMap: instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}} client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:8761/eureka/
三、使用api
@Autowired private DiscoveryClient discoveryClient; public String serviceUrl() { InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false); return instance.getHomePageUrl(); }
package me.j360.cloud.eurekaclient.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.feign. * User: min_xu * Date: 2015/10/9 * Time: 10:49 * 说明:映射到service中的hello rest,在controller中直接调用helloClient便可 */ @FeignClient("eurekaservice") public interface HelloClient { @RequestMapping(value = "/", method = GET) String hello(); }
在controller中调用,这里的案例只调用hello方法,关于hystrix调用将在下一个系列中描述
package me.j360.cloud.eurekaclient.controller; import me.j360.cloud.eurekaclient.feign.HelloClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.controller. * User: min_xu * Date: 2015/10/9 * Time: 10:53 * 说明: */ @RestController public class HelloController { @Autowired @Qualifier("helloClient") HelloClient client; @Autowired @Qualifier("hystrixHelloClient") HelloClient hytrixClient; /** * 直接调用feign,feign会去调用eurekaService * */ @RequestMapping("/") public String hello() { return client.hello(); } /** * 一、调用hytrix * 二、hytrix继承并调用feign * 三、feign会去调用eurekaService * */ @RequestMapping("/hytrix") public String hytrixHello() { return hytrixClient.hello(); } }
按顺序运行server、service、client,能够打开eurekaserver的监视器页面
将eurekaservice按照集群的方式运行,修改port以后再执行一次,分别为8081/8082,,在执行localhost:8080查看运行的结果以下
Hello World: eurekaservice:min_xu:8081
Hello World: eurekaservice:min_xu:8082
能够看到,feign在执行时已经经过Ribbon实现了客户端的负载均衡,此时共运行了5台服务器,实现基于微服务的分布式架构的雏形结构,分别为
configserver
eurekaserver
eruekaservice1
eurekaservice2(集群可扩展并实现负载均衡)
eurekaclient
那么问题来了,如何实现高可用高质量的微服务api的调用,下一节介绍netflix利器:hytrix,该框架在基于springboot的微服务架构项目中有描述:
在springcloud中,hytrix经过spring start方式集成,使用起来更加方便。
SpringCloud提供了另一种服务发现框架,spring-cloud-zookeeper,一样其中使用了负载均衡Robbin+Feign组合使用,依然Hytrix也能够组合使用,spring-cloud-zookeeper还未同步到1.0.3版本,介绍文档也只有个标题,可是依然不凡读读源码,跑个test,了解其中的不一样之处:
在使用Eureka框架时,使用@EnableDiscoveryClient+eureka=@EnableEurekaClient
在使用zookeeper框架时:使用@EnableDiscoveryClient
一样还有另外一个很是流行的服务发现框架:consul,这三种框架均可以做为spring-cloud服务发现的实现框架,之后有机会补充。
看看官方对于springc-cloud-zookeeper的功能介绍:
Spring Cloud Zookeeper features:
Service Discovery: instances can be registered with Zookeeper and clients can discover the instances using Spring-managed beans
Supports Ribbon, the client side load-balancer via Spring Cloud Netflix
Supports Zuul, a dynamic router and filter via Spring Cloud Netflix
Distributed Configuration: using Zookeeper as a data store
服务发现:实例使用zookeeper注册,而且客户端经过spring-beans能够发现该实例
分布式配置:仅仅做为data store
至关的杯具:spring-cloud集群管理模块还在写代码,尚未完成start模块,简单介绍下
Spring Cloud Cluster offers a set of primitives for building "cluster" features into a distributed system. Example are leadership election, consistent storage of cluster state, global locks and one-time tokens.
用了zookeeper、redis、hazelcast三个服务
使用场景:分布式锁、选举、集群状态管理、一次性令牌