Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门
服务治理: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快速入门的更多相关文章
- spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象
一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系 (bean与b ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门
文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Spring Cloud微服务笔记(二)Spring Cloud 简介
Spring Cloud 简介 Spring Cloud的设计理念是Integrate Everything,即充分利用现有的开源组件, 在它们之上设计一套统一的规范/接口使它们能够接入Spring ...
- Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...
- go微服务系列(三) - 服务调用(http)
1. 关于服务调用 2. 基本方式调用服务 3. 服务调用正确姿势(初步) 3.1 服务端代码 3.2 客户端调用(重要) 1. 关于服务调用 这里的服务调用,我们调用的可以是http api也可以是 ...
- 《Spring技术内幕》笔记-第四章 Spring MVC与web环境
上下文在web容器中的启动 1,IoC容器的启动过程 IoC的启动过程就是建立上下文的过程.该上下文是与ServletContext相伴.在Spring中存在一个核心控制分发器,Dispatcher ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤
因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...
- 第三个视频作品《小白快速入门greenplum》上线了
1.场景描述 第三个视频作品出炉了,<小白快速入门greenplum>上线了,有需要的朋友可以直接点击链接观看.(如需购买,请通过本文链接购买) 2. 课程内容 课程地址:https:// ...
随机推荐
- nginx第三方库安装以及连接memcache
一.nginx第三方模块的安装 第三方模块查询地址:https://www.nginx.com/resources/wiki/modules/ 后来新出来一个nginx memcache增强版,有空可 ...
- openstack项目【day23】:keystone组件网关协议
本节内容 一 静态页面和动态页面 二 什么是web server 三 什么是网关协议 3.1 引子 3.2 网关协议 四 网关协议CGI.FastCGI.WSGI.UWSGI 五 网关协议与keyst ...
- EF CodeFirst系列(6)---配置1对1,1对多,多对多关系
这一节介绍EF CodeFirst模式中的1对0/1,1对多,多对多关系的配置,只有梳理清楚实体间的关系,才能进行愉快的开发,因此这节虽然很简单但是还是记录了一下. 1. 1对0/1关系配置 1. 通 ...
- Java String相关
一.String类的常用方法 1. int indexOf(String s) 字符串查找 2. int lastIndexOf(String str) 3. char charAt(int inde ...
- Richard Sabey于2004年给出了由123456789各出现一次的e的估计
- jpa @onetomany 级联查询时会有重复数据,去重问题
自己是直接查出来然后利用set去重(自己感觉不是太好,不过能达到目的) List<CampaignDashboardDimensionDo> list = query.getResultL ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...
- Python3:判断三角形的类型
# 判断三角形类型def triangle(a,b,c): if a>0 and b>0 and c>0: if a+b>c and b+c>a and a+c>b ...
- unity发布的WebGL部署到IIS
一.创建WebGL代码 在win7下,Unity3D中发布WebGL,然后部署到IIS,只要代码是对,关键是添加mime类型 二.为网站添加mime类型 .json text/json .unity3 ...
- jarvis level6_x64堆溢出unlink拾遗
level6 32位的我没有调出来,貌似32位的堆结构和64位不太一样,嘤嘤嘤?,所以做了一下这个64位的,题目地址,level6_x64 首先看一下程序的结构体 struct list //0x18 ...