You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.9 KiB
167 lines
4.9 KiB
# 项目接入到SOP
|
|
|
|
以springboot项目为例,完整项目可参考sop-example下的sop-story
|
|
|
|
- pom.xml添加版本配置
|
|
|
|
```xml
|
|
<properties>
|
|
<!-- springboot 版本-->
|
|
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
|
|
<!-- spring cloud 版本 -->
|
|
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
|
|
<!-- spring cloud alibaba 版本 -->
|
|
<!-- 具体版本对应关系见:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E -->
|
|
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
|
|
</properties>
|
|
```
|
|
|
|
- pom.xml添加SpringCloud支持
|
|
|
|
```xml
|
|
<dependencyManagement>
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-dependencies</artifactId>
|
|
<version>${spring-boot.version}</version>
|
|
<type>pom</type>
|
|
<scope>import</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>org.springframework.cloud</groupId>
|
|
<artifactId>spring-cloud-dependencies</artifactId>
|
|
<version>${spring-cloud.version}</version>
|
|
<type>pom</type>
|
|
<scope>import</scope>
|
|
</dependency>
|
|
<dependency>
|
|
<groupId>com.alibaba.cloud</groupId>
|
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
|
<version>${spring-cloud-alibaba.version}</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>
|
|
|
|
<dependency>
|
|
<groupId>com.alibaba.cloud</groupId>
|
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
|
</dependency>
|
|
```
|
|
|
|
- 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,网关会触发监听事件,获取新注册的服务,然后会向你的服务拉取路由配置。 |