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三個服務
使用場景:分佈式鎖、選舉、集羣狀態管理、一次性令牌