场景
本文将以一个简单的Hello服务演示网关的使用,Hello服务提供一个hello/{name}接口,只要传递路径参数name就可以返回打招呼内容。接口返回结果通过网关将在浏览器界面显示
技术准备
ServiceComb官网:http://servicecomb.apache.org/cn/
使用ServiceComb作为后端框架,
ServiceCenter作为服务发现与注册中心,
Spring Cloud Zuul组件做服务网关
服务网关是微服务架构中一个不可或缺的部分。通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由、均衡负载功能之外,它还具备了权限控制等功能。Spring Cloud Netflix中的Zuul就担任了这样的一个角色,为微服务架构提供了前门保护的作用,同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,使得服务集群主体能够具备更高的可复用性和可测试性。
环境准备
以下环境为Windows 64位系统
ServiceCenter安装
在该目录下双击service-center.exe即可启动,命令窗口中出现如下信息基本代表ServiceCenter启动成功,从这个信息也可以得知ServiceCenter监听的是30100端口,等下配置文件要用到。
问题点: 有可能会有如下信息,这个一般是端口被占用,很可能你打开了两个ServiceCenter,都关闭后再打开就可以了。
集成Demo
- RestSchema:作用类似于RestController,但概念上有所区别,是一个服务契约。
新建maven项目HelloService,pom文件如下
1. 编写Zuul网关
新建maven项目 zuulserver,pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.servicecomb.example</groupId>
<artifactId>zuulserver</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-servicecomb</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-starter-discovery</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-cloud-zuul</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<dependencyManagement