博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
J360-cloud SpringCloud系列二:服务发现Discovery Service
阅读量:6938 次
发布时间:2019-06-27

本文共 5013 字,大约阅读时间需要 16 分钟。

hot3.png

j360开源博客之

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

J360-Cloud系列

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

spring cloud系列博客

服务发现Discovery Service

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

discovery service细分为3个部分:

  1. EurekaServer 服务注册服务器

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

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

Feign声明式 REST Client

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

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

EurekaServer

1、注解:

@EnableEurekaServer

2、bootstrap.yml/application.yml

---server:  port: 8761spring:  application:    name:eurekaserver  cloud:    config:      uri:http://localhost:8888eureka:  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: 8080spring:  application:    name: eurekaclient  cloud:    config:      enabled: true      uri: http://localhost:8888eureka:  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/

3、使用api

@Autowiredprivate 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 * 说明: */@RestControllerpublic 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的监视器页面

142037_LF5i_1026123.png

将eurekaservice按照集群的方式运行,修改port之后再执行一次,分别为8081/8082,,在执行localhost:8080查看运行的结果如下

Hello World: eurekaservice:min_xu:8081

Hello World: eurekaservice:min_xu:8082

可以看到,feign在执行时已经通过Ribbon实现了客户端的负载均衡,此时共运行了5台服务器,实现基于微服务的分布式架构的雏形结构,分别为

  1. configserver

  2. eurekaserver

  3. eruekaservice1

  4. eurekaservice2(集群可扩展并实现负载均衡)

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

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

  1. 服务发现:实例使用zookeeper注册,并且客户端通过spring-beans可以发现该实例

  2. 分布式配置:仅仅作为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三个服务

使用场景:分布式锁、选举、集群状态管理、一次性令牌

转载于:https://my.oschina.net/smartsales/blog/515420

你可能感兴趣的文章
帮助你检查Linux系统内存及其使用情况的命令
查看>>
Jeff Dean本科论文首次曝光!第一批90后出生时,他就在训练神经网络
查看>>
Python中lambda的用法
查看>>
E036-rpmdb open failed
查看>>
手把手:一张图看清编程语言发展史,你也能用Python画出来!
查看>>
使用MaxCompute进行数据质量核查
查看>>
SQLServer2005 判断数据库中是否存在某张表或是查找库中的所有表名,然后删除...
查看>>
awk工具
查看>>
mysql 常用日期处理函数
查看>>
使用python进行数据的采集 编辑 删除
查看>>
布尔类型及return 严重程度
查看>>
解决springboot程序员的一点小困惑,nginx的反向代理
查看>>
Java虚拟机基本结构的简单记忆
查看>>
hibernate查询返回结果值为实体时接收方法
查看>>
七牛云 ssl免费证书申请
查看>>
基于结构化平均感知机的分词器Java实现
查看>>
比较好的中文分词方案汇总推荐
查看>>
Chrome本地安装Metamask
查看>>
Tomcat介绍 (资源 )
查看>>
【2018.06.28学习笔记】【linux高级知识 17.1-17.5】
查看>>