基于Eureka的服务治理

栏目: Java · 发布时间: 4年前

内容简介:一、服务的注册与发现关系调用说明:
编辑推荐:
本文来自于博客园,本文主要介绍了如何搭建服务注册中心,使用Eureka进行服务治理,希望对您的学习有所帮助。

一、服务的注册与发现

基于Eureka的服务治理

关系调用说明:

服务生产者启动时,向服务注册中心注册自己提供的服务

服务消费者启动时,在服务注册中心订阅自己所需要的服务

注册中心返回服务提供者的地址信息个消费者

消费者从提供者中调用服务

二、Eureka简介

Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。

Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。

客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。

三、使用Eureka进行服务治理

1. 搭建服务注册中心

单独创建一个Springboot项目,并添加如下的依赖

<dependencies>
 <!--添加Eureka服务器端依赖-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-eureka-server</artifactId>
 </dependency>
 </dependencies>
 <dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Brixton.SR5</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
 </dependencyManagement> 

在Springboot项目中的main入口,添加@EnableEurekaServer注解,来开启服务注册中心

@EnableEurekaServer
 @SpringBootApplication
 public class EurekaServerApplication {

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

在默认情况下,服务注册中心也会把自己当做是一个服务,将自己注册进服务注册中心,所以我们可以通过配置来禁用他的客户端注册行为,在application.properties中添加如下配置:

server.port=3333
 eureka.instance.hostname=localhost
 #不要向注册中心注册自己
 eureka.client.register-with-eureka=false
 #禁止检索服务
 eureka.client.fetch-registry=false
 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eurek

启动应用,并访问http://localhost:3333/即可看到Eureka信息面板,如下:

基于Eureka的服务治理

从上图看到,在"Instances currently registered with Eureka"信息中,没有一个实例,说明目前还没有服务注册。

2. 注册服务

基于文章[微服务系列] 微服务构建框架--Spring Boot中构建的第一个Springboot项目(在附件中的springbootdemo.zip实例),来进行改造。首先在pom文件中添加Eureka客户端相关的依赖:

<dependencies>
 <!---->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-eureka</artifactId>
 </dependency>
 </dependencies>
 
<dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Brixton.RS5</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
 </dependencyManagement> 

在之前的TestRestful方法中添加如下代码,将org.springframework.cloud.client.discovery.DiscoveryClient;对象注入,并且在日志中打印出与服务相关的一些信息。

@RestController
 public class TestRestful { 
private final Logger logger=Logger.getLogger(getClass()); 
 @Autowired
 private DiscoveryClient client;
 
 @RequestMapping(value = "/hello",method = RequestMethod.GET)
 public String home(){
 ServiceInstance instance=client.getLocalServiceInstance();
 logger.info("serviceId"+instance.getServiceId()+"host:post=
 
"+instance.getHost()+":"+instance.getPort());
 return "hello spring";
 }
 } 

在主类上添加@EnableEurekaClient注解以实现Eureka中的DiscoveryClient实现。

@EnableEurekaClient
 @SpringBootApplication
 public class SpringbootdemoApplication {
 
public static void main(String[] args) {
 SpringApplication.run(SpringbootdemoApplication.class, args);
 }
 } 

最后在配置文件application.yml中配置与服务相关的一些基本信息,如服务名、注册中心地址(即上一节中设置的注册中心地址http://localhost:3333/eureka)

# 设置服务名
 spring:
 application:
 name: hello-service
 # 设置注册中心地址
 eureka:
 client:
 service-url:
 defaultZone: http://localhost:3333/eureka 

3. 测试

启动注册中心服务

启动springbootdemo项目,可以看到如下的信息,说明此服务已经注册在了注册中心

基于Eureka的服务治理

同时访问http://localhost:3333/可以在Instances currently registered with Eureka中看到已经有一个服务注册了进来,并且名称为HELLO-SERVICE的服务

基于Eureka的服务治理

访问http://localhost:8088/hello可以在控制台中看到如下日志信息:

基于Eureka的服务治理

四、项目代码截图

基于Eureka的服务治理

代码地址如下:http://www.demodashi.com/demo/11927.html


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深入浅出SQL(中文版)

深入浅出SQL(中文版)

贝里 编 / O‘Reilly Taiwan公司 / 东南大学 / 2009-6 / 98.00元

你将从《深入浅出SQL(中文版)》学到什么?在如今的世界,数据就是力量,但是成功的真正秘诀却是管理你的数据的力量。《深入浅出SQL(中文版)》带你进入SQL语言的心脏地带,从使用INSERT和SELECT这些基本的查询语法到使用子查询(subquery)、连接(join)和事务(transaction)这样的核心技术来操作数据库。到读完《深入浅出SQL(中文版)》之时,你将不仅能够理解高效数据库设......一起来看看 《深入浅出SQL(中文版)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具