2019独角兽企业重金招聘Python工程师标准>>>
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,它基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能。
服务治理可以说是微服务架构中最核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现。下面开始搭建 Eureka 的三个核心要素:
1. 服务注册中心:
a) 用 eclipse 新建一个 gradle 项目 EurekaServer.
b) 配置 build.gradle 文件,配置内容如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
repositories {
jcenter()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server:1.2.7.RELEASE'
}
配置结束后, 在 eclipse 中刷新一次 gradle,让所有jar都引用到位。
c) 调用@enableEurekaServer来实现一个 Eureka 注册中心。
java 代码如下:
/**
*
*/
package com.simonton.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author simonton
*
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaDemoServer {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoServer.class, args);
}
}
application.yml配置文件如下:
server:
port: 9999
spring:
application:
name: eureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://simonton:9999/eureka
instance:
hostname: simonton
yml文件中,由于 demo 只启用了一个注册中心,没有采用向其它注册中心注册再来来实现高可行。所以把 register-with-eureka 和 fetch-register 设置为false.
通过以上三步,一个简单的注册中心已经实现,只需要 run 起来行了。EurekaDemoServer 起来后,可以通过http://localhost:9999来查看注册中心的服务注册信息。