简介:Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集,为开发人员提供快速创建分布式系统中的一些常见的模式。php
例如:配置管理(configuration management),服务发现(service discovery),断路器(circuit breakers),智能路由( intelligent routing),html
微代理(micro-proxy),控制总线(control bus),一次性令牌( one-time tokens),全局锁(global locks),领导选举(leadership election),java
分布式会话(distributed sessions),集群状态(cluster state)。git
Spring Cloud 包含了多个子项目:github
例如:Spring Cloud Config、Spring Cloud Netflix等spring
Spring Cloud 项目主页:http://projects.spring.io/spring-cloud/apache
在微服务架构中,服务发现(Service Discovery)是关键原则之一。手动配置每一个客户端或某种形式的约定是很难作的,而且很脆弱。Spring Cloud提供了多种服务发现的实现方式,session
例如:Eureka、Consul、Zookeeper。 Spring Cloud支持得最好的是Eureka,其次是Consul,最次是Zookeeper。架构
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-discovery-eureka</artifactId> <packaging>jar</packaging> <parent> <groupId>com.itmuch.cloud</groupId> <artifactId>spring-cloud-microservice-study</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
/** * 使用Eureka作服务发现。 * @author eacdy */ @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } } |
server: port: 8761 # 指定该Eureka实例的端口 eureka: instance: hostname: discovery # 指定该Eureka实例的主机名 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ |
# 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode
# 参考文档:http://my.oschina.net/buwei/blog/618756
启动工程后,访问:http://discovery:8761/ ,以下图。咱们会发现此时尚未服务注册到Eureka上面。app
http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-discovery-eureka
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-discovery-eureka