国庆假期,一直没有时间更新。
根据群里面的同窗的提问,强烈推荐你们先熟悉下spring cloud。文章下面有纯洁大神的spring cloud系列。
上一章最后说了,由于服务是不对外暴露的,因此在外网要访问服务必须经过API网关来完成,而spring cloud 提供了现成的Api网关组件zuul。它包含了路由,受权,压力测试等一系列功能。以下图所示,Api网关在整个应用环境的位置。
html
咱们先模拟一个业务场景,客户端(web,ios,android...)经过Api网关访问订单服务,订单服务有两个节点,为了模拟集群效果,这两个节点分别返回不一样的数据。那么咱们一共须要建立4个应用程序。服务中心(Java)、Api网关(Java)、订单服务1(.NET Core)、订单服务2(.NET Core)。java
使用intellij idea建立一个spring boot项目,建立服务中心。设置端口为5000。
pom.xmlandroid
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
ServiceCenterApplication.javaios
@EnableEurekaServer @SpringBootApplication public class ServiceCenterApplication { public static void main(String[] args) { SpringApplication.run(ServiceCenterApplication.class, args); } }
application.propertiesnginx
spring.application.name=service-center server.port=5000
使用intellij idea建立一个spring boot项目,建立Api网关服务。设置端口为5555。
pom.xmlgit
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
ServiceGatewayApplication.javagithub
@SpringBootApplication @EnableZuulProxy public class ServiceGatewayApplication { public static void main(String[] args) { SpringApplication.run(ServiceGatewayApplication.class, args); } }
application.propertiesweb
spring.application.name=service-gateway server.port=5555 eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/ #下面的代码能够注释 zuul.routes.order.path=/order/** zuul.routes.order.serviceId=order
上面配置是否是和nginx很像。zuul还提供了默认规则,
http://ZUUL_HOST:ZUUL_PORT/serviceId/**
,知足这一规则的会自动代理,如上面的配置彻底能够不用写,这样大量的服务就不用一个一个配置了。spring
使用vs2017建立 .NET Core Web Api应用程序json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "spring": { "application": { "name": "order" } }, "eureka": { "client": { "serviceUrl": "http://localhost:5000/eureka/" }, "instance": { "port": 8010 } } }
[Route("/")] public class ValuesController : Controller { [HttpGet] public string Get() { return "order1"; } }
同订单服务1的建立过程,修改端口为8011和返回结果。
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "spring": { "application": { "name": "order" } }, "eureka": { "client": { "serviceUrl": "http://localhost:5000/eureka/" }, "instance": { "port": 8011 } } }
[Route("/")] public class ValuesController : Controller { [HttpGet] public string Get() { return "order2"; } }
篇幅有限,以上代码均有精简,完整代码请去Github上获取。
咱们如今一共有4个应用程序:
- eureka服务中心,端口5000,应用名称service-center
- zuul网关服务,端口5555,应用名称service-gateway
- 订单服务1,端口8010,应用名称order
- 订单服务2,端口8011,应用名称order
其中订单服务1和订单服务2组成了订单服务集群
分别启动这4个应用程序。打开eureka服务:http://localhost:5000/,
以下图所示都注册成功。
打开http://localhost:5555/order,返回"order1"
刷新后返回"order2",反复屡次刷新,"order1"和"order2"依次返回。
经过上面的例子说明Api网关服务已经生效,而且实现了负载均衡。结合具体的业务场景,咱们的生产环境只对外暴露5555端口,客户端访问Api网关,由Api网关去路由到各个服务节点,这样全部的客户端调用都统一了入口。
全部代码均上传github。代码按照章节的顺序上传,例如第一章demo1,第二章demo2以此类推。
求推荐,大家的支持是我写做最大的动力,个人QQ群:328438252,交流微服务。
java部分
.net部分