搭建Eureka Server服务中心

  Spring Cloud中使用Eureka来做服务注册和发现,来统一管理微服务实例。

  1.使用IDEA创建一个空的Maven项目做父模块

    (也可以不用父项目,所有模块都用平行结构)

  搭建Eureka Server服务中心_第1张图片

  删除父模块src文件夹

  搭建Eureka Server服务中心_第2张图片

  可使用Spring Initializr来创建模块或者创建Maven项目手动添加依赖

  2.使用Spring Initializr创建Eureka Server

  选择SDK 1.8

  搭建Eureka Server服务中心_第3张图片

  Artifact填写对应模块的名称

  搭建Eureka Server服务中心_第4张图片

  依次选择Cloud Discovery->Eureka Server,Spring Boot默认选择的是2.0.4

  搭建Eureka Server服务中心_第5张图片

  可以参考Spring Cloud官方对应的Spring Cloud版本对应组件的版本,可以看到Finchley.SR1对应的是Spring Boot 2.0.4.RELEASE版

  https://projects.spring.io/spring-cloud/

  搭建Eureka Server服务中心_第6张图片

  创建好后会还原Maven项目的依赖,需要一段时间。

  2.1配置Eureka Server注解
  还原成功后会生成一个模块名称+Application的启动类,在类名上加上@EnableEurekaServer注解用来申明这是个Eureka服务器

  搭建Eureka Server服务中心_第7张图片

  2.2配置Eureka Server配置文件

  配置文件可参考号官方文档,找到对应的Spring Cloud版本,点击Reference。

   搭建Eureka Server服务中心_第8张图片

  http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#spring-cloud-eureka-server

  搜索找到Standalone Mode单机模式

  搭建Eureka Server服务中心_第9张图片

  搭建Eureka Server服务中心_第10张图片

  将项目中的application.properties后缀名改为yml,把application.yml的配置文件内容复制到项目中。

  port为服务的端口,可以自定义,我这里把端口改为了8881
  idea使用Debug启动成功后,在浏览器输入:http://localhost:8881/ 有一下内容则说明该服务启动成功。

   搭建Eureka Server服务中心_第11张图片

  

   3.使用Maven项目创建Eureka Server

  创建一个空的Maven项目,在父模块创建的会自动选中父模块

  搭建Eureka Server服务中心_第12张图片

  将项目中的pom文件parent节点全部删除,到Spring Cloud官网复制Maven项目的依赖相关内容到pom文件的project节点下

  搭建Eureka Server服务中心_第13张图片

  官网的依赖文件是Eureka客户的依赖,把dependencies节点下的依赖都删除

  添加Eureka Server的依赖:  


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

  再添加Spring Cloud版本属性


    UTF-8
    UTF-8
    1.8
    Finchley.SR1

  在resources文件夹中添加application.yml文件,并设置为资源根目录,配置文件内容和第一一样,改下端口为:8882

  搭建Eureka Server服务中心_第14张图片

  新建启动类(项目名+Application.java),写一个main方法启动Spring Boot程序,并加上Eureka Server的注解

  搭建Eureka Server服务中心_第15张图片

  配置Spring Boot启动配置项

  搭建Eureka Server服务中心_第16张图片

  选择对应的启动类

  搭建Eureka Server服务中心_第17张图片

  启动该模块,成功后再浏览器输入:http://localhost:8882/,使用Maven创建的Eureka Server项目也成功启动了

   搭建Eureka Server服务中心_第18张图片

  4.创建Eureka Server集群

   集群的作用是一个服务复制多份实例,当某一个实例挂掉后不至于影响整个系统,Eureka Server集群中的每个实例都是相互复制来保障所注册的服务一致。

实际生产环境中只要有一个Eureka Server通过不同的实例对应配置不同的端口来实现,为方便演示,我这里创建集群就采用刚刚创建的两个不同的Eureka Server来做。

  在官方文档中搜索:Peer Awareness
  http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#spring-cloud-eureka-server-peer-awareness
  找到对应的集群配置

  搭建Eureka Server服务中心_第19张图片搭建Eureka Server服务中心_第20张图片

 

   修改Host文件,添加 127.0.0.1 peer1 peer2,并将服务名设置一样

  服务1配置:

  

复制代码

server:
  port: 8881

eureka:
  instance:
    hostname: peer1
  client:
    registerWithEureka: true #把自身当做客户端注册到其他Eureka服务器
    fetchRegistry: true #在本地缓存所有实例注册信息
    serviceUrl:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
      ##指向另一个注册中心
      defaultZone: http://peer2:8882/eureka/

spring:
  #profiles: peer1  #没做环境配置,注释掉
  application:
    name: tmsapi-discovery-server

复制代码

  服务2配置:

复制代码

server:
  port: 8882

eureka:
  instance:
    hostname: peer2
  client:
    registerWithEureka: true #把自身当做客户端注册到其他Eureka服务器
    fetchRegistry: true #在本地缓存所有实例注册信息
    serviceUrl:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
      ##指向另一个注册中心
      defaultZone: http://peer1:8881/eureka/

spring:
  #profiles: peer2  #没做环境配置,注释掉
  application:
    name: tmsapi-discovery-server

复制代码

  启动两个服务,成功浏览器输入:http://peer1:8881/ 和http://peer2:8882/,同一个Application中就有两个实例了
  peer1:

  搭建Eureka Server服务中心_第21张图片

  peer2:

  搭建Eureka Server服务中心_第22张图片

  至此Eureka Server集群就搭建成功了。

 

  Eureka相关资料:

 http://www.cgpwyj.cn/
http://www.peacemind.com.cn/
http://www.tasknet.com.cn/
http://www.metroworld.com.cn/
http://www.cngodo.cn/
http://www.gzrdbp.cn/
http://www.dnapt.cn/
http://www.zgxxyp.cn/
http://www.sjjdvr.cn/
http://www.sujinkeji.cn/
http://www.zsjxbd.cn/
http://www.yesgas.cn/
http://www.quickpass.sh.cn/
http://www.jspcrm.cn/
http://www.yjdwpt.cn/
http://www.henanwulian.cn/
http://www.hhrshh.cn/
http://www.gpgold.cn/
http://www.jingzhuiyou.cn/
http://www.ownbar.cn/
http://www.shtarchao.net.cn/

你可能感兴趣的:(搭建Eureka Server服务中心)