微服务体系,有效解决项目庞大、互相依赖的问题。目前SpringCloud体系有强大的一整套针对微服务的解决方案。本文中,重点对微服务体系中的服务发现注册中心进行详细说明。本篇中的注册中心,采用Netflix 公司的Eureka。

本系列教程列表:

【微服务】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)

【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)

注册中心简介

Netflix Eureka:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移。他包含很多功能,本文重点讲解它的服务注册中心。

官方解释

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing.

Eureka支持服务动态扩容、缩容、失效剔除。

Eureka提供了完整的Service Registry和Service Discovery实现,与现有Spring Cloud完美融合。

注册中心服务原理

由上图可以看出,蓝色部分为注册中心,黄色部分为一个生产者、消费者。所有应用都被同一个注册中心纳入管理。通常有Register(服务注册)、Renew(服务续约)、Cancel(服务下线)等操作。

环境清单

JDK: 1.8

Maven:3.x+

IDE:idea

开始起飞

为了后续博文的开展,我们约定所有子系统都放置在一个父类项目下,采用Idea的模块开发机制,对所有子系统进行统一仓库管理,方便交流学习。

创建父类项目

使用idea创建一个maven项目(创建之前设置好maven、jdk版本)。

然后在pom.xml文件中加入以下超级父类依赖。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

创建子模块

通过上面箭头提示的Module创建模块项目,流程与新建项目类似。

创建完成以后在子项目pom文件中加入以下依赖。

配置pom文件

<!--依赖管理-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--资源库管理-->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!--依赖管理中心-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!--构建中心-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- defined in spring-cloud-starter-parent pom (as documentation hint),
but needs to be repeated here -->
<configuration>
<requiresUnpack>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-core</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>

设置完成以后,开始主体设置,创建包>>创建主方法>>配置yml文件

项目目录结构

新建Application.java文件


/**
* @Description : Eureka 服务发现server 启动类
* @Author hanyahong
* @Date 2017/12/4- 16:14
*/
@SpringBootApplication
@EnableEurekaServer
public class Application { public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
} }

说明:

@EnableEurekaServer :服务发现服务端注解,设置以后将作为服务注册中心。

@SpringBootApplication:springboot启动主程序注解。

配置application.yml文件

在位于resources的文件夹下面创建该文件,该文件作为服务的配置文件,可以配置相关子项目参数。

#server 端口号设置
server:
port: 8081 #注册中心设置,server本身不被发现
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0

至此,一个完整的服务注册中心基本搭建完毕。可以进行启动测试。

验证是否成功

我们可以在Terminal看到以下信息,说明启动成功。


. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)
...
2017-12-04 18:04:30.780 INFO 32476 --- [ Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-12-04 18:04:30.823 INFO 32476 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-12-04 18:04:30.825 INFO 32476 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8081
2017-12-04 18:04:30.830 INFO 32476 --- [ main] com.hyh.Application : Started Application in 14.784 seconds (JVM running for 15.647)
2017-12-04 18:05:30.759 INFO 32476 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms

通过浏览器访问 http://localhost:8081/ 可以看到已经启动正常。

使用Maven 插件进行打包

提醒:在打包之前需要本地已经安装好Git/Maven并配置好环境变量。博主因为不喜欢使用Idea自带的JDK/MAVEN/GIT,都是单独安装的。这个看个人喜好吧。

另外,博主喜欢使用命令进行驱动打包,因此对Idea的Terminal进行了重新配置,将Win下面CMD替换成了Git.bash,如果有同学感兴趣可以自行体验。

IDEA 默认Terminal 修改

打开 File>>Settings,找到修改配置选项



然后将默认的Shell地址改成Git.

设置完成以后进行保存。回到我们界面可以通过Alt + F12 快速打开shell窗口。

正式开始打包

首先使用pwd 查看所在的目录,应该是父类项目的根目录,ls 命令或者dir 查看,该目录下面的文件与文件夹,我们需要跳转到子目录下面,进行子项目(服务注册中心)打包。所以使用 cd 命令跳转子目录

使用命令mvn clean package 就可以实现maven打包。可能会先下载一些依赖,完了就会开始打包操作。

Maven打包

mvn clean package

执行命令后出发maven构建,如下。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cloud-hyh-discovery-eureka V1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ cloud-hyh-discovery-eureka ---
[INFO] Deleting E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cloud-hyh-discovery-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ cloud-hyh-discovery-eureka ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cloud-hyh-discovery-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ cloud-hyh-discovery-eureka ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ cloud-hyh-discovery-eureka ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ cloud-hyh-discovery-eureka ---
[INFO] Building jar: E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target\cloud-hyh-discovery-eureka-V1.0.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ cloud-hyh-discovery-eureka ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.984 s
[INFO] Finished at: 2017-12-04T19:24:35+08:00
[INFO] Final Memory: 37M/326M
[INFO] ------------------------------------------------------------------------

至此,一个完整的服务注册中心,我们就开发并打包完毕。可以通过**target ** 文件夹找到刚刚生成的jar文件。进行独立启动了。

使用命令可以启动独立的jar保程序。

java -jar XXX.jar

Linux环境下,后台启动命令

nohup java -jar xxx.jar &

备注: Linux查看端口对于的启动程序命令,XXXX代表端口号

netstat -anp|grep XXXX

通过以上命令查到程序对于的信息(包括PID)。如果想停止,可以通过PID(进程ID)杀死相关进程。

kill -9 PID

博客源码地址

Github地址:https://github.com/hanyahong/spring-cloud-microservice

码云地址:https://gitee.com/hyhvpn/spring-cloud-microservice

【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)的更多相关文章

  1. 【微服务】之四:轻松搞定SpringCloud微服务-负载均衡Ribbon

    对于任何一个高可用高负载的系统来说,负载均衡是一个必不可少的名称.在大型分布式计算体系中,某个服务在单例的情况下,很难应对各种突发情况.因此,负载均衡是为了让系统在性能出现瓶颈或者其中一些出现状态下可 ...

  2. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

  3. 【微服务】之六:轻松搞定SpringCloud微服务-API网关zuul

    通过前面几篇文章的介绍,我们可以轻松搭建起来微服务体系中比较重要的几个基础构建服务.那么,在本篇博文中,我们重点讲解一下,如何将所有微服务的API同意对外暴露,这个就设计API网关的概念. 本系列教程 ...

  4. 【微服务】之七:轻松搞定SpringCloud微服务-API权限控制

    权限控制,是一个系统当中必须的重要功能.张三只能访问输入张三的特定功能,李四不能访问属于赵六的特定菜单.这就要求对整个体系做一个完善的权限控制体系.该体系应该具备针区分用户.权限.角色等各种必须的功能 ...

  5. 【微服务】之三:从零开始,轻松搞定SpringCloud微服务-配置中心

    在整个微服务体系中,除了注册中心具有非常重要的意义之外,还有一个注册中心.注册中心作为管理在整个项目群的配置文件及动态参数的重要载体服务.Spring Cloud体系的子项目中,Spring Clou ...

  6. 从零开始,轻松搞定SpringCloud微服务系列

    本系列博文目录 [微服务]之一:从零开始,轻松搞定SpringCloud微服务系列–开山篇(spring boot 小demo) [微服务]之二:从零开始,轻松搞定SpringCloud微服务系列–注 ...

  7. 【微框架】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  8. 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  9. 带你十天轻松搞定 Go 微服务系列(一)

    本文开始,我们会出一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建(本文) 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Au ...

随机推荐

  1. Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)

    前言:当我们想在页面上给客户端返回一个当前时间,一些初学者可能会很自然的想到用占位符,字符串拼接来达到我们想要的效果,但是这样做会有一个问题,HTML被直接硬编码在 Python代码之中. 1 2 3 ...

  2. 想要写出高性能sql语句,你得记住这些……

    1.在from子句中包含多个表名时,必须选择记录条数最少的表作为基础表:若有三个以上的表连接查询,那就需要选择交叉表作为基础表,交叉表指被其他表引用的表. 2.使用exists代替in.使用not e ...

  3. Fastify 系列教程四 (求对象、响应对象和插件)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...

  4. (function($){...})(jQuery)和$(document).ready(function(){}) 的区别

    (function($){...})(jQuery)  实际上是执行()(para)匿名函数,只不过是传递了jQuery对象.   立即执行函数:相当于先申明一个函数,声明完后直接调用: 用于存放开发 ...

  5. 【唯星宠物】——CSS/BootStrap/Jquery爬坑之响应式首页

    前言:唯星宠物产品官网,分为首页.子页和登录注册页三个页面,除网页内容设计与图片素材的部分使用网上的材料之外,其余内容呈现以及功能模块全部为自己重构. 一.响应式轮播banner 思路:使用BootS ...

  6. Redis的各项功能解决了哪些问题?

    先看一下Redis是一个什么东西.官方简介解释到:Redis是一个基于BSD开源的项目,是一个把结构化的数据放在内存中的一个存储系统,你可以把它作为数据库,缓存和消息中间件来使用.同时支持string ...

  7. Azkaban安装部署

    在root的用户下搭建的 • Azkaban安装部署(可参照:http://azkaban.github.io/azkaban/docs/latest/) 1):前提 安装JDK,安装Hadoop,H ...

  8. Formatting the event object

    尽量将IE与DOM函数事件对象不同的性质或方法转成DOM标准   EventUtil.formatEvent = function (oEvent) {    if (isIE && ...

  9. 通讯框架 T-io 学习——给初学者的Demo:ShowCase设计分析

    前言 最近闲暇时间研究Springboot,正好需要用到即时通讯部分了,虽然springboot 有websocket,但是我还是看中了 t-io框架.看了部分源代码和示例,先把helloworld敲 ...

  10. WebSocket小插件

    一.WebSocket小介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信 ...