Eureka

重点在使用,概念和源码基本不涉及

Eureka是一个基于REST(REST是HTTP协议的)的服务,主要在亚马逊网络服务(AWS)云中使用,定位服务来进行中间层服务器的均衡负载和故障转移.

Spring Cloud封装Eureka来实现服务注册和发现,Eureka采用了C-S的设计架构,Eureka Server作为服务注册功能的服务器,是服务注册中心,系统中的其他微服务都是Eureka Client,连接到Eureka Server,并维持心跳连接.这样就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。

Eureka由两个组件组成: Eureka Server和Eureka Client. Eureka Server用作服务注册服务器.Eureka Client就是Java客户端,分为两种,一种是Service Provider,一种是Service Consume,但是两者不是严格的概念区分,也就是说一个Service Consume也可以是Service Provider,看实际的场景.

也就是Eureka Server提供服务的注册和发现

Service Provider将自身服务注册到Eureka Server,并保持心跳续约等

Service Consumer从Eureka Server获取注册列表,进行服务消费,也就是通过远程调用与Service Provider进行通信.

搭建Eureka Server

  1. 创建项目,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  1. 启动类添加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerSmileApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerSmileApplication.class, args);
} }
  1. 配置文件
spring:
application:
name: spring-cloud-eureka-server
server:
port: 8761
eureka:
client:
# 是否注册自己
register-with-eureka: false
# 是否从Eureka Server获取注册信息
fetch-registry: false
# 设置与Eureka Server交互的地址,多个地址使用逗号合开,也就是集群
service-url:
default-zone: http://localhost:${server.port}/eureka/
  1. 启动项目,直接访问http://localhost:8761/就可以看到Eureka Server的主界面,这个时候No instances available.

集群的搭建(3个)

  1. 创建项目添加依赖(同上)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  1. 启动类添加注解(同上)
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerSmileJqApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerSmileJqApplication.class, args);
} }
  1. 配置文件,三个配置文件,第一个个application-master.properties:
server.port=8761
eureka.instance.hostname=master
eureka.client.serviceUrl.defaultZone=http://slave1:8762/eureka/,http://slave2:8763/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

第二个application-slave1.properties

server.port=8762
eureka.instance.hostname=slave1
eureka.client.serviceUrl.defaultZone=http://master:8761/eureka/,http://slave2:8763/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

application-slave2.properties

server.port=8763
eureka.instance.hostname=slave2
eureka.client.serviceUrl.defaultZone=http://master:8761/eureka/,http://slave1:8762/eureka/
spring.application.name=eureka-server
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

之后找到本地的hosts文件,推荐使用everything软件,可以搜索全盘,找到hosts文件添加下面内容:

127.0.0.1 	master
127.0.0.1 slave1
127.0.0.1 slave2

进行打包,运行

mvn clean package
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=master
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave1
java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave2

启动完成后进行访问就可以看到效果,这次进行了注册.

服务注册与调用实战

这个就要说到上面的Eureka的三个角色,一个注册中心,一个服务提供者,一个服务消费者,也就是Eureka Server,Service Provider(案例中拼错了,写成producter了,谅解),Service Consumer.中间还还涉及到远程过程调用Feign,这里只需要看一下就好,后面再说这个.

  1. 创建Eureka Service项目,添加依赖,添加注解,修改配置文件如下展示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> @SpringBootApplication
@EnableEurekaServer
public class ServerSmileDemoApplication { public static void main(String[] args) {
SpringApplication.run(ServerSmileDemoApplication.class, args);
} } server.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
  1. 创建提供者,依次依赖,注解,配置文件,最后写个controller
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> @SpringBootApplication
@EnableDiscoveryClient
public class ServiceProducterDemoApplication { public static void main(String[] args) {
SpringApplication.run(ServiceProducterDemoApplication.class, args);
} } server.port=9000
spring.application.name=eureka-service-producter
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ @RestController
public class HelloController { /** 看的博客上出现了Request method ‘POST’ not supported问题,因为没有添加@RequestParam注解,说是不添加那个注解就是post请求,就算是用GetMapping指定了还是POST请求,这个本人没有测试 */
@GetMapping("hello")
public String hello(@RequestParam String name){
return "hello," + name + "1111111";
} }
  1. 创建消费者,添加依赖(这个需要添加feign的依赖),后面依次
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceConsumerDemoApplication { public static void main(String[] args) {
SpringApplication.run(ServiceConsumerDemoApplication.class, args);
} } server.port=9010
spring.application.name=eureka-service-consumer
eureka.client.service-url.defaultZone=http://localhost:8761/eureka @RestController
public class ConsumerController { @Autowired
private HelloRemote helloRemote; @GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name){
return helloRemote.hello(name);
}
}

上面按照顺序启动,先启动注册中心,然后启动提供者,最后启动消费者,访问的时候访问消费者的hello,就可以访问到提供者的hello了.

负载均衡

feign有负载均衡的作用(feign是基于Ribbon实现的,所以自带客户端负载均衡功能),在上面的基础之上,再次创建个提供者,与上面提供者不同的是端口号和controller稍微变下,具体代码如下:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> server.port=9001
spring.application.name=eureka-service-producter
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ @SpringBootApplication
@EnableDiscoveryClient
public class ServiceProducterDemo1Application { public static void main(String[] args) {
SpringApplication.run(ServiceProducterDemo1Application.class, args);
} } @RestController
public class HelloController { @GetMapping("hello")
public String hello(@RequestParam String name){
return "hello," + name + "222222";
}
}

之后同时启动四个项目,看注册中心就能看到EUREKA-SERVICE-PRODUCTER后有两个服务,之后访问http://localhost:9010/hello/wangzhi就可以看到hello,wangzhi1111111和hello,wangzhi222222交替出现,这个就是负载均衡了.

到这Eureka基本就结束了我没有看源码,这个说明下,还有人说Eureka闭源了,其实并不是,只是不继续开发2.X版本,1.X版本还在更新维护呢.而且现在阿里开源了Nacos,这个也是可以替代Eureka的,所以说替代方案还是有的.

SpringCloud学习(二)---Eureka的更多相关文章

  1. SpringCloud学习(二):微服务入门实战项目搭建

    一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...

  2. SpringCloud学习(3)——Eureka服务注册中心及服务发现

    Eureka概述: Eureka是Netflix的一个子模块, 也是核心模块之一.Eureka是一个基于REST的服务, 用于定位服务, 以实现云端中间层服务发现和故障转移.服务注册与发现对于微服务框 ...

  3. SpringCloud 学习(二)-2 :Securing The Eureka Server

    由于工作等种种原因未能连续进行学习,现在继续学习微服务,不过是新建的demo,springcloud版本用的是Finchley.SR2. 之前用简单demo实现了注册中心,现在来对注册中心加安全验证: ...

  4. SpringCloud 学习(二) :服务注册与发现Eureka

    Spring Cloud应用中可以支持多种的服务治理框架,比如Eureka.Consul.Zookeeper等,现在我们用的是consul,本文以SpringCloud Dalston.SR5版本介绍 ...

  5. SpringCloud 学习(二)-1 :服务注册与发现Eureka扩展

    上一篇介绍了Eureka Server的搭建跟配置.Eureka Client的搭建跟配置.服务间通过服务名调用等,还有几个实际实验中遇到的问题及处理方案,本篇来玩一下Eureka的其他配置. 上一篇 ...

  6. SpringCloud学习之—Eureka集群搭建

    Eureka集群的搭建 上次说过了在SpringCloud应用中使用Eureka注册中心,用来对服务提供者进行服务注册与发现,但同时,它也是一个"微服务",单个应用使用空间有限,因 ...

  7. SpringCloud学习:Eureka、Ribbon和Feign

    Talk is cheap,show me the code , 书上得来终觉浅,绝知此事要躬行.在自己真正实现的过程中,会遇到很多莫名其妙的问题,而正是在解决这些问题的过程中,你会发现自己之前思维的 ...

  8. SpringCloud学习笔记-Eureka基础

    Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的微服务治理功能. 服务端 ...

  9. SpringCloud学习之eureka集群配置

    一.集群方案及部署思路: 如果是单节点的注册中心,是无法保证系统稳定性的,当然现在项目部署架构不可能是单节点的. 集群节点的部署思路:通过运行多个实例并请求他们相互注册,来完成注册中心的高可用性(结伴 ...

随机推荐

  1. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [三] 配置式爬虫

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 上一篇介绍的基本的使用方式,自由度很高,但是编写的代码相对 ...

  2. Zabbix监控系统部署:基本功能测试

    1. 概述2. 登陆2.1 登陆账号密码2.1 设置中文语言环境3. 创建用户3.1 用户创建入口3.2 添加用户信息3.3 用户报警媒介3.4 用户权限4. 创建监控主机4.1 添加一台监控主机4. ...

  3. jenkins中配置svn 出现absolute path is not allowed

    代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处. 想用jenkins作自动化部署tomcat. svn代码已经checkout到本地目录了(/ ...

  4. Code Review —— by12061154Joy

    对结对队友刘丽萍的代码进行了复审: 优点: 1,代码逻辑正确,基本能够完全需求 2,用了不少C#自带的函数,第一次写C#,相信是查阅了不少资料,虽然还有很多地方值得优化,不过第一次能做到这样已经很不错 ...

  5. 《Linux内核设计与实现》 第五章学习笔记

    第五章 系统调用 在现代操作系统中,内核提供了进程与内核进行交互的一组接口.有如下作用: 让应用程序受限的访问硬件设备 提供了创新进程并与已有进程进行通信的机制 提供了申请操作系统其它资源的能力 保证 ...

  6. hbase 1.2.1 分布式安装

    1.机器信息 五台centos 64位机器 2.集群规划 Server Name Hadoop Cluster Zookeeper   Ensemble HBase Cluster Ip   Hado ...

  7. Atcoder C - Vacation ( DP )

    C - Vacation Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Taro' ...

  8. varnish页面缓存服务

    varnish页面缓存服务 https://www.cnblogs.com/L-dongf/p/9310144.html http://blog.51cto.com/xinzong/1782669 阅 ...

  9. Lucene源码

    看Lucene源码必须知道的基本概念 终于有时间总结点Lucene,虽然是大周末的,已经感觉是对自己的奖励,毕竟只是喜欢,现在的工作中用不到的.自己看源码比较快,看英文原著的技术书也很快.都和语言有很 ...

  10. CentOS7.3安装rz、sz命令

    安装命令: yum install lrzsz 关于rz.sz: lrzsz是一款在linux里可代替ftp上传和下载的程序.lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议. ...