Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式绘画和集群状态管理等操做提供了一种简单的开发方式。html
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不一样开源产品),好比:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目git
服务治理能够说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务示例的自动化注册与发现。服务治理主要分为两步:github
Spring Cloud Eureka,使用Netfix Eureka 来实现服务注册与发现,它即包含了服务端组件,也包含了客户端组件。web
Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。经过一些简单的注解,开发者就能够快速的在应用中配置一下经常使用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等spring
Eureka Server 咱们能够称之为服务注册中心,它同其余注册中心同样,支持高可用配置,常见的注册中心有:segmentfault
服务注册中心,即将当前服务状态注册到注册中心,便于其余服务发现以及调用。bash
Eureka Server 是 EurekaClient 的注册服务中心,管理全部注册服务,以及其示例信息和状态服务器
相关依赖:org.springframework.cloud:spring-cloud-starter-netflix-eureka-server
激活服务:@EnableEurekaServer
复制代码
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.app
抠脚翻译:
服务发现 是微服务体系结构中很是关键的一个环节。尝试经过手动去管理配置每一个客户端或着相关协议是很是困难的一件事。Eureka 是 Netflix服务的服务端与客户端。它能够配置和部署服务器使其具备高可用性,而且将每一个服务的注册状态同步到其余服务器中。
接下来咱们手动操做一把,经过Idea 快速建立一个 Eureka Server 服务注册中心。
服务名:spring-cloud-eureka
咱们在构建服务时,建立基于Spring Boot 的程序 ,详情参考传送门
<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
便可
启动类须要加上 @EnableEurekaServer
进行服务注册中心启动
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
复制代码
相关配置信息:
//服务端口
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
复制代码
服务启动后,打开地址:http://localhost:8761/
咱们能够看到,目前并无服务注册到咱们的注册中心中。
与服务端不一样,这里引入的依赖为: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>
复制代码
@EnableEurekaClient
该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
}
}
复制代码
server.port=8762
//服务名称
spring.application.name = jaycekon-hi
//服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
复制代码
服务启动后,打开地址:http://localhost:8761/
咱们能够看到,服务已经成功注册到Eureka Server 中。
源码地址:https://github.com/jaycekon/Spring-Cloud
参考资料:
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