【微服务】之二:从零开始,轻松搞定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 ...
随机推荐
- 使用Aspose.Cells利用模板导出Excel(C#)
前言 随着互联网的流行,web项目逐渐占据主流.我相信大部分人开发项目的过程中都写过上传以及导出Excel和Word的功能,本文仅讨论导出Excel.C#中有很多第三方组件支持导出Excel,比如:N ...
- 微信小程序---wx.request(OBJECT)
详情 :https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html#wxrequestobject 1: 首先要配置你的域名 ...
- SpringMVC 集成redis
一.下载导入jar 二.配置redis 1.创建redis.properties # Redis settings #redis.host=192.168.20.101 #redis.port= #r ...
- mybatis 的mapper配置文件sql语句中, 有时用到 大于, 小于等等
一, 用<![CDATA[ ]]>标识,例如: <if test="create_timeStart != null and create_timeStart != ' ...
- C++数组做参数
首先,看一下下面这段代码: void changearr(int a[],int n){ cout<<sizeof(a)<<endl; // 输出4}in ...
- JavaScript+HTML5 实现打地鼠小游戏
一.游戏简介 打地鼠这个游戏相信大家都不陌生,也是童年时候一款经典的游戏.本次游戏的编写是以html文件形式完成的,并且使用HBulider软件进行编写,使用谷歌浏览器展示效果,游戏将会采用JavaS ...
- DNS主从服务部署
(1)节点信息 console01 主DNS 192.168.80.3 192.168.10.3 console02 从DNS 192.168.80.4 192.168.10.4 (2)环境部署 # ...
- 独热编码OneHotEncoder简介
在分类和聚类运算中我们经常计算两个个体之间的距离,对于连续的数字(Numric)这一点不成问题,但是对于名词性(Norminal)的类别,计算距离很难.即使将类别与数字对应,例如{'A','B','C ...
- HDFS--笔记
HDFS的简介 分布式的文件系统,基于流数据模式访问和处理超大文件的分布式文件系统 Hadoop Distributed File System HDFS的优点 处理超大文件 流数据访问 运行廉价的商 ...
- 移动开发 meta元素
meta标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 web 服务. SEO优化: &l ...