服务治理:Spring Cloud Eureka

一、服务治理

服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现。

1.服务注册

在服务治理框架中,通常会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、

版本号、通信协议等一些附件信息告知注册中心,注册中心按服务名称分类组织服务清单,例如:

2.服务发现:

调用方需要向服务注册中心咨询服务,并获取所有服务的实例清单,以实现对具体服务实例的访问。

二、Netflexi Euraka

Spring Cloud Euraka使用Netflexi Euraka来实现服务的注册与发现,它既包含了

服务端的组件也包含了客户端的组件。

Euraka 服务端:即服务注册中心。

Euraka 客户端:主要处理服务的注册与发现。Euraka客户端向注册中心注册自身提供的服务并

周期性地发送心跳来更新它的服务租约。同时,他能从服务端查询当前注册的服务信息并把它们

缓存到本地并周期性地更新服务状态。

三、搭建服务注册中心

pom.xml中引入必要依赖:

    <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

启用服务注册中心:

//启动一个服务注册中心,提供给其他应用进行对话。
//在默认设置下,该服务注册中心也会将自己作为客户端尝试注册它自己
@EnableEurekaServer
@SpringBootApplication
public class TangcloudApplication {
public static void main(String[] args) {
SpringApplication.run(TangcloudApplication.class, args);
}
}

禁用默认的客户端注册行为:

#设置服务注册中心端口
server.port=
eureka.instance.hostname=localhost
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去检索服务
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eurka/

四、注册服务提供者

pom.xml:

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

controller:

@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
ServiceInstance serviceInstance = client.getLocalServiceInstance();
System.out.println("/hello, host: " + serviceInstance.getHost() + ", service_id: " +
serviceInstance.getServiceId());
return "Hello World!";
}
}
@EnableDiscoveryClient //激活DiscoveryClient实现
@SpringBootApplication
public class Service1Application { public static void main(String[] args) {
SpringApplication.run(Service1Application.class, args);
} }

application.properties文件:

server.port=
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

五、高可用注册中心

Euraka高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就可以

形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用效果。

构建一个服务注册中心集群

1)创建application-peer1.properties,作为peer1服务中心的配置:

pring.application.name=eureka-server
server.port=1111 eureka.instance.hostname=peer1
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/

2)建application-peer1.properties,作为peer1服务中心的配置:

spring.application.name=eureka-server
server.port=1112 eureka.instance.hostname=peer2
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/

3)服务提供方的配置:

eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

六、服务的发现与消费

下面构建一个服务消费者,它可以发现服务及消费服务。其中服务的发现任务由Eureka的客户端完成。

Ribbon是一个基于HTTP和TCP的客户端负载均衡器,它在Eureka发现服务的基础上,实现了一套对服务

的选择策略。

首先,创建一个Spring Boot工程,并在之前hello-service的pom.xml基础上新增:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

启动类:

@SpringBootApplication
@EnableDiscoveryClient //让该应用注册为Eureka客户端,获得服务发现的能力
public class ConsumerApplication {
@Bean
@LoadBalanced //开启客户端负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

application.properties

server.port=9671
spring.application.name=ribbon-consumer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

controller:

@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate; //通过它来调用service-hello服务的/hello接口
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity("http://hello-service/hello",
String.class).getBody();
}
}

Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门的更多相关文章

  1. spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象

    一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系 (bean与b ...

  2. Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门

    文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...

  3. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  4. Spring Cloud微服务笔记(二)Spring Cloud 简介

    Spring Cloud 简介 Spring Cloud的设计理念是Integrate Everything,即充分利用现有的开源组件, 在它们之上设计一套统一的规范/接口使它们能够接入Spring ...

  5. Spring Cloud 微服务笔记(六)Spring Cloud Hystrix

    Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...

  6. go微服务系列(三) - 服务调用(http)

    1. 关于服务调用 2. 基本方式调用服务 3. 服务调用正确姿势(初步) 3.1 服务端代码 3.2 客户端调用(重要) 1. 关于服务调用 这里的服务调用,我们调用的可以是http api也可以是 ...

  7. 《Spring技术内幕》笔记-第四章 Spring MVC与web环境

    ​上下文在web容器中的启动 1,IoC容器的启动过程 IoC的启动过程就是建立上下文的过程.该上下文是与ServletContext相伴.在Spring中存在一个核心控制分发器,Dispatcher ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤

    因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...

  9. 第三个视频作品《小白快速入门greenplum》上线了

    1.场景描述 第三个视频作品出炉了,<小白快速入门greenplum>上线了,有需要的朋友可以直接点击链接观看.(如需购买,请通过本文链接购买) 2. 课程内容 课程地址:https:// ...

随机推荐

  1. python之路day03--数据类型分析,转换,索引切片,str常用操作方法

    数据类型整体分析 int :用于计算bool:True False 用户判断str:少量数据的存储 list:列表 储存大量数据 上亿数据[1,2,3,'zzy',[aa]] 元组:只读列表(1,23 ...

  2. 微信小程序之 3d轮播(swiper来实现)

    以前写过一篇3d轮播,就是这篇,使用的方法比较笨拙,而且代码不简洁.这次发现swiper也能实现同样的效果.故记录一下. 先看看效果: wxml: <swiper previous-margin ...

  3. Linux下安装部署Samba共享盘的操作手册

    简述 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的 ...

  4. saltstack主机管理项目:计主机管理项目命令分发器(三)

    一.开发目标命令格式如下: 二.目录结构 三.代码注解 01.salt.py 只是一个入口,没干什么事情 #!/usr/bin/env python # -*- coding:utf-8 -*- # ...

  5. Fiddler--Composer

    Composer选项卡支持手动构建和发请求:也可以在Session列表中拖拽Session放到Composer中,把该Session的请求复制到用户界面: 点击"execute"按 ...

  6. Jasmine

    Jasmine https://www.npmjs.com/package/jasmine The Jasmine Module The jasmine module is a package of ...

  7. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  8. tangent space与object space

    3d渲染每个网格(Mesh)的面都可配一个材质(Material),要想在一个面上显示出更多的细节,除了模型做的更精致,还可以使用法线贴图(Normal Texture). 法线向量(Normal V ...

  9. eclipse hadoop环境搭建 查看HDFS文件内容

    1.下载插件 hadoop-eclipse-plugin-2.5.2.jar放入eclipse/plugin 2.准备hadoop-2.5.0-cdh5.3.6 使用WinSCP远程连接虚拟机,复制h ...

  10. 10张思维导图带你学习Java​Script

    10张思维导图带你学习Java​Script   下面将po出10张JavaScript相关的思维导图. 分别归类为: JavaScript变量 JavaScript运算符 JavaScript数组 ...