一、Spring Cloud 简介
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操做提供了一种简单的开发方式。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等项目。web
二、微服务架构
“微服务架构”在这几年很是的火热,以致于关于微服务架构相关的产品社区也变得愈来愈活跃(好比:netflix、dubbo),Spring Cloud也因Spring社区的强大知名度和影响力也被广大架构师与开发者备受关注。spring
那么什么是“微服务架构”呢?简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分红多个不一样的服务,每一个服务都能独立部署、独立维护、独立扩展,服务与服务间经过诸如RESTful API的方式互相调用。架构
对于“微服务架构”,你们在互联网能够搜索到不少相关的介绍和研究文章来进行学习和了解。也能够阅读始祖Martin Fowler的《Microservices》,本文不作更多的介绍和描述。app
三、服务注册与发现
在简单介绍了Spring Cloud和微服务架构以后,下面回归本文的主旨内容,如何使用Spring Cloud搭建服务注册与发现模块。负载均衡
这里咱们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。经过一些简单的注解,开发者就能够快速的在应用中配置一下经常使用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路有(Zuul),客户端负载均衡(Ribbon)等。分布式
因此,咱们这里的核心内容就是服务发现模块:Eureka。下面咱们动手来作一些尝试。spring-boot
建立“服务注册中心”微服务
建立一个基础的Spring Boot工程,并在pom.xml中引入须要的依赖内容:工具
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
经过@EnableEurekaServer注解启动一个服务注册中心提供给其余应用进行对话。这一步很是的简单,只须要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,好比下面的例子:
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } |
在默认设置下,该服务注册中心也会将本身做为客户端来尝试注册它本身,因此咱们须要禁用它的客户端注册行为,只须要在application.properties中问增长以下配置:
server.port=1111 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ |
为了与后续要进行注册的服务区分,这里将服务注册中心的端口经过server.port属性设置为1111。
启动工程后,访问:http://localhost:1111/
能够看到下面的页面,其中尚未发现任何服务
建立“服务提供方”
下面咱们建立提供服务的客户端,并向服务注册中心注册本身。
假设咱们有一个提供计算功能的微服务模块,咱们实现一个RESTful API,经过传入两个参数a和b,最后返回a + b的结果。
首先,建立一个基本的Spring Boot应用,在pom.xml中,加入以下配置:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
其次,实现/add请求处理接口,经过DiscoveryClient对象,在日志中打印出服务实例的相关内容。
@RestController public class ComputeController { private final Logger logger = Logger.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping(value = "/add" ,method = RequestMethod.GET) public Integer add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); return r; } } |
最后在主类中经过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。
@EnableDiscoveryClient @SpringBootApplication public class ComputeServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); } } |
咱们在完成了服务内容的实现以后,再继续对application.properties作一些配置工做,具体以下:
spring.application.name=compute-service server.port=2222 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ |
经过spring.application.name属性,咱们能够指定微服务的名称后续在调用的时候只须要使用该名称就能够进行服务的访问。
eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。
为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不一样的端口。
启动该工程后,再次访问:http://localhost:1111/
能够看到,咱们定义的服务被注册了。