Spring Cloud:Eureka服务注册

第一次写关于spring cloud相关的技术类文章,希望大家多多支持。

首先我们创建一个Gradle项目,引入java和spring-boot框架。

依赖:
1: spring-cloud-starter-eureka-server用于Eureka的服务注册。
2: spring-cloud-starter-config用于配置管理(比如对bootstrap.yml和application.yml等统一管理)。
3: spring-boot-actuator是spring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。

//build.gradle 为了学习可以仅添加spring-cloud-starter-eureka-server依赖
group 'org.sauceggplant'
version '0.0.1-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    //maven 仓库可以自己搭建nexus服务器用作maven仓库,也可以使用mavenCentral()中央仓库
    maven{url 'http://192.168.56.103:8081/repository/zhaozx/'}
}

dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-config:1.3.1.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-eureka-server:1.3.1.RELEASE'
    compile 'org.springframework.boot:spring-boot-actuator:1.5.3.RELEASE'
    //spring-boot-starter-test是spring-boot用于测试的,可以去掉
    testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.3.RELEASE'
}

buildscript {
    repositories {
        maven{url 'http://192.168.56.103:8081/repository/zhaozx/'}
    }
    dependencies {
        //spring-boot的gradle插件,如果是maven构建的,可以使用maven插件
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE'
    }
}

jar {
    //用于打包jar文件的名称和版本
    baseName = 'EurekaServer'
    version =  '0.0.1-SNAPSHOT'
}

Eureka服务注册只需要一个java类,spring-boot的启动类

//EurekaServerApplication.java
package org.sauceggplant.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

@SpringBootApplication注解用于声明这是一个Spring Boot项目。
@EnableEurekaServer注解用于声明启用Eureka服务注册,其他项目的服务可以注册到本应用服务器上。

resource中可以配置bootstrap.yml和application.yml。如习惯配置bootstrap.properties和application.properties也可,区别在于yml可以支持docker运行。

bootstrap多用于全局配置,application多用于应用本身的配置。项目配置特别多可以采用 spring.application.profiles.active对配置分开管理。内部原理与传统的spring的xml配置区别不大。

格式上:

#***.properties
server.port=8761
server.host=127.0.0.1

#***.yml
server:
    port: 8761
    host: 127.0.0.1

笔者觉得采用yml缩进更简洁,注意对齐和":"后边的空格.

#application.yml
server:
  port: 8761  //服务端口
spring:
  application:
    name: EurekaServer  //服务名称
#  cloud:
#    config:
#      server:
#        git:
#         url: http://192.168.56.101/zhaozx/config-repo/eureka-server  #yml或者properties配置在git资源库中统一配置管理
#  profiles:
#    active: dev  #相当于引入了application-dev.yml的内容,用于多个配置文件切换,比如开发模式有一套自己的配置。
eureka:
  client:
    registerWithEureka: false  #声明不在Eureka服务注册自己
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #${}方式为引用变量,这里指代Eureka服务注册地址
  instance:
    hostname: 127.0.0.1

对于bootstrap.yml

#bootstrap.yml
eureka:
  server:
    enable-self-preservation: false       # 关闭自我保护模式(缺省为打开)
    eviction-interval-timer-in-ms: 10000  # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)

项目启动(直接运行EurekaServerApplication类,或者打包后通过):

java -jar EurekaServer-0.0.1-SNAPSHOT.jar &

运行
EurekaServer启动

Eureka界面(http://127.0.0.1:8761,也可以输入本机主机名或者IP后加端口8761。8761是Eureka默认端口,可以通过配置文件修改):
EurekaServer界面

评论

暂无评论