Spring Cloud简介
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理、
服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、
Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、
pring Cloud Zookeeper、Spring Cloud CLI等项目。
微服务架构
“微服务架构”在这几年非常的火热,以至于关于微服务架构相关的产品社区也变得越来越活跃(比如:netflix、dubbo),
Spring Cloud也因Spring社区的强大知名度和影响力也被广大架构师与开发者备受关注。
服务注册与发现
这里用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,
 它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用
 模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),
 客户端负载均衡(Ribbon)等。
所以,我们这里的核心内容就是服务发现模块:Eureka。下面我们动手来做一些尝试。
创建“服务注册中心”
创建一个基础的Spring Boot工程,并在pom.xml中引入需要的依赖内容:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 | 	<parent> 		<groupId>org.springframework.boot</groupId> 		<artifactId>spring-boot-starter-parent</artifactId> 		<version>1.4.0.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应用中添加这个注解就能开启此功能,比如下面的例子:
| 1 2 3 4 5 6 7 8
 | @SpringBootApplication @EnableEurekaServer public class SpringCloudApplication { 	public static void main(String[] args) { 		SpringApplication.run(SpringCloudApplication.class, args); 	} }
 | 
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,
所以我们需要禁用它的客户端注册行为,只需要在application.properties中问增加如下配置:
| 1 2 3 4 5
 | server.port=11111 eureka.client.register-with-eureka=false   eureka.client.fetch-registry=false   eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
 | 
启动项目,在浏览器输入网址:http://localhost:11111
可以看到注册中心已经启动了
创建服务提供方
现在 提供计算功能的微服务模块,我们实现一个RESTful API,通过传入两个参数a和b,最后返回a + b的结果。
首先,创建一个基本的Spring Boot应用,在pom.xml中,加入如下配置:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 | <parent> 		<groupId>org.springframework.boot</groupId> 		<artifactId>spring-boot-starter-parent</artifactId> 		<version>1.4.0.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对象,在日志中打印出服务实例的相关内容。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 | @RestController public class ComputeController {     private final Logger logger = LoggerFactory.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中对服务信息的输出。
| 1 2 3 4 5 6 7 8
 | @SpringBootApplication @EnableDiscoveryClient public class SpringCloudProvideApplication { 	public static void main(String[] args) { 		SpringApplication.run(SpringCloudProvideApplication.class, args); 	} }
 | 
application.properties的配置
| 1 2 3 4 5
 | spring.application.name=compute-service server.port=2222 eureka.client.serviceUrl.defaultZone=http://localhost:11111/eureka/
 | 
通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。
为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。
启动该工程后,再次访问:http://localhost:11111/