【微服务】之二:从零开始,轻松搞定SpringCloud微服务系列--注册中心(一)
微服务体系,有效解决项目庞大、互相依赖的问题。目前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微服务系列--注册中心(一)的更多相关文章
- 【微服务】之四:轻松搞定SpringCloud微服务-负载均衡Ribbon
对于任何一个高可用高负载的系统来说,负载均衡是一个必不可少的名称.在大型分布式计算体系中,某个服务在单例的情况下,很难应对各种突发情况.因此,负载均衡是为了让系统在性能出现瓶颈或者其中一些出现状态下可 ...
- 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign
上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...
- 【微服务】之六:轻松搞定SpringCloud微服务-API网关zuul
通过前面几篇文章的介绍,我们可以轻松搭建起来微服务体系中比较重要的几个基础构建服务.那么,在本篇博文中,我们重点讲解一下,如何将所有微服务的API同意对外暴露,这个就设计API网关的概念. 本系列教程 ...
- 【微服务】之七:轻松搞定SpringCloud微服务-API权限控制
权限控制,是一个系统当中必须的重要功能.张三只能访问输入张三的特定功能,李四不能访问属于赵六的特定菜单.这就要求对整个体系做一个完善的权限控制体系.该体系应该具备针区分用户.权限.角色等各种必须的功能 ...
- 【微服务】之三:从零开始,轻松搞定SpringCloud微服务-配置中心
在整个微服务体系中,除了注册中心具有非常重要的意义之外,还有一个注册中心.注册中心作为管理在整个项目群的配置文件及动态参数的重要载体服务.Spring Cloud体系的子项目中,Spring Clou ...
- 从零开始,轻松搞定SpringCloud微服务系列
本系列博文目录 [微服务]之一:从零开始,轻松搞定SpringCloud微服务系列–开山篇(spring boot 小demo) [微服务]之二:从零开始,轻松搞定SpringCloud微服务系列–注 ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 带你十天轻松搞定 Go 微服务系列(一)
本文开始,我们会出一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建(本文) 服务拆分 用户服务 产品服务 订单服务 支付服务 RPC 服务 Au ...
随机推荐
- ios 返回不会自动刷新页面问题
在实际开发过程中,移动端的兼容性问题有很大的坑,安卓可以了ios不行,ios可以了安卓又失效了这样,其中ios的回退操作就是不会自动刷新页面,很烦! 常见的history.back() history ...
- Python3处理HTML获取所需内容
处理HTML页面,经常使用的便是使用beautifulsoup库 pip install beautifulsoup4 执行上述语句下载bs4库 一般请求下来的所需数据都位于tbody的tr标签里,下 ...
- Python学习笔记(二)-Python文件类型及编程模式
Python环境搭建:linux,Windows... Linux下:[root@localhost StudyPython]# python #进入交互模式Python 2.7.11 (defaul ...
- Linux系列教程(四)——Linux文件和目录处理命令
这个系列教程的前面我们讲解了如何安装Linux系统,以及学习Linux系统的一些方法.那么从这篇博客开始,我们就正式进入Linux命令的学习.学习命令,首先要跟大家纠正的一点就是,我们不需要记住每一条 ...
- MongoDB复制
1. 什么是复制 (1)MongoDB复制是将数据同步在多个服务器的过程. (2)复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. (3)复制还允 ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- Python对于CSV文件的读取与写入
今天天气"刚刚好"(薛之谦么么哒),无聊的我翻到了一篇关于csv文件读取与写入的帖子,作为测试小白的我一直对python情有独钟,顿时心血来潮,决定小搞他一下,分享给那些需要的小白 ...
- 基于HTML5及WebGL的工控SCADA模拟飞机飞行
昨天看到一篇文章说是学习如何开飞机的,然后我就想,如果我也可以开飞机那就好玩了,每个人小时候都想做飞行员!中国飞行员太难当了,再说也不轻易让你开飞机!后来我就想如果能用 HT 开飞机那就是真的有趣了, ...
- 聊聊pthread_cond_wait的虚假唤醒
使用条件变量时,仅仅从pthread_cond_wait返回就说条件成立是不恰当的.我们正确使用pthread_cond_wait的唯一方式是当线程被从pthread_cond_wait唤醒时,再检查 ...
- 微信小程序之页面下拉刷新
如果需要给单个页面设置下拉刷新功能,不需要写在""window"对象里面,直接在 文件名称.json 里面设置即可 { "enablePullDownRefr ...