Spring Cloud 系列(一)Eureka 服务注册与发现

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

内容简介:Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式绘画和集群状态管理等操作提供了一种简单的开发方式。Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务示例的自动化注册与发现。服务治理主要分为两步:

Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式绘画和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如: Spring Cloud ConfigSpring Cloud NetflixSpring Cloud CloudFoundrySpring Cloud AWSSpring Cloud SecuritySpring Cloud CommonsSpring Cloud ZookeeperSpring Cloud CLI 等项目

服务治理

服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务示例的自动化注册与发现。服务治理主要分为两步:

  • 服务注册
  • 服务发现

Spring Cloud Eureka

Spring Cloud Eureka,使用Netfix Eureka 来实现服务注册与发现,它即包含了服务端组件,也包含了客户端组件。

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等

Eureka 服务端

Eureka Server我们可以称之为服务注册中心,它同其他注册中心一样,支持高可用配置,常见的注册中心有:

  • Apache Zookeeper
  • Netflix Eureka
  • Consul

Eureka Server是 EurekaClient 的注册服务中心,管理所有注册服务,以及其示例信息和状态

相关依赖:org.springframework.cloud:spring-cloud-starter-netflix-eureka-server
激活服务:@EnableEurekaServer
复制代码

Eureka 客户端

Eureka Client主要处理服务的注册与发现。在Spring Cloud 官方文档中这样描述:

Service Discovery is one of the key tenets of a microservice based architecture. Trying to hand configure each client or some form of convention can be very difficult to do and can be very brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.

抠脚翻译:

服务发现 是微服务体系结构中非常关键的一个环节。尝试通过手动去管理配置每个客户端或着相关协议是非常困难的一件事。Eureka 是 Netflix服务的服务端与客户端。它可以配置和部署服务器使其具有高可用性,并且将每个服务的注册状态同步到其他服务器中。

Spring Eureka Server

接下来我们手动操作一把,通过Idea 快速创建一个 Eureka Server 服务注册中心。

服务名: spring-cloud-eureka

我们在构建服务时,创建基于Spring Boot 的程序 ,详情参考传送门

1、Maven 依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
...

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
...
复制代码

在项目依赖方面, Spring Cloud Eureka 服务端非常简单,主要是基于 Spring Boot ,其次引入 spring-cloud-starter-netflix-eureka-server 即可

2、SpringCloudEurekaApplication

启动类需要加上 @EnableEurekaServer 进行服务注册中心启动

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaApplication.class, args);
    }
}
复制代码

3、application.properties

相关配置信息:

//服务端口
server.port=8761
//eureka 服务地址
eureka.instance.hostname=localhost
//是否将当前服务注册到 Eureka 注册中心
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
//服务地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
//服务名称
spring.application.name=eureka-server
复制代码

4、启动服务

服务启动后,打开地址: http://localhost:8761/

Spring Cloud 系列(一)Eureka 服务注册与发现

我们可以看到,目前并没有服务注册到我们的注册中心中。

Spring Eureka Client

1、Maven 依赖

与服务端不同,这里引入的依赖为: spring-cloud-starter-netflix-eureka-client

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
复制代码

2、SpringCloudEurekaClientApplication

@EnableEurekaClient 该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudEurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
    }
}
复制代码

3、application.properties

server.port=8762
//服务名称
spring.application.name = jaycekon-hi

//服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

复制代码

4、启动服务

服务启动后,打开地址: http://localhost:8761/

Spring Cloud 系列(一)Eureka 服务注册与发现

我们可以看到,服务已经成功注册到Eureka Server 中。

参考

https://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#_service_discovery_eureka_clients
http://blog.didispace.com/spring-cloud-starter-dalston-1/
https://segmentfault.com/l/1500000011386051/play

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

查看所有标签

猜你喜欢:

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

Django 1.0 Template Development

Django 1.0 Template Development

Scott Newman / Packt / 2008 / 24.99

Django is a high-level Python web application framework designed to support the rapid development of dynamic websites, web applications, and web services. Getting the most out of its template system a......一起来看看 《Django 1.0 Template Development》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具