【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 ...
随机推荐
- 以EF形式操作mysql数据库
1.引入Nuget包: 2.书写EF操作上下文 public class MySqlContext:DbContext { protected override void OnConfiguring( ...
- 简单使用Spring Boot+JpaRepository+hibernate搭建项目
sql: -- -------------------------------------------------------- -- 主机: 127.0.0.1 -- 服务器版本: 10.3.9-M ...
- ViewController里的loadView和viewDidLoad什么区别
当你访问一个ViewController的view属性时,如果此时view的值是nil,那么,ViewController就会自动调用loadView这个方法.这个方法就会加载或者创建一个view对象 ...
- iOS视频播放(AVFoundation)
iOS视频播放(AVFoundation) 关于iOS平台的音视频处理,苹果官方提供了OC和swift接口的AVFoundation框架,可以进行各种音频播放和剪辑,底层实现使用了GPU加速,编解码效 ...
- 浅析MySQL 5.7组复制技术(Group Replication)
Group Replication is know as an up to date HA(High Availablity) solution which is supported in ...
- Ubuntu18.04安装完应该做的一些事 显卡驱动安装和cuda8.0
博主装Ubuntu18.04主要是为了用于跑深度学习,所以我们先来搞搞gcc环境 第一步:安装多版本gcc.g++可切换 sudo apt-get install gcc-4.8 gcc-4.8-mu ...
- 关于mybatis编写sql问题.
最近呢楼主回到长沙进行面试:被问了一个这样的问题,在mybatis中怎么进行模糊查询,望各位大佬在下方进行评论,好让我这菜鸡多学习一些.
- vue 项目 切换手机端和pc端。同一个项目,配置不同的路由
1, 首先判断设备:在main.js里面写 // vue原型挂载 - 是否PC端 if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator. ...
- Elasticsearch 数据操作
一.新增数据 1.1 随机生成id 语法: POST /索引库名/类型名 { "key1": "value1", "key2": " ...
- kubernetes基础使用
查看节点详细信息 kubectl describe node vm2 显示版本号 kubectl version Client Version: version.Info{Major:"1& ...