您的位置:首页 > 编程语言 > Java开发

J360-cloud SpringCloud系列二:服务发现Discovery Service

2015-10-10 22:15 621 查看
j360开源博客之

----------------------------------------------------------

J360-Cloud系列

spring-cloud快速入门工程之j360-cloud-all:(欢迎star、fork)

https://github.com/xuminwlt/j360-cloud-all

spring cloud系列博客
(oschina首页推荐)J360-cloud SpringCloud系列一:分布式配置服务器ConfigServer

服务发现Discovery Service

Discovery Service 是另一个重要的微服务架构的组件.Discovery Service管理运行在容器中的众多服务实例,而这些实例工作在集群环境下.在这些应用中,我们使用客户端的方式称之为从服务到服务

discovery service细分为3个部分:

EurekaServer 服务注册服务器

EurekaService 服务提供方,服务启动时会向注册服务器server注册该服务,服务通过rest形式提供api接口

EurekaClient 服务客户端,通过restTemplate、Feign完成调用

Feign声明式 REST Client

Spring Cloud整合Ribbon和Eureka提供负载均衡的http client时使用Feign.

【注】:本案例中使用了第一节的configserver。当然可以把这个环节的配置去掉,可以分别独立使用

EurekaServer

1、注解:

@EnableEurekaServer

2、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


EurekaClient

1、注解
@EnableEurekaClient

2、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/[/code] 3、使用api

@Autowired
private DiscoveryClient discoveryClient;

public String serviceUrl() {
InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
return instance.getHomePageUrl();
}


Feign实施

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();
}

/**
* 1、调用hytrix
* 2、hytrix继承并调用feign
* 3、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的微服务架构项目中有描述:

j360-microservice

在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服务发现的实现框架,以后有机会补充。

spring-cloud-zookeeper补充

看看官方对于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-cluster补充

相当的杯具: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三个服务

使用场景:分布式锁、选举、集群状态管理、一次性令牌
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  j360 spring cloud boot