SpringBoot + Spring Cloud Eureka 服务注册与发现

什么是Spring Cloud Eureka
Eureka是Netflix公司开发的开源服务注册发现组件,服务发现可以说是微服务开发的核心功能了,微服务部署后一定要有服务注册和发现的能力,Eureka就是担任这个角色。如果你用过Dubbo的话,Dubbo里服务注册和发现就是通过Zookeeper框架完成的。
Eureka 目前是2.2.x版本,目前官方已经宣布不再维护和更新了,不过Eureka 做注册中心已经在生产环境中大规模使用了,可以说很稳定了。从我个人的角度看,目前大家使用的更多的是阿里的Nacos和Consul这两个组件实现了不止服务发现和注册,微服务开发不用再去依赖更多的组件和框架。这篇文章模拟一下Eureka Server集群和服务提供者集群和服务消费。
版本说明
SpringCloud + SpringBoot开发微服务并不是版本越新越好,Spring Cloud官方提供了一个版本对应关系。目前最新的就是Hoxton, 对应SpringBoot 2.2.x版本。
准备工作
- 新建父工程, 主要约定SpringCloud, SpringBoot版本号,我使用的是Hoxton.SR1, SpringBoot2.2。
- 新建5个子Module,这里两个Eureka Server,两个Service,一个consumer, 两个Eureka Servr我用7001和7002端口来模拟和区分Eureka集群,两个Service 用8001和8002来模拟,主要是想在服务消费时反应客户端负载均衡。
项目结构如下图

父工程pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
服务注册中心
服务提供者我用了7001和7002模拟集群,两个程序代码都是相同。
新建Module,添加spring-cloud-starter-netflix-eureka-server。
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 7001
eureka:
client:
service-url:
defaultZone: http://eureka7002.com:7002/eureka # 集群就是指向其他配置中心
register-with-eureka: false
fetch-registry: false
instance:
hostname: eureka7001.com #eureka服务端实例名称
server:
enable-self-preservation: false
修改启动类
@SpringBootApplication
@EnableEurekaServer
public class MyEurekaServer7001 {
public static void main(String[] args) {
SpringApplication.run(MyEurekaServer7001.class,args);
}
}
我这里贴出了7001的代码,7002唯一不同的地方在application.yml配置里,如果是集群的话eureka.client.service-url.defaultZone需要配置集群里其他server节点。这里instance.hostname配置为eureka7001.com, 对应配置需要修改下本机hosts
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
启动两个注册中心服务
eureka7001
eureka7002
服务启动后,注意到DS Replicas中包含集群中另一台机器,说明集群已生效。
服务提供者
服务提供者我这里用了8001和8002两个来模拟集群。
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
name: CLOUD-STUDENT-SERVICE
eureka:
client:
register-with-eureka: true
#集群节点下需要设置为true,才能配合客户端使用Ribbon 负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka #集群需要配置所有注册中心
instance:
instance-id: student-service8001
prefer-ip-address: true # 访问路径可以显示ip地址
添加一个服务接口
@RestController
@RequestMapping("/student")
public class StudentController {
@GetMapping("/list")
public List<String> list(){
return Arrays.asList("Kobe","Lin","Tim","James");
}
@GetMapping("/version")
public String version(){
return "8001,202007162310";
}
}
修改启动类
@SpringBootApplication
@EnableEurekaClient
public class MyStudentService8001 {
public static void main(String[] args) {
SpringApplication.run(MyStudentService8001.class,args);
}
}
依次启动两个服务提供者8001和8002。服务启动后无错误的情况下刷新Eureka Server界面。
eureka7001
eureka7002
在Application里已经看到CLOUD-STUDENT-SERVICE的状态中已经有两个服务注册进来了。
服务消费者
服务消费者我就写了一个Module,端口8087
pom.xml
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
application.yml
这里客户端消费服务时需要配置集群中的两个注册中心地址。
server:
port: 8087
spring:
application:
name: student-consumer
eureka:
client:
fetch-registry: true
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
服务消费接口
服务消费我们仍然用的RestTemplate,客户端负载均衡用的实际上是Ribbon组件,默认使用轮训算法。
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/version")
public String index(){
return restTemplate.getForObject("http://CLOUD-STUDENT-SERVICE/student/version",String.class);
}
}
修改启动类
我在启动类还配置了RestTemplate,启动类两个关键注解SpringBootApplication和EnableEurekaClient。
@SpringBootApplication
@EnableEurekaClient
public class StudentConsumer8087 {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(StudentConsumer8087.class,args);
}
}
启动并访问服务消费者接口http://localhost:8087/student/version, 从输出结果能看到会轮训调用8001和8002的接口8002,202007162310,8001,202007162310。
SpringBoot + Spring Cloud Eureka 服务注册与发现的更多相关文章
- SpringBoot + Spring Cloud Consul 服务注册和发现
什么是Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更"一站式" ...
- Spring cloud实现服务注册及发现
服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务. 本文属于<7天学会spring cloud系列& ...
- Spring Cloud 之 服务注册与发现
作为微服务框架,提供服务注册发现是最基本的功能.Spring Cloud 针对服务注册发现 提供了 Eureka版本的实现 .Zookeeper版本的实现.Consul版本的实现.由于历史原因 Eur ...
- spring cloud Eureka 服务注册发现与调用
记录一下用spring cloud Eureka搭建服务注册与发现框架的过程. 为了创建spring项目方便,使用了STS. 一.Eureka注册中心 1.新建项目-Spring Starter Pr ...
- Spring Cloud Eureka 服务注册中心(二)
序言 Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Clie ...
- Spring Cloud Eureka 服务注册列表显示 IP 配置问题
服务提供者向 Eureka 注册中心注册,默认以 hostname 的形式显示,Eureka 服务页面显示的服务是机器名:端口,并不是IP+端口的形式 ,可以通过修改服务提供者配置自己的 IP 地址, ...
- 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现
前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...
- 【Spring Cloud】服务注册与发现组件——Eureka(二)
一.Eureka原理 1.架构图 首先来看eureka的官方结构图 所有应用作为Eureka Client和Eureka Server交互,服务提供者启动时向Eureka Server注册自己的IP. ...
- Spring Cloud Eureka服务注册源码分析
Eureka是怎么work的 那eureka client如何将本地服务的注册信息发送到远端的注册服务器eureka server上.通过下面的源码分析,看出Eureka Client的定时任务调用E ...
随机推荐
- Riccati方程迭代法求解
根据上述迭代法求解P,P为Riccati方程的解,然而用LQR需要计算K,再将K算出. (迭代过程中 ,我们可以将此算法和dlqr函数求解的参数进行对比,当误差小于我们设置的允许误差我们就可以把此算法 ...
- Linux hostname主机名配置文件/etc/hosts详解
这篇文章为大家介绍linux hostname主机名配置文件/etc/hosts,包括主机名的用途.配置文件的操作方法等,有需要的朋友,可以参考下 1.什么是Linux主机名 无论在局域网还是INTE ...
- 利用 React 高阶组件实现一个面包屑导航
什么是 React 高阶组件 React 高阶组件就是以高阶函数的方式包裹需要修饰的 React 组件,并返回处理完成后的 React 组件.React 高阶组件在 React 生态中使用的非常频繁, ...
- windows php5.5安装redis扩展,并用redis存储session
1.确定安装版本 先通过phpinfo()查看php的Compiler.Architecture.Thread Safety,其中Thread Safety如果是enabled,那么就是线程安全(ts ...
- postman-5-授权
授权 Inherit auth from parent 假设现在将一个文件夹添加到集合中.在授权选项卡下,默认授权类型就被设置为“从父继承授权”“从父继承授权”设置表示默认情况下此文件夹中的每个请求都 ...
- 安装FeedReader添加RSS订阅
#0x1 FeedReader FeedReader是一款功能齐全,界面优美的GTK+ 3RSS阅读器客户端,用于在线RSS服务. FeedReader目前支持Feedbin,Feedly,Fresh ...
- Python之爬虫(二十二) Scrapy分布式原理
关于Scrapy工作流程回顾 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享爬取队列. 分布式架 ...
- Python函数04/生成器/推导式/内置函数
Python函数04/生成器/推导式/内置函数 目录 Python函数04/生成器/推导式/内置函数 内容大纲 1.生成器 2.推导式 3.内置函数(一) 4.今日总结 5.今日练习 内容大纲 1.生 ...
- Python面向对象06 /元类type、反射、函数与类的区别、特殊的双下方法
Python面向对象06 /元类type.反射.函数与类的区别.特殊的双下方法 目录 Python面向对象06 /元类type.反射.函数与类的区别.特殊的双下方法 1. 元类type 2. 反射 3 ...
- 区分C语言中的指针函数和函数指针
1.指针函数: 类型说明符 *函数名(形参表) { .......... /*函数体*/ .......... /*函数体*/ } 其中函数名之前加了"*"号表明,这是一 ...