文章大纲

一、Spring Cloud基础知识介绍
二、创建单一的服务注册中心
三、创建一个服务提供者
四、搭建高可用服务注册中心
五、项目源码与参考资料下载
六、参考文章

 

一、Spring Cloud基础知识介绍

https://www.cnblogs.com/WUXIAOCHANG/p/10888500.html

二、创建单一的服务注册中心

1. 简介

Spring Cloud是基于Spring Boot的基础进行的,所以我们需要创建一个普通的Spring Boot工程,命名为eureka-server

2. idea创建基础的Spring Boot项目

 
 

创建后项目结构如下:

 

3. pom.xml添加相关依赖

目前eureka的稳定版本是Dalston.SR3,下面添加了Spring Boot和Spring Cloud相关依赖

<?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.wxc</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-server</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <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>Dalston.SR3</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>

4. 新增相关静态资源文件

 

其中static和templates文件夹为空的,其中application.properties文件内容如下:

#单机下配置
#1.server.port=1111表示设置该服务注册中心的端口号
#2.eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
#3.eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,
# 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
#4.eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

温馨提示
(1)application.properties文件中配置了服务注册中心的信息
(2)具体服务注册中心信息如下:
  1)server.port=1111表示设置该服务注册中心的端口号
  2)eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
  3)eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
  4)eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务

5. 添加项目启动入口

在main下新建com.wxc.test包,并新建入口类EurekaServerApplication.java

 

EurekaServerApplication.java内容如下:

package com.wxc.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

6. 运行项目并访问

6.1 运行项目

 

6.2 运行结果

 

可以看到,运行成功了。

6.3 项目访问
浏览器输入http://localhost:1111,访问情况如下:

 

此时代表服务注册中心启动并访问成功

三、创建一个服务提供者

1. idea创建基础的Spring Boot项目

创建名为eureka-provider的Spring Boot项目

 
 
 
 

创建后项目结构如下;

 

2. pom.xml添加相关依赖

<?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>org.sang</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
</properties>
<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>
<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>Dalston.SR3</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>

3. 新增相关静态资源文件

 

application.properties文件内容如下:

server.port=8080
#配置一下服务名和注册中心地址
spring.application.name=hello-service
#注册单个服务
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

4. Controller添加访问入口

在com.wxc.test包下新建HelloController.java

package com.wxc.test.controller;

import org.apache.log4j.Logger;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class HelloController { private final Logger logger = Logger.getLogger(getClass()); @Autowired
private DiscoveryClient discoveryClient; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
List<ServiceInstance> instances = discoveryClient.getInstances("hello-service");
for (int i = 0; i < instances.size(); i++) {
logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
}
return "Hello World";
}
}

这里创建服务之后,在日志中将服务相关的信息打印出来,创建后项目结构如下:

 

5. 添加项目启动入口

在com.wxc.test包下新建ProviderApplication.java,在Spring Boot的入口函数处,通过添加@EnableDiscoveryClient注解来激活Eureka中的DiscoveryClient实现(因为我们在HelloController中注入了DiscoveryClient)

package com.wxc.test;

        import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}

6. 运行项目并访问

6.1 运行项目
首先确保eureka-server注册中心处于启动,可以刷新一下原先的浏览器访问,如下代表启动成功

在eureka-provider中进行项目启动

 
 

6.2 项目访问
http://localhost:1111服务注册中心中点击刷新,之后可以看到服务提供者已经注册成功

 
 

7. 总结

如此之后,我们一个服务注册中心就搭建成功了,同时也有一个服务提供者成功的注册了。但是这样还有一个小问题,那就是我们这里是一个单节点的服务注册中心,一旦发生了故障整个服务就瘫痪了,所以在实际应用中,我们需要搭建高可用注册中心,高可用注册中心将在第四点中进行讲解

四、搭建高可用服务注册中心

1. 简介

上面搭建好的服务注册中心是一个单节点的服务注册中心,这样一旦发生了故障,那么整个服务就会瘫痪,所以我们需要一个高可用的服务注册中心,那么在Eureka中,我们通过集群来解决这个问题。Eureka Server的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就会形成一组互相注册的服务注册中心,进而实现服务清单的互相同步,达到高可用的效果。

2. 服务注册中心创建与配置

我们需要新建一个服务注册中心,项目名为:eureka-server2,具体创建方式参考二中的内容,创建后项目结构与内容如下:

 

3. 修改配置文件

3.1 eureka-server修改
修改application.properties文件内容如下:

#(1)在peer1的配置文件中,让它的service-url指向peer2,在peer2的配置文件中让它的service-url指向peer1
#由于peer1和peer2互相指向对方,实际上我们构建了一个双节点的服务注册中心集群
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:1112/eureka/

3.2 eureka-server2修改
修改application.properties文件内容如下:

#1.server.port=1111表示设置该服务注册中心的端口号
#2.eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
#3.eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,
# 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
#4.eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

温馨提示
(1)在peer1的配置文件中,让它的service-url指向peer2,在peer2的配置文件中让它的service-url指向peer1
(2)为了让peer1和peer2能够被正确的访问到,我们需要在C:\Windows\System32\drivers\etc目录下的hosts文件总添加两行配置,如下:
127.0.0.1 peer1
127.0.0.1 peer2
(3)由于peer1和peer2互相指向对方,实际上我们构建了一个双节点的服务注册中心集群

4. 启动集群服务注册中心

分别运行两个服务注册中心的启动类

 
 

访问http://localhost:1111/http://localhost:1112/,出现以下内容

 
 

我们可以看到,在server1的节点的我们已经可以看到server2节点了,在server2的中我们也可以看到server1节点了。如此之后,我们的服务注册中心集群就搭建好了,然后我们可以做一个简单的测试。

5. 进行集群测试

5.1 修改eureka_provdier中配置

 

5.2 启动eureka_provdier项目

 
 

5.3 访问集群注册中心

 
 

大家可以看到两个服务注册中心都发现了服务提供者了。

五、项目源码与参考资料下载

链接:https://pan.baidu.com/s/1UKgV6Kz9DdguzXOzPONKJg
提取码:gug4

六、参考文章

https://www.cnblogs.com/lenve/p/7985943.html

搭建高可用服务注册中心-Spring Cloud学习第一天(非原创)的更多相关文章

  1. 使用Spring Cloud搭建高可用服务注册中心

    我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客[使用Spring Cloud搭建服务注册中心]中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭 ...

  2. 使用SpringCloud搭建高可用服务注册中心

    我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭建好的服务注册中心是一个单节点的服务注册中心,这 ...

  3. spring cloud 入门系列三:使用Eureka 搭建高可用服务注册中心

    在上一篇中分享了如何使用Eureka 进行服务治理,里面搭建的服务注册中心是单体的, 但是在实际的应用中,分布式系统为了防止单体服务宕机带来严重后果,一般都会采用服务器集群的形式,服务注册中心也是一样 ...

  4. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  5. 搭建服务与负载均衡的客户端-Spring Cloud学习第二天(非原创)

    文章大纲 一.Eureka中的核心概念二.Spring RestTemplate详解三.代码实战服务与负载均衡的客户端四.项目源码与参考资料下载五.参考文章 一.Eureka中的核心概念 1. 服务提 ...

  6. Spring Cloud第三篇 | 搭建高可用Eureka注册中心

    ​ ​本文是Spring Cloud专栏的第三篇文章,了解前两篇文章内容有助于更好的理解后面文章: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

  7. Spring Cloud Eureka 4 (高可用服务注册中心)

    在微服务这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须考虑对各个组件进行高可用部署,对于服务注册中心也是一样. Eureka Server 的高可用实际上就是讲自己作为服务向 ...

  8. springboot+cloud 学习(一)高可用服务注册中心(Eureka)

    先说说Eureka Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringClo ...

  9. SpringCloud的服务注册中心(四)- 高可用服务注册中心的搭建

    一.双 服务注册注册中心 1.服务注册中心的服务端 - EurekaServer 1.1.EurekaServer1 String.application.name=eureka-server ser ...

随机推荐

  1. leetcode-11-dfs

    DFS算法: explore(G, v) visited(v) = trueprevisit(v) for each edge(v, u) in E: if not visited(u): explo ...

  2. jenkins的构建项目配置

    继http://www.cnblogs.com/yajing-zh/p/5109517.html搭建好jenkins系统配置之后,新建jenkins构建项目,用于自动化构建. 点击Jenkins界面左 ...

  3. 光学字符识别OCR-3

    连通性 可以看到,每一层的图像是由若干连通区域组成的,文字本身是由笔画较为密集组成的,因此往往文字也能够组成一个连通区域.这里的连通定义为 8邻接,即某个像素周围的8个像素都定义为邻接像素,邻接的像素 ...

  4. Oracle 10g Data Pump Expdp/Impdp 详解

    Data Pump 介绍 在第一部分看了2段官网的说明, 可以看出数据泵的工作流程如下: (1)在命令行执行命令 (2)expdp/impd 命令调用DBMS_DATAPUMP PL/SQL包. 这个 ...

  5. 为什么要使用数据库连接池?以及用法(DBUtils)

    看代码, from flask import Flask from db import POOL import pymysql app = Flask(__name__) app.secret_key ...

  6. K-means算法的优缺点

    K-means算法的优缺点 优点:原理简单,实现容易 缺点: 收敛较慢 算法时间复杂度比较高 \(O(nkt)\) 不能发现非凸形状的簇 需要事先确定超参数K 对噪声和离群点敏感 结果不一定是全局最优 ...

  7. linux磁盘问题排查

    一.ls $>>ls /data* //查看有无input/output error报错 二.demsg $>>demsg|grep sd 问题盘结果: 三.iostat使用 ...

  8. 【Luogu】P4363一双木棋(状压爆搜)

    题目链接 唉,只有AC了这道题才会感叹考场上没有想出解法的我是多么智障. 我甚至连任何想法都没有. 天啊我当时到底在想些什么. AC这道题我就能进前15了诶. 我们发现只要确定了轮廓线那么此时的状态就 ...

  9. SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  10. NOJ——1672剪绳子(博弈)

    [1672] 剪绳子 时间限制: 500 ms 内存限制: 65535 K 问题描述 已知长度为n的线圈,两人依次截取1~m的长度,n, m为整数,不能取者为输. 输入 输入n, m:( 0 < ...