微服务的注册与发现:如何集成Eureka Server?

1496 篇文章 10 订阅
1494 篇文章 14 订阅

如何集成Eureka Server

本节将创建一个基于Eureka Server实现的注册服务器。由于Spring Cloud项目本身也是基于Spring Boot的,因此,我们可以基于现有的Spring Boot来进行更改。

以前面创建的hello-world应用作为基础,改造成为新的应用micro-weather-eureka- server。

所需环境

为了演示本例,需要采用如下开发环境。

JDK 8。

●Gradle 4.0。

●Spring Boot 2.0.0.M3。

●Spring Cloud Starter Netflix Eureka Server Finchley.M2。

更改build.gradle配置

与hello-world相比,
micro-weather-eureka-server 应用的build.gradle 配置的变化,主要体现在以下几点。

●springBootVersion 变量指定了Spring Boot的版本,这里设定为2.0.0.M3,而非之前的2.0.0.M4。

因为最新的Spring Cloud并未对Spring Boot 2.0.0.M4做好兼容。

添加了springCloudVersion变量,用于指定Spring Cloud的版本。目前,本书中主要用了Finchley.M2版本。

在dependencyManagement (依赖管理)中,我们导入了Spring Cloud的依赖的管理。

最为重要的是,在依赖中,我们添加了Spring Cloud Starter Netlix Eureka Server依赖。


micro-weather-eureka-server应用的build.gradle详细配置如下。

// buildscript 代码块中脚本优先执行
buildscript {
// ext用于定义动态属性
ext {
springBootversion = '2.0.0.M3'
//使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
/ /mavenCentral()
maven { url "https://repo. spring. io/snapshot" }
maven { url "https://repo. spring. io/milestone" }
maven { url "http:/ /maven. aliyun. com/ nexus/ content/groups/
public/" }
//依赖关系
dependencies {
// classpath声明 了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath ("org. springframework . boot :spring-boot-gradle-plugin:
${ springBootVersion}")
}
//使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: ' org. springf ramework . boot'
apply plugin: ' io. spring .dependency-management'
//指定了生成的编译文件的版本,默认是打成了jar包
group = ' com. waylau. spring.cloud'
version = '1.0.0'
//指定编译. java文件的JDK版本
sourceCompatibility =1.8
//使用了Maven的中央仓库及Spr ing自己的仓库(也可以指定其他仓库)
repositories
/ /mavenCentral ()
maven { url "https:// repo.spring. io/ snapshot" }
maven { url "https://repo. spring. io/milestone" }
maven { url "http: //maven. aliyun. com/ nexus/content/groups/public/"
ext
springCloudVersion = 'Finchley .M2 '
dependencies {
//添加Spring Cloud Starter Netflix Eureka Server依赖
compile ('org. springf ramework. cloud:spring-cloud-starter-netflix-
eureka-server')
//该依赖用于测试阶段
tes tCompile ( 'org. spr ingf ramework . boot: spring-boot-starter-test')
dependencyManagement{
imports {
mavenBom "org. spr ingf ramework.cloud:spring-cloud-dependencies:
$ {springCloudVersion}"

其中,Spring Cloud Starter Netlix Eureka Server自身又依赖了如下的项目。

<dependencies>
<dependency>
<groupId>org.spr ingframework.cloud</groupId>
<arti factId>spring-cloud-starter</arti factId>
</ dependency>
<dependency>
<groupId>org. spr ingf ramework.cloud</groupId>
<arti factId>spring-cloud-netflix-eureka-server</artifactId>
</ dependency>
<dependency>
<groupId>org. spr ingframework. cloud</groupId>
<arti factId>spring-cloud-starter-netflix-archaius</arti factId>
</ dependency>
<dependency>
<groupId>org . springfr amework.cloud</groupId>
<arti factId> spring-cloud-starter -netflix- ribbon</arti factId>
</ dependency>
<dependency>
<groupId>com. netflix. ribbon</groupId>
<artifactId> ribbon-eureka</artifactId>
</dependency>
</ dependencies>
所有配置都能够在Spring Cloud Starter Netlix Eureka Server项目的porm文件中查看到。

启用Eureka Server

为了启用Eureka Server, 在应用的根目录的Application类上增加@EnableEurekaServer注解即可。

import org. springframework.boot .SpringApplication;
import org. springf ramework . boot . autoconfigure . Spr ingBootApplication;
import org. springframework. cloud. netflix. eureka.server .EnableEurekaServer;
/**
主应用程序.
* @since 1.0.0 2017年10月31日
@author <a href="https:/ /waylau. com">Way Lau</a>
*/
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication. run (Application.class, args) ;
}
}

该注解就是为了激活Eureka Server相关的自动配置类
org.springframework.cloud.ntix.eureka.server.EurekaServerAutoConfiguration。

修改项目配置

修改application.properties,增加如下配置。

server.port: 8761
eureka. instance . hostname: localhost
eureka. client. registerWithEureka: false
eureka.client. fetchRegistry: false
eureka. client. serviceUrl . defaultZone: http:/ /${eureka. instance. hostname} :
${server . port}/eureka/

其中:

server.port:指明了应用启动的端口号;

eureka.instance.hostname:应用的主机名称;


eureka.client.registerWithEureka: 值为false 意味着自身仅作为服务器,不作为客户端;


eureka.client.fetchRegstry:值为false 意味着无须注册自身;


eureka.client.serviceUrl.defaultZone: 指明了应用的URL。

清空资源目录

在src/main/resources目录下,除了application.properties 文件外,其他没有用到的目录或文件都删除,特别是templates目录,因为这个目录会覆盖Eureka Server自带的管理界面。

启动

启动应用,访问htt:/tcalhost:8761,可以看到如图8-1所示的Eureka Server自带的UI管理界面。

自此,Eureka Server注册服务器搭建完毕。

本篇文章给大家讲解的内容是如何集成Eureka Server

  1. 下篇文章给大家讲解如何集成Eureka Client;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值