|
|
|
# 项目接入到SOP
|
|
|
|
|
|
|
|
以springboot项目为例,完整项目可参考sop-example下的sop-story
|
|
|
|
|
|
|
|
- pom.xml添加版本配置
|
|
|
|
|
|
|
|
```xml
|
|
|
|
<properties>
|
|
|
|
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
|
|
|
<!-- nacos spring cloud版本号 -->
|
|
|
|
<spring-cloud-alibaba-nacos.version>0.9.0.RELEASE</spring-cloud-alibaba-nacos.version>
|
|
|
|
<!-- nacos客户端版本号 -->
|
|
|
|
<nacos-client.version>1.1.3</nacos-client.version>
|
|
|
|
</properties>
|
|
|
|
```
|
|
|
|
|
|
|
|
- pom.xml添加SpringCloud支持
|
|
|
|
|
|
|
|
```xml
|
|
|
|
<dependencyManagement>
|
|
|
|
<dependencies>
|
|
|
|
<dependency>
|
|
|
|
<groupId>org.springframework.cloud</groupId>
|
|
|
|
<artifactId>spring-cloud-dependencies</artifactId>
|
|
|
|
<version>Greenwich.RELEASE</version>
|
|
|
|
<type>pom</type>
|
|
|
|
<scope>import</scope>
|
|
|
|
</dependency>
|
|
|
|
</dependencies>
|
|
|
|
</dependencyManagement>
|
|
|
|
```
|
|
|
|
|
|
|
|
- pom.xml依赖sop-service-common和nacos服务发现
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
|
|
|
<dependency>
|
|
|
|
<groupId>com.gitee.sop</groupId>
|
|
|
|
<artifactId>sop-service-common</artifactId>
|
|
|
|
<version>最新版本</version>
|
|
|
|
</dependency>
|
|
|
|
|
|
|
|
<!-- 使用nacos注册中心
|
|
|
|
版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
|
|
|
|
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-alibaba-nacos-discovery
|
|
|
|
-->
|
|
|
|
<dependency>
|
|
|
|
<groupId>org.springframework.cloud</groupId>
|
|
|
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
|
|
|
<version>${spring-cloud-alibaba-nacos.version}</version>
|
|
|
|
</dependency>
|
|
|
|
<dependency>
|
|
|
|
<groupId>com.alibaba.nacos</groupId>
|
|
|
|
<artifactId>nacos-client</artifactId>
|
|
|
|
<version>${nacos-client.version}</version>
|
|
|
|
</dependency>
|
|
|
|
<!-- 注册中心end -->
|
|
|
|
```
|
|
|
|
|
|
|
|
- application.properties配置文件添加
|
|
|
|
|
|
|
|
```properties
|
|
|
|
server.port=2222
|
|
|
|
# 服务名称
|
|
|
|
spring.application.name=story-service
|
|
|
|
# nacos注册中心
|
|
|
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
|
|
|
```
|
|
|
|
|
|
|
|
- 在springboot启动类上添加`@EnableDiscoveryClient`
|
|
|
|
- 新增一个配置类,继承`AlipayServiceConfiguration.java`,内容为空
|
|
|
|
|
|
|
|
```java
|
|
|
|
@Configuration
|
|
|
|
public class OpenServiceConfig extends AlipayServiceConfiguration {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
到此准备工作就完成了,接下来可前往`新增接口`查看如何新增接口。
|
|
|
|
|
|
|
|
## 非Java项目接入
|
|
|
|
|
|
|
|
如果是非Java项目接入,如php,需要做到以下几点:
|
|
|
|
|
|
|
|
> 1. 提供路由配置接口
|
|
|
|
> 2. 服务注册到nacos,并在nacos的metadata中指定接口路径,metadata的key为:`sop.routes.path`
|
|
|
|
|
|
|
|
|
|
|
|
- 提供路由配置接口
|
|
|
|
|
|
|
|
php应用提供的接口需要返回如下json内容:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"serviceId": "goods-service",
|
|
|
|
"createTime": 1568603471646,
|
|
|
|
"updateTime": 1568603471646,
|
|
|
|
"description": null,
|
|
|
|
"routeDefinitionList": [
|
|
|
|
{
|
|
|
|
"id": "goods.list1.0",
|
|
|
|
"name": "goods.list",
|
|
|
|
"version": "1.0",
|
|
|
|
"uri": "lb://goods-service",
|
|
|
|
"path": "/goods/list_goods",
|
|
|
|
"order": 0,
|
|
|
|
"ignoreValidate": 0,
|
|
|
|
"status": 1,
|
|
|
|
"mergeResult": 1,
|
|
|
|
"permission": 0
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
json参数说明
|
|
|
|
|
|
|
|
|参数名|是否必填|说明|
|
|
|
|
|:----|:----|:----|
|
|
|
|
|serviceId |是|serviceId,服务id |
|
|
|
|
|createTime |是|创建时间,Unix timestamp,毫秒 |
|
|
|
|
|updateTime |是|修改时间,Unix timestamp,毫秒 |
|
|
|
|
|description|否|描述|
|
|
|
|
|routeDefinitionList元素参数说明|是|路由配置,见:routeDefinitionList元素参数说明|
|
|
|
|
|
|
|
|
routeDefinitionList元素参数说明
|
|
|
|
|
|
|
|
|参数名|是否必填|说明|
|
|
|
|
|:----|:----|:----|
|
|
|
|
|id |是|路由id,全局唯一,格式:接口名+版本号 |
|
|
|
|
|name|是|接口名称|
|
|
|
|
|version|是|版本号|
|
|
|
|
|uri|是|格式:lb:// + serviceId,如:lb://goods-service|
|
|
|
|
|path|是|接口path,填端口号后面的path,如你的接口为`http://open.domain.com:8080/goods/list_goods`,填:`/goods/list_goods`|
|
|
|
|
|order|是|固定填:0|
|
|
|
|
|ignoreValidate|是|忽略签名验证,1:是,0:否|
|
|
|
|
|status|是|启用状态,1:启用,2:禁用|
|
|
|
|
|mergeResult|是|是否统一返回结果,1:是,0:否|
|
|
|
|
|permission|是|是否需要权限访问,1:是,0:否|
|
|
|
|
|
|
|
|
- 服务注册到nacos
|
|
|
|
|
|
|
|
可前往nacos官网,参考[open-api](https://nacos.io/zh-cn/docs/open-api.html),使用nacos提供的接口完成服务注册
|
|
|
|
|
|
|
|
|
|
|
|
- 在nacos的metadata中指定接口路径
|
|
|
|
|
|
|
|
伪代码如下:
|
|
|
|
|
|
|
|
```java
|
|
|
|
Instance instance = new Instance();
|
|
|
|
instance.setServiceName("goods-service");
|
|
|
|
instance.setIp("192.168.0.11");
|
|
|
|
instance.setPort(8080);
|
|
|
|
// 在nacos的metadata中指定接口路径
|
|
|
|
instance.getMetadata().put("sop.routes.path", "/goods/list_goods");
|
|
|
|
|
|
|
|
namingService.registerInstance(serviceId, instance);
|
|
|
|
```
|
|
|
|
|
|
|
|
完成以上步骤后,php服务注册到nacos,网关会触发监听事件,获取新注册的服务,然后会向你的服务拉取路由配置。
|