spring-cloud入门环境搭建

1.什么是spring-cloud

spring-cloud是spring提供的微服务整合开发框架。Spring Cloud 为开发者提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性 Token、全局锁、决策竞选、分布式会话和集群状态)操作的开发工具。使用 Spring Cloud 开发者可以快速实现上述这些模式。

2.为什么使用spring-cloud

  1. 经历过netflix业务考验,国外大规模使用
  2. 入门门槛低,国内大批量使用spring
  3. 快速搭建

3.spring-cloud快熟搭建入门

1. eureka 服务注册组件

image
下载 https://github.com/mykite/eureka-server.git
编译后直接运行即可,或 mvn clean install 后直接运行jar包后访问
部署后:
_20160809115004

2. configServer

对配置的集中管理,使用svn or git
https://github.com/mykite/configserver.git
编译后直接运行即可,或 mvn clean install 后直接运行jar包后访问
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
使用方式
在configserver中配置的
spring:
cloud:
config:
server:
git:
uri: https://github.com/mykite/config-repostory
提交到test分支文件hell-server.yml
文件内容:
test.name: kite
访问:http://localhost:8888/hello-server/profiles/test
会访问当前配置github上的test分支下的hello-server.yml(or properties文件)
对应应用中的配置
spring:
cloud:
config:
uri: http://localhost:8888
label: test
可以实现注入

3. ribbon

ribbon用以实现负载均衡;实现软负载均衡,核心有三点:

  1. 服务发现,发现依赖服务的列表
  2. 服务选择规则,在多个服务中如何选择一个有效服务
  3. 服务监听,检测失效的服务,高效剔除失效服务

服务选择规则,其中包括:

  • 简单轮询负载均衡
  • 加权响应时间负载均衡
  • 区域感知轮询负载均衡
  • 随机负载均衡
    _20160810095858

4. hystrix

断路器

1495376295-5714a86048d32_articlex

5. zuul

类似nginx,提供反向代理的功能

2240067315-5714a89f0ce51_articlex

项目搭建

项目结构

image
springcloud-server 提供的服务
springcloud-client 通过feginClient调用服务
springcloud-feginclient 通过feginClient调用server
springcloud-parent maven父项目

parent

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<groupId>com.kite.test</groupId>
<artifactId>springcloud-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>springcloud-parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>../springcloud-client</module>
<module>../springcloud-server</module>
<module>../springcloud-feginclient</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
</project>
server

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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>
<packaging>jar</packaging>
<name>springcloud-client</name>
<artifactId>springcloud-server</artifactId>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.kite.test</groupId>
<artifactId>springcloud-parent</artifactId>
<version>1.0.0</version>
</parent>
</project>

提供的服务

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
package com.kite.test.springcloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
*
* 类HelloController.java的实现描述:暴露对外服务
* @author pengliang 2016年8月8日 下午4:23:14
*/
@RestController
public class HelloController {
/**
* rest 服务用来测试
* --@requestParam url?xxx=name
* --requestBody 认定为json传输解析 url?{xxx=name}
* @param name
* @return
*/
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(String name) {
return "{hello: '" + name + "'}";
}
}

启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.kite.test.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//springBoot 作为主启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
feginClient

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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>
<packaging>jar</packaging>
<name>springcloud-feginclient</name>
<artifactId>springcloud-feginclient</artifactId>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.kite.test</groupId>
<artifactId>springcloud-parent</artifactId>
<version>1.0.0</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

feginClient提供接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.kite.test.springcloud.feginclient;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* feginClient接口
* 类HelloFeginClient.java的实现描述:通过feginClient自动调用
* @author pengliang 2016年8月8日 下午4:25:36
*/
@FeignClient(value="HelloServer") //对应到的server端的spring.application.name
public interface HelloFeginClient {
@RequestMapping(value = "/hello", method=RequestMethod.POST)
public String hello(@RequestParam(name="name") String name);
}
cliet

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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>
<packaging>jar</packaging>
<name>springcloud-client</name>
<artifactId>springcloud-client</artifactId>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.kite.test</groupId>
<artifactId>springcloud-parent</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.kite.test</groupId>
<artifactId>springcloud-feginclient</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

client 调用服务类

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
package com.kite.test.springcloud.client.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.kite.test.springcloud.feginclient.HelloFeginClient;
/**
* 调用测试
* 类CallHelloController.java的实现描述:调用feginClient测试
* @author pengliang 2016年8月8日 下午4:42:14
*/
@RestController
public class CallHelloController {
private Logger log = LoggerFactory.getLogger(CallHelloController.class);
@Autowired
private HelloFeginClient helloFeginClient;
@RequestMapping(value="/hello", method = RequestMethod.GET)
public String hello(String name) {
log.info("call hello parameter:{}", name);
return helloFeginClient.hello(name);
}
}

client 启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kite.test.springcloud.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.kite.test")
@EnableCircuitBreaker
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
调用流程图

image

应用实例:

在具体的微服务用力中我们一般采用json来作为数据传输格式,通过feginClient来对服务调用来做一层封装hystrix在对feginClient调用时对依赖失败做隔离,ribbon做负载均衡(使用feginClient时已经默认集成ribbon)

项目源码 https://github.com/mykite/springcloud-test-compoments.git