文章大纲

一、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. 面试之mybatis和hibernate的区别

    mybatis是支持普通SQL查询.存储过程和高级映射的优秀持久层框架.封装了 几乎所有的JDBC代码和参数的手工设置 ,以及结果集的检索: 封装了:1,获取连接,执行sql,释放连接. 2,sql的 ...

  2. Python爬虫二

    常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...

  3. LeetCode(228) Summary Ranges

    题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...

  4. 《鸟哥的Linux私房菜》学习笔记(2)——Bash特性

    一.shell的基本概念:                               shell 意思是外壳,它是离用户最近的程序.shell提供用户操作系统的接口,我们通过shell将输入的命令与 ...

  5. JS实现——贪吃蛇

    把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...

  6. MySQL数据库详解(一)执行SQL查询语句时,其底层到底经历了什么?

    一条SQL查询语句是如何执行的? 前言 ​ 大家好,我是WZY,今天我们学习下MySQL的基础框架,看一件事千万不要直接陷入细节里,你应该先鸟瞰其全貌,这样能够帮助你从高维度理解问题.同样,对于MyS ...

  7. dependency or constituency

    what's dependenct or constituency involved in a sentence? In linguistics, when it comes to sentence ...

  8. 爬虫Scrapy框架-Crawlspider链接提取器与规则解析器

    Crawlspider 一:Crawlspider简介 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著 ...

  9. CentOS7下RabbitMQ服务安装配置胜多负少

    RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.RabbitMQ据说具有良 ...

  10. 【bzoj】P4407于神之怒加强版(莫比乌斯反演)

    题目链接 套路一般的枚举$gcd(i,j)=w$.设$min(n,m)=top$,则有 $\sum\limits_{i=1}^{n}\sum\limits_{j=1}{m}gcd(i,j)$ $=\s ...