2019独角兽企业重金招聘Python工程师标准>>>

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个部分:

  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:eurekaservercloud:config:uri:http://localhost:8888eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://localhost:8761/eureka/spring.cloud.config.discovery.enabled: true

EurekaClient

1、注解

@EnableEurekaClient

2、bootstrap.yml/application.yml

---
server:port: 8080spring:application:name: eurekaclientcloud:config:enabled: trueuri: http://localhost:8888eureka:instance:leaseRenewalIntervalInSeconds: 10metadataMap:instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}client:registerWithEureka: truefetchRegistry: trueserviceUrl:defaultZone: http://localhost:8761/eureka/

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台服务器,实现基于微服务的分布式架构的雏形结构,分别为

  1. configserver

  2. eurekaserver

  3. eruekaservice1

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

  5. 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

  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

J360-cloud SpringCloud系列二:服务发现Discovery Service相关推荐

  1. Nacos教程_3 整合SpringCloud(配置中心+服务发现)

    教程原稿 https://gitee.com/fakerlove/joker-nacos 文章目录 3. 整合SpringCloud(配置中心+服务发现) 3.1 写配置 3.2 创建父工程demo ...

  2. SpringCloud使用Nacos服务发现实现远程调用

    本文使用SpringCloud结合Nacos服务发现,Feign远程调用做一个简单的Demo. 1 Nacos 关于Nacos之前写了两篇文章关于SpringBoot对它的使用,感兴趣可以查看一下. ...

  3. k8s 之服务发现(service)

    文章目录 k8s 之服务发现(service) 一.基本概念 二.类型介绍 Service 在 K8S 中有以下四种类型 三.代理模式 userspace 代理模式 iptables 代理模式 ipv ...

  4. eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理

    服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分 ...

  5. SpringCloud学习笔记(五)服务发现Discovery

    对于注册进Eureka里的微服务,可以通过服务发现来获得该服务的信息 以下代码以payment8001模块为例 修改COntroller 通过调用DiscoveryClient的getServices ...

  6. 【SpringCloud系列】服务注册与发现 - Eureka Server源码分析(2)

    3.6.Eureka Server 源码分析 上一篇文章简单介绍了 Eureka 的一些概念,今天咱们来看看其原理和源码,首先先看 Eureka Server 的原理. 3.6.1.Eureka Se ...

  7. springcloud微服务(九)-服务发现Discovery

    Discovery功能: 对于注册进Eureka里面的微服务,可以通过服务发现来获取服务的信息.此处以payment8001为例.payment8002与payment8001一样. 1. 修改clo ...

  8. SpringCloud接入EDAS——服务发现篇

    旁白 很久没有写技术文章了,最近不是写水文就是写小说.说到底,还是最近很少研究技术的缘故,已经到了江郎才尽的地步了. 不过,LZ无意间看到自己团队的小伙伴写的一些文章,觉得还是不错的,于是便动了心思, ...

  9. SpringCloud系列之服务注册中心(Eureka)

    文章目录 0.前言 1.搭建第一个Eureka服务 ①.简介 ②.组成 ③.创建Eureka Server模块 ④.编写POM ⑤.YML ⑥.主启动 ⑦.测试 2.注册服务 ①服务提供者 Ⅰ.添加依 ...

最新文章

  1. matlab模拟伯努利实验,matlab仿真随机数的发生
  2. H∞控制的simulink仿真
  3. Android应用开发—AsyncTask
  4. 华为hcia H31-311 练习题
  5. R语言与正态总体均值的区间估计
  6. JAVA复习(date)
  7. centos amd双显卡_PCIe4.0和PCIe3.0对显卡性能差别大吗?
  8. matlab动画_用matlab做一个脉动磁势分解的动画
  9. Gogs 查看 git 代码地址
  10. win7录屏_屏幕录制软件哪个好用?推荐三种正确录屏方法
  11. 微博清理多个百万粉丝大号:打击违规账号要命中七寸
  12. html logo写法,教你用CSS3打造HTML5的Logo
  13. AttributeError: 'module' object has no attribute 'urlopen报错解决办法
  14. Mentor.Graphics.FloTHERM.XT.2.3+Mentor.Graphics.Flowmaster.7.9.4
  15. 10gR2--用asmcmd对ASM进行管理
  16. ubuntu16.04 安装hustoj 最新【亲测,附带常见问题解决】
  17. finecms V5 会员头像任意文件上传漏洞 附修复代码
  18. 一种化学荧光探针945928-17-6,TAMRA alkyne,5-isomer,四甲基罗丹明-炔基
  19. PMP49个过程组知识领域ITTO汇总 -- 项目范围管理
  20. 应用内打开QQ,发送消息给指定联系人(临时会话),应用内加群

热门文章

  1. 大脑构造图与功能解析_施工技术特辑 | 全套脚手架三维构造图解析
  2. 怎么获取codeforces的数据_原创 | codeforces 1417C,逆向思考的数据结构题
  3. php指针注意问题,c语言指针注意问题
  4. Java实用教程笔记 类与对象
  5. matlab 恶俗的缩写combntns()
  6. word中如何删除顽固的空白页?
  7. HDLBits 系列(38)值得一看的状态机设计题目
  8. 【 MATLAB 】信号处理工具箱之波形产生函数 tripuls
  9. 【深度学习篇】--Windows 64下tensorflow-gpu安装到应用
  10. Cisco产品线一览