- soul网关使用 divide 插件来处理http请求
- divide 插件实现 HTTP 正向代理的功能,所有 HTTP 类型的请求,都由它进行负载均衡的调用。
- 官方提供的各种demo https://github.com/dromara/soul/tree/master/soul-examples
.
├── pom.xml
├── soul-examples-dubbo
├── soul-examples-eureka
├── soul-examples-http
├── soul-examples-sofa
├── soul-examples-springcloud
├── soul-examples-tars
└── soul-examples.iml
soul-examples-http
.
├── pom.xml
├── soul-examples-http.iml
├── src
│ └── main
│ ├── java
│ │ └── org
│ │ └── dromara
│ │ └── soul
│ │ └── examples
│ │ └── http
│ │ ├── SoulTestHttpApplication.java
│ │ ├── config
│ │ │ ├── HttpServerConfig.java
│ │ │ └── WebSocketConfiguration.java
│ │ ├── controller
│ │ │ ├── HttpTestController.java
│ │ │ └── OrderController.java
│ │ ├── dto
│ │ │ ├── OrderDTO.java
│ │ │ ├── RequestDTO.java
│ │ │ └── UserDTO.java
│ │ ├── result
│ │ │ ├── PaymentTypeService.java
│ │ │ └── ResultBean.java
│ │ ├── router
│ │ │ └── SoulTestHttpRouter.java
│ │ └── websocket
│ │ └── EchoHandler.java
│ └── resources
│ └── application.yml
引入的maven依赖
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springmvc</artifactId>
<version>2.2.1</version>
</dependency>
配置文件
soul:
http:
adminUrl: http:localhost:9095 #Soul Admin 地址
port: 8188 #本项目的启动端口
contextPath: /http #设置在 Soul 网关的路由前缀,例如说 /order、/product
appName: http #应用名。未配置情况下,默认使用 `spring.application.name` 配置项
full: false #设置true 代表代理你的整个服务,false表示代理你其中某几个controller
需要在 Controller 的 HTTP API 方法上,添加 @SoulSpringMvcClient
@RestController
@RequestMapping("/test")
@SoulSpringMvcClient(path = "/test/**")
public class HttpTestController {
/**
* Post user dto.
*
* @param userDTO the user dto
* @return the user dto
*/
@PostMapping("/payment")
public UserDTO post(@RequestBody final UserDTO userDTO) {
return userDTO;
}
/**
* Find by user id string.
*
* @param userId the user id
* @return the string
*/
@GetMapping("/findByUserId")
public UserDTO findByUserId(@RequestParam("userId") final String userId) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(userId);
userDTO.setUserName("hello world");
return userDTO;
}
}
- 启动soul-admin
- 启动soul-bootstrap 网关地址: http://localhost:9195/
- 启动soul-examples-http http://localhost:8188/
- 进入soul-admin http://localhost:9095/ 用户名:admin 密码:123456
- 可看见divide插件已开启
服务名称 | 服务端口 | 功能说明 |
---|---|---|
soul-admin | 9095 | 管理控制台 |
soul-examples-http | 8188 | 正式的项目 |
soul-bootstrap | 9195 | 网关地址 |
http://localhost:8188/order/findById?id=1
网关地址的定义规则
通过网关访问
http://localhost:9195/http/order/findById?id=1
和真实的服务地址访问结果比较一致
服务名称 | 服务端口 | 功能说明 |
---|---|---|
soul-admin | 9095 | 管理控制台 |
soul-examples-http | 8188 | 正式的项目 |
soul-examples-http | 8189 | 正式的项目 |
soul-bootstrap | 9195 | 网关地址 |
更改并行启动
更改端口
启动
进入控制台
weight为100的port是8189 、weight为1的port是8188
结果基本上都在8189的链接上