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:// ...
随机推荐
- 越光后端开发——ygapi(1.新建项目ygapi、新建MySQL数据库yg、项目连接数据库)
1.新建MySQL数据库 show databases;//查看已经有的数据库 create database yg; 2.新建项目ygapi 1.使用pycharm新建django项目取名ygapi ...
- xadmin+django2.0删除用户报错,get_deleted_objects() takes 3 positional arguments but 5 were given
解决方法:将xadmin/plugins/actions.py中的 if django_version > (2, 0): #deletable_objects, model_count, pe ...
- 团体程序设计天梯赛(CCCC) L3021 神坛 的一些错误做法(目前网上的方法没一个是对的) 和 一些想法
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- Mysql SQL注入漏洞
学习网址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html https://www.cnblogs.com/sdya/p/4568 ...
- mysql普通用户本机无法登录的解决办法
背景 mysql和mariadb的用户表里存在匿名用户时,普通用户出现无法登录的情况 分析 先查看下用户表 mysql> select user, host, password from mys ...
- Pipeline load and load from git
load https://www.sourcefield.nl/post/jenkins-pipeline-tutorial/ node { // Use the shell to create th ...
- 【linux】基础知识
为什么学习Linux? 服务器大多数都是linux操作系统,学习[上线][运维]是我们前端开发工程师的必备技能.Linux系统和maxOS系统十分类似,所以推荐大家使用max电脑工作. Linux的版 ...
- Python3 字符串与hex之间的相互转换
在字符串转换上,python2和python3是不同的,在查看一些python2的脚本时候,总是遇到字符串与hex之间之间的转换出现问题,记录一下解决方法. 1. 在Python2.7.x上,hex字 ...
- 分布式系列五: RMI通信
RPC(Remote Procedure Call)协议 RPC协议是一种通过网络从远程计算机上请求服务, 而不需要了解底层网络技术的协议, 在OSI模型中处在应用层和网络层. 作为一个规范, 使用R ...
- JVM的垃圾回收机制 总结(垃圾收集、回收算法、垃圾回收器)
相信和小编一样的程序猿们在日常工作或面试当中经常会遇到JVM的垃圾回收问题,有没有在夜深人静的时候详细捋一捋JVM垃圾回收机制中的知识点呢?没时间捋也没关系,因为小编接下来会给你捋一捋. 一. 技术 ...