spring cloud是一系列框架的有序集合。它利用spring boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用spring boot的开发风格做到一键启动和部署。spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过spring boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
接下来我们就使用springcloud实现一套简单的微服务架构。
以下所有代码都已开源到github上了,地址:https://github.com/lynnlovemin/softservice
eureka(服务注册与发现)
首先引入相关的依赖包
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
64
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.rc1</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> |
配置application.yml
1
2
3
4
5
6
7
8
9
10
|
server: port: 8761 eureka: instance: hostname: localhost client: registerwitheureka: false fetchregistry: false serviceurl: defaultzone: http: //${eureka.instance.hostname}:${server.port}/eureka/ |
创建启动类application
1
2
3
4
5
6
7
8
|
@enableeurekaserver @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application. class , args); } } |
运行main方法,浏览器访问:http://localhost:8761,我们就能在浏览器看到如下界面:
说明eureka启动成功。
接下来,我们实现负债均衡、断路器、网关、客户端,所有的服务都应该注册到eureka中,并且访问eureka就能看到所有注册的服务
client(客户端)
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
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.rc1</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> |
application.yml
1
2
3
4
5
6
7
8
9
|
eureka: client: serviceurl: defaultzone: http: //localhost:8761/eureka/ #这里注册到eureka中 server: port: 8763 spring: application: name: service-hi |
application类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@springbootapplication @enableeurekaclient @restcontroller public class application { public static void main(string[] args) { springapplication.run(applicatioin. class , args); } @value ( "${server.port}" ) string port; //这里我们提供一个接口 @requestmapping ( "/hi" ) public string home( @requestparam string name) { return "hi " +name+ ",i am from port:" +port; } } |
feign(负债均衡、断路器)
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
64
65
66
67
68
69
70
71
72
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-hystrix-dashboard</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.rc1</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> |
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
|
eureka: client: serviceurl: defaultzone: http: //localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign feign: hystrix: enabled: true |
application类
1
2
3
4
5
6
7
8
9
10
|
@springbootapplication @enablediscoveryclient @enablefeignclients @enablehystrixdashboard public class application { public static void main(string[] args) { springapplication.run(application. class , args); } } |
然后再提供一个service,他的作用就是做负债均衡和断路器功能
1
2
3
4
5
|
@feignclient (value = "service-hi" ,fallback = schedualservicehihystric. class ) public interface schedualservicehi { @requestmapping (value = "/hi" ,method = requestmethod.get) string sayhifromclientone( @requestparam (value = "name" ) string name); } |
1
2
3
4
5
6
7
|
@component public class schedualservicehihystric implements schedualservicehi { @override public string sayhifromclientone(string name) { return "sorry " +name; } } |
feignclient我们指定之前创建client时指定的name:service-hi,fallback指定服务不可用时的返回数据,这样我们启动多个client时就可以看到http请求时会交替访问不同的feign端口,当停掉clien时再访问接口会返回错误信息。
zuul(服务网关)
在一般情况下,我们不会直接暴露客户端给外部,而是通过服务网关来转发,内部服务都是在局域网内通信,外部访问不了,通过服务网关,我们还可以统一做接口的安全性校验,统一拦截,请看代码:
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
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 9 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <project.reporting.outputencoding>utf- 8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-zuul</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>dalston.rc1</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> |
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
eureka: client: serviceurl: defaultzone: http: //localhost:8761/eureka/ server: port: 8080 spring: application: name: service-zuul zuul: routes: api-b: path: /api/** serviceid: service-feign #凡是以api开始的请求都访问service-feign服务 |
application类
1
2
3
4
5
6
7
8
9
|
@enablezuulproxy @enableeurekaclient @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application. class , args); } } |
启动application,访问:http://localhost:8080/api/hi,就能访问到之前我们定义的接口,接下来我们做接口的拦截:
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
|
/** * filtertype:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: pre:路由之前 routing:路由之时 post: 路由之后 error:发送错误调用 filterorder:过滤的顺序 shouldfilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。 run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。 */ @component public class myfilter extends zuulfilter{ private static logger log = loggerfactory.getlogger(myfilter. class ); @override public string filtertype() { return "pre" ; } @override public int filterorder() { return 0 ; } @override public boolean shouldfilter() { return true ; } @override public object run() { requestcontext ctx = requestcontext.getcurrentcontext(); httpservletrequest request = ctx.getrequest(); log.info(string.format( "%s >>> %s" , request.getmethod(), request.getrequesturl().tostring())); object accesstoken = request.getparameter( "token" ); if (accesstoken == null ) { log.warn( "token is empty" ); ctx.setsendzuulresponse( false ); ctx.setresponsestatuscode( 401 ); try { ctx.getresponse().getwriter().write( "token is empty" ); } catch (exception e){} return null ; } log.info( "ok" ); return null ; } } |
这样我们在调用接口前会先执行myfilter类中的run方法,在这个方法里可以做一系列安全验证,比如token。
好了,一个简单的微服务架构就已经搭建完成了。
以上所有代码都已开源到github上了,地址:https://github.com/lynnlovemin/softservice
以上所述是小编给大家介绍的springcloud实现简单的微服务架构,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复
原文链接:http://blog.csdn.net/lynnlovemin/article/details/79019680