【SpringCloud】 第十篇: 高可用的服务注册中心
前言:
必需学会SpringBoot基础知识
简介:
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2017.3 x64
文章 SpringCloud 教程 第一篇: 服务的注册与发现(Eureka) 介绍了服务注册与发现,其中服务注册中心Eureka Server,是一个实例,当成千上万个服务向它注册的时候,它的负载是非常高的,这在生产环境上是不太合适的,这篇文章主要介绍怎么将Eureka Server集群化。

一、准备工作
创建三个实例:
- eureka-server-peer1
- eureka-server-peer2
- eureka-server-peer3
二、Code.View
2.0 peer*
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lwc</groupId>
<artifactId>eureka-server-peer1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-server-peer1</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.1 peer1
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* @author Eddie
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerPeer1Application { public static void main(String[] args) {
SpringApplication.run(EurekaServerPeer1Application.class, args);
}
}
server:
port: 8861
spring:
application:
name: eureka-server-peer1
profiles:
active: peer1
eureka:
instance:
hostname: eureka-server-peer1
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://eureka-server-peer2:8862/eureka/,http://eureka-server-peer3:8863/eureka/
2.2 peer2
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* @author Eddie
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerPeer2Application { public static void main(String[] args) {
SpringApplication.run(EurekaServerPeer2Application.class, args);
}
}
server:
port: 8862
spring:
application:
name: eureka-server-peer2
profiles:
active: peer2
eureka:
instance:
hostname: eureka-server-peer2
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://eureka-server-peer2:8861/eureka/,http://eureka-server-peer3:8863/eureka/
2.3 peer3
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /**
* @author Eddie
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerPeer3Application { public static void main(String[] args) {
SpringApplication.run(EurekaServerPeer3Application.class, args);
}
}
server:
port: 8863
spring:
application:
name: eureka-server-peer3
profiles:
active: peer3
eureka:
instance:
hostname: eureka-server-peer3
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://eureka-server-peer2:8862/eureka/,http://eureka-server-peer3:8861/eureka/
2.4 改造 eureka-client
package com.lwc.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName EurekaClientController
* @description
* @date created in 2018-03-26 17:19
* @modified by
*/
@RestController
@RequestMapping("/eureka")
public class EurekaClientController { @Autowired
private DiscoveryClient discoveryClient; @GetMapping("/client")
public String home(@RequestParam String name) {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>(16);
List<String> stringList = discoveryClient.getServices();
stringList.forEach(str -> {
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(str);
serviceInstances.forEach(ins -> {
map.put("host", ins.getHost());
map.put("port", ins.getPort());
map.put("serviceId", ins.getServiceId());
map.put("uri", ins.getUri());
list.add(map);
});
}); String url = null; for (Map<String, Object> m : list) {
for (String k : m.keySet()) {
System.out.println(k + " : " + m.get(k));
url = "欢迎点击: "+m.get("uri") + "/eureka/client?name=" + name;
}
}
return url;
} }
eureka:
client:
serviceUrl:
#defaultZone: http://localhost:8761/eureka/
defaultZone: http://eureka-server-peer1:8861/eureka/
server:
port: 8762
三、测试
启动:


最后改造 client 是为了让大家学习一下获取信息,访问: http://localhost:8762/eureka/client?name=eddie
欢迎点击: http://EDDIE-THINKPAD:8762/eureka/client?name=eddie
题外话:
也可以通过原来第一篇的项目结构, 修改配置文件的端口达到效果;
1. 通过Maven打包之后
2. CMD下运行:
java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer2
3. 在运行:
java -jar eureka-client-0.0.1-SNAPSHOT.jar
技巧:
eureka.instance.preferIpAddress=true是通过设置ip让eureka让其他服务注册它。也许能通过去改变去通过改变host的方式。
原理:
peer1 8861,peer2 8862,peer2 8863 相互感应,当有服务注册时,两个Eureka-server是对等的,它们都存有相同的信息,这就是通过服务器的冗余来增加可靠性,当有一台服务器宕机了,服务并不会终止,因为另一台服务存有相同的数据。
三、源码下载
标签 10-1
https://github.com/eddie-code/SpringCloudDemo
【SpringCloud】 第十篇: 高可用的服务注册中心的更多相关文章
- 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)
转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...
- SpringCloud教程 | 第十篇: 高可用的服务注册中心
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 微服务配置内容《网上copy》=========》如何创建一个高可用的服务注册中心
前言:首先要知道什么是一个高可用的服务注册中心,基于spring boot建成的服务注册中心是一个单节点的服务注册中心,这样一旦发生了故障,那么整个服务就会瘫痪,所以我们需要一个高可用的服务注册中心, ...
- SpringCloud 教程 (三)高可用的服务注册中心
一.准备工作 Eureka can be made even more resilient and available by running multiple instances and asking ...
- springcloud 高可用的服务注册中心
https://blog.csdn.net/forezp/article/details/81041101 上面是方老师的博客,看liuyan也有好多同学不是很清楚,这里自己也记录一下具体的做法. 1 ...
- 搭建高可用的Eureka注册中心
搭建高可用的Eureka注册中心 一.搭建高可用的Eureka的作用 当服务器因种种原因导致Eureka注册中心(后面简称Eureka)服务当机(服务器跪了,异常关闭停止服务).这样就会影响到整个业务 ...
- 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
随机推荐
- dropout总结
1.伯努利分布:伯努利分布亦称“零一分布”.“两点分布”.称随机变量X有伯努利分布, 参数为p(0<p<1),如果它分别以概率p和1-p取1和0为值.EX= p,DX=p(1-p). 2. ...
- 使用IPDB调试Python代码
(转载自:https://xmfbit.github.io/2017/08/21/debugging-with-ipdb/) IPDB是什么?IPDB(Ipython Debugger),和GDB类似 ...
- DU1525 Euclid's Game 博弈
HDU1525 Euclid's Game 博弈 题意 给定两个数字 a, b. 每次只能用 较大的值 减去 较小的值的倍数, 两个人轮流进行操作, 第一个得到 0 的胜利. 分析 对于 a == b ...
- Number & Math
Java Number & Math 类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 实例 int a = 5000; floa ...
- Struts2-01
一.Struts2的介绍 讲Struts2框架之前,我们需要知道框架是什么呢?估计大多数初学者都只知道其名却不知其意,框架就是一个半成品,别人将一些功能已经写好了,我们只需要拿来用即可,像我们之前使用 ...
- 增加oracle表空间
查找用户对应的表空间 1.查询表空间物理文件路径select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_ ...
- hdu_4336_Card Collector
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- thinkphp5实现定位功能
一.所需资源链接:百度网盘.主要包含一个ip地址库和一个ip类文件. 二.下载好后,在extend目录下面创建一个location的目录,将下载的文件解压到该目录.给类文件增加一个命名空间,便于我们使 ...
- QQ空间认证之数据篇
最近,我们发现可以利用开通企鹅媒体平台的方式开通QQ公众号从而绑定我们的QQ号,这样我们所绑定的QQ号就成了认证空间了. 虽说这样很快捷的就认证了我们的QQ空间,但是,起有利也有弊.任何事情都不是十全 ...
- Laravel 开发支付宝支付与提现转账问题小结
由于项目需要,所以需要开发支付宝支付与微信支付,支付部分采用了 yansongda/pay https://packagist.org/packages/yansongda/pay https ...