微服务架构开发实战:如何集成 Eureka Client?

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

如何集成 Eureka Client

本节将创建一个
micro-weather-cureka-client作为客户端,并演示如何将自身向注册服务器进行注册,这样以便其他服务都能够通过名称来访问服务。该客户端基于Eureka Client来实现。


micro-weather-eureka-client可以基于micro-weather-eureka-server应用来做更改。

所需环境

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

.JDK8。

.Gradle 4.0。

Spring Boot 2.0.0.M3。

.Spring Cloud Starter Netflix Eureka Client Finchley.M2。

更改build.gradle配置


micro-weather-eureka-server相比,micro-weather-eureka-client应用的build.gradle配置的变化,主要是在依赖上面,将Eureka Server的依赖改为Eureka Client即可。

dependencies {
//添加Spring Cloud Starter Netflix Eureka Client依赖
compile ('org.springframework.cloud:spring-cloud-starter-netflix-
eureka-client')
//该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}

一个最简单的Eureka Client

将@EnableEurekaServer注解改为@EnableDiscoveryClient。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscovery
Client;
/**
*主应用程序.
*
*@since 1-o.0 2017年11月01日
*@author <a href="https://waylau.com">Way Lau</a>
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Application
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}


org.springframework.cloud.client.discovery.EnableDiscoveryClient注解,就是一个自动发现客户端的实现。

修改项目配置

修改application.properties,修改为如下配置。

spring.application.name: micro-weather-eureka-client

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

其中
:.spring.application.name:指定了应用的名称;


.eureka.client.serviceUrl.defaultZonet:指明了Eureka Server的位置。

运行和测试

首先运行Eureka Server实例
micro-weather-eureka-server,它启动在8761端口。

而后分别在8081和8082上启动了Eureka Client实例
micro-weather-eureka-client。

java -jar micro-weather-eureka-client-1.0.0.jar--server.port=8081
java -jar micro-weather-eureka-client-1.0.0.jar --server.port=8082

这样,就可以在Eureka Server 上看到这两个实例的信息。访问http:/localhost:8761,可以看到如图8-2所示的Eureka Server自带的UI管理界面。

从管理界面“Instances currently registered with Eureka”中,能看到每个Eureka Client的状态,相同的应用(指具有相同的spring.application.name )下,能够看到每个应用的实例。

如果Eureka Client离线了,Eureka Server也能及时感知到。

不同的应用之间,就能够通过应用的名称来互相发现。

其中,从界面上也可以看出,Eureka Server运行的IP为192.168.1.101。

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

  1. 下篇文章给大家讲解实现服务的注册与发现;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值