SpringCloud2.0 Eureka Server 服务中心 基础教程(二)

一、建立【服务中心】,即 Eureka Server

1.一、新建 Spring Boot 工程,工程名称: springcloud-eureka-server

1.二、工程 pom.xml 文件添加以下依赖:

<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.三、在工程启动类中,添加注解 @EnableEurekaServer

package com.miniooc.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * EurekaServerApplication
 *
 * @author 宋陆
 * @version 1.0.0
 */
@EnableEurekaServer // 启用 eureka server 相关默认配置
@SpringBootApplication // 等价于 @Configuration、@EnableAutoConfiguration、@ComponentScan
public class EurekaServerApplication {

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

}

1.四、新建工程配置文件 application.yml ,配置内容:

server:
  port: 9527

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      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.1 # 默认是0.85

1.五、启动 服务中心 工程,打开浏览器,访问 服务中心 前台页面:http://localhost:9527

红框处 No instances available :没有可用的实例。目前还任何服务注册到服务中心。java

至此,一个简单的单点服务中心搭建完成。web

为了保证服务的高可用,咱们须要把单点应用改为集群应用。spring

接下来,咱们对服务中心工程作些配置修改,来完成集群部署。浏览器

二、搭建【服务中心】集群

2.一、建立工程配置文件 application-server1.yml ,配置内容:

server:
  port: 9527

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/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,本地单机测试设为0.49

参照2.1,建立工程配置文件 application-server2.yml,并修改 server.port 为 9528bash

参照2.1,建立工程配置文件 application-server3.yml,并修改 server.port 为 9529app

2.二、在 IntelliJ IDEA 集成开发环境中,添加 spring boot 启动配置 EurekaServerS1,修改 Active profiles 为 server1

参照2.2, spring boot 启动配置 EurekaServerS2,修改 Active profiles 为 server2spring-boot

参照2.2, spring boot 启动配置 EurekaServerS3,修改 Active profiles 为 server3测试

2.三、按照启动配置 EurekaServerS一、EurekaServerS二、EurekaServerS3 依次启动服务中心

2.四、打开浏览器,访问 服务中心 前台页面:http://localhost:9527,http://localhost:9528,http://localhost:9529

若是三个服务都启动成功的话,三个页面都应该看到如上图所示。fetch

至此,一个简单的服务中心集群搭建完成。.net

三、本章小结

本章主要讲解了 Eureka Server 服务中心的搭建,并讲解若是扩展为集群应用。但服务中心的使用以及集群的优势,本章并未讲解,由于这须要 Eureka Client 服务提供者和 Eureka Discovery Client 服务发现者配合使用,后续的章节咱们会讲解后二者的使用。

下一章,咱们将讲解,如何建立 Eureka Client 服务提供者,并把咱们的服务注册到服务中心,以及如何搭建服务提供者集群。

相关文章
相关标签/搜索