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.
217 lines
6.3 KiB
217 lines
6.3 KiB
# 项目接入到SOP
|
|
|
|
以springboot项目为例,完整项目可参考sop-example下的sop-story
|
|
|
|
- pom.xml添加版本配置
|
|
|
|
```xml
|
|
<!-- springboot 版本-->
|
|
<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
|
|
<!-- spring cloud 版本 -->
|
|
<spring-cloud.version>Hoxton.SR8</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.5.RELEASE</spring-cloud-alibaba.version>
|
|
```
|
|
|
|
- pom.xml添加`<dependencyManagement>`控制版本
|
|
|
|
```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 {
|
|
|
|
}
|
|
```
|
|
|
|
- 全局异常处理
|
|
|
|
在微服务项目的全局异常处理中添加一句:`ExceptionHolder.hold(request, response, exception);`
|
|
|
|
```java
|
|
@ExceptionHandler(Exception.class)
|
|
@ResponseBody
|
|
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
|
...
|
|
// 在返回前加这一句
|
|
ExceptionHolder.hold(request, response, exception);
|
|
...
|
|
return ..;
|
|
}
|
|
```
|
|
|
|
如果没有配置全局异常,可参考下面配置
|
|
|
|
```java
|
|
@ControllerAdvice
|
|
@Slf4j
|
|
public class StoryGlobalExceptionHandler {
|
|
|
|
|
|
/**
|
|
* 捕获手动抛出的异常
|
|
*
|
|
* @param request request
|
|
* @param response response
|
|
* @param exception 异常信息
|
|
* @return 返回提示信息
|
|
*/
|
|
@ExceptionHandler(Exception.class)
|
|
@ResponseBody
|
|
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
|
// 在返回前加这一句
|
|
ExceptionHolder.hold(request, response, exception);
|
|
// 下面可以实现自己的全局异常处理
|
|
return new ErrorResult(500, exception.getMessage());
|
|
}
|
|
|
|
@Data
|
|
@AllArgsConstructor
|
|
public static class ErrorResult {
|
|
private int code;
|
|
private String msg;
|
|
}
|
|
}
|
|
```
|
|
|
|
到此准备工作就完成了,接下来可前往`新增接口`查看如何新增接口。
|
|
|
|
## 非Java项目接入
|
|
|
|
如果是非Java项目接入,如php,需要做到以下几点:
|
|
|
|
> 1. 提供路由配置接口
|
|
> 2. 服务注册到nacos,并在nacos的metadata中指定接口路径,metadata的key为:`sop.routes.path`
|
|
|
|
|
|
- 提供路由配置接口
|
|
|
|
php应用提供的接口需要返回如下json内容:
|
|
|
|
假设请求的接口为:`http://open.xxx.com/get_routes`
|
|
|
|
```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", "http://open.xxx.com/get_routes");
|
|
|
|
namingService.registerInstance(serviceId, instance);
|
|
```
|
|
|
|
完成以上步骤后,php服务注册到nacos,网关会触发监听事件,获取新注册的服务,然后会向你的服务拉取路由配置。 |