spring cloud 2.x版本 Eureka Server服务注册中心教程

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

1.创建服务注册中心

1.1 新建Spring boot工程:eureka-server

1.2 pom.xml所需依赖jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

1.3 EurekaServerApplication添加注解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

@EnableEurekaServer:启用eureka server相关配置

1.4 添加配置文件内容:application.yml

spring:
  application:
    name: eureka-server

server:
  port: 8701
#无参数启动
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false #默认为true,设置为false,仅作为服务中心,不作为服务客户端
    fetch-registry: false #默认为true,设置为false, 不从服务中心检索注册的服务
  server:
    eviction-interval-timer-in-ms: 5000 #清理间隔(单位毫秒, 默认是60*1000)
    enable-self-preservation: true #默认为true,设置为false,关闭自我保护
    #eureka server: 在运行期间会去统计心跳失败比例在15分钟之内是否低于85%
    renewal-percent-threshold: 0.49 #默认0.85

单机模式下:
register-with-eureka和fetch-registry应为false,否则启动会报错:Cannot execute request on any known server。原因,在默认设置下,eureka服务注册中心会将自己作为客户端来尝试注册自己。

1.5 启动服务

打开浏览器输入:http://localhost:8701, 显示如下:

红框内容代表还没有实例注册

结语

至此,一个简单的单机服务注册中心就搭建完成了。

搭建服务注册中心集群

为了保证服务的高可用,我们需要把单机应用改成集群应用,接下来,我们创建一个简单的eureka server集群.

1.1 修改本地host

  • 127.0.0.1 eureka1.server.com
  • 127.0.0.1 eureka2.server.com
  • 127.0.0.1 eureka3.server.com

1.2 增加application.yml配置文件

增加application-server1.yml和application-server2.yml文件,同时修改原来的application.yml文件,文件内容如下:

  • application.yml
spring:
  application:
    name: eureka-server

server:
  port: 8701
#无参数启动
eureka:
  instance:
    hostname: eureka1.server.com
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false
    fetch-registry: true
  server:
    eviction-interval-timer-in-ms: 5000
    enable-self-preservation: true
    renewal-percent-threshold: 0.49 
  • application-server1.yml
spring:
  application:
    name: eureka-server

server:
  port: 8702
#无参数启动
eureka:
  instance:
    hostname: eureka2.server.com
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false 
    fetch-registry: true 
  server:
    eviction-interval-timer-in-ms: 5000 
    enable-self-preservation: true 
    renewal-percent-threshold: 0.49
  • application-server2.yml
spring:
  application:
    name: eureka-server

server:
  port: 8703
#无参数启动
eureka:
  instance:
    hostname: eureka3.server.com
  client:
    service-url:
         defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false 
    fetch-registry: true
  server:
    eviction-interval-timer-in-ms: 5000
    enable-self-preservation: true
    renewal-percent-threshold: 0.49 #默认0.85

1.3 在idea中添加启动服务

设置启动服务,按照截图中1234顺序依次添加


分别创建eureka-server2和eureka-server3.(注:eureka-server1用原来的启动就可以)

1.4 按照顺序分别启动3个eureka server服务

启动服务后分别访问 http://eureka1.server.com:8701http://eureka1.server.com:8702http://eureka1.server.com:8703 如图显示:

三个页面如上图显示就代表服务全部启动成功。至此,一个简单的服务中心集群就搭建完成。

总结

可以将application-server1.yml和application-server2.yml的配置信息都放到原application.yml配置中,通过‘---’ 三横杠模加spring.profiles模式来启动,同时增加启动参数: --spring.profiles.active=config-server1。本文采用过个application.yml的方式,方便以后的维护。

本文只是简单的搭建了服务注册中心的单机和集群应用,对eureka做为服务注册中心有一个简单对认识,后续会更新eureka的其他内容。

代码仓库

gitHub地址


《Srping Cloud 2.X小白教程》目录

  • 写作不易,转载请注明出处,喜欢的小伙伴可以关注公众号查看更多喜欢的文章。
  • 联系方式:4272231@163.com
原文地址:https://www.cnblogs.com/fengfujie/p/11769425.html