文章大纲

一、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. Thonny -- 简洁的 python 轻量级 IDE

    Thonny目前是 树莓派 上 默认的 Python 开发环境. 该 IDE 是 Institute of Computer Science of University of Tartu (爱沙尼亚 ...

  2. JAVA基础篇—Servlet小结

    一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...

  3. UVa 1407 树形背包 Caves

    这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...

  4. python基础学习笔记—— 多继承

    本节主要内容: 1.python多继承 2.python经典类的MRO 3.python新式类的MRO.C3算法 4.super是什么鬼? 一.python多继承 在前⾯的学习过程中. 我们已经知道了 ...

  5. MyBatis配置文件中报错:URI is not registered(Settings | Languages & Frameworks | Schemas and DTDs)

    如下错误: 解决办法: 在file->Settings中添加如下图所示: URI为出现红色部分的地址 点击OK后会发现: 这样就解决了!

  6. centos7下nginx添加到自定义系统服务中提示Access denied

    安装好nginx后,添加到系统服务中 在/usr/lib/systemd/system下创建nginx.service文件内容如下: [Unit] Description=nginx - high p ...

  7. Invalid regular expression flags 错误

    找到写正则表达式的地方,检查是不是写了一个非法的正则表达式. Invalid regular expression flags

  8. 【Luogu】U16325小奇的花园(树链剖分)

    题目链接 学了学动态开点的树链剖分,其实跟动态开点的线段树差不多啦 查询的时候别ssbb地动态开点,如果没这个点果断返回0就行 只要注意花的种类能到intmax就行qwq!!!! #include&l ...

  9. 洛谷P4363 [九省联考2018]一双木棋chess 【状压dp】

    题目 菲菲和牛牛在一块n 行m 列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手. 棋局开始时,棋盘上没有任何棋子,两人轮流在格子上落子,直到填满棋盘时结束. 落子的规则是:一个格子可以落子当且仅当这个 ...

  10. 微信小程序答题系统实现随机出题 答题小程序如何实现随机出题 微信小程序 答题系统

    最近头脑王者非常火爆,公司也在开发类似头脑王者的答题系统,这个重任交到我这边来了,我们在开发的这个微信小程序答题系统,需要实现随机出题.尤其是一些比如闯关的环节,需要随机从题库里抽取若干道题目,给到用 ...