1.1 下载源码
我们可以在下面链接下载Soul源码 链接,下载完成后通过以下命令进行编译。
mvn clean package install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Drat.skip=true -Dcheckstyle.skip=true
1.1.1 Soul-admin
soul-admin soul控制台,主要负载soul的元数据管理,它是集成了一个前端管理控制台,可以配置插件,限流,防火墙等功能。
server:
port: 9095
address: 0.0.0.0
spring:
thymeleaf:
cache: true
encoding: utf-8
enabled: true
prefix: classpath:/static/
suffix: .html
datasource:
url: jdbc:mysql://localhost:3306/soul?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
mybatis:
config-location: classpath:/mybatis/mybatis-config.xml
mapper-locations: classpath:/mappers/*.xml
soul:
sync:
websocket:
enabled : true
如上,我们只需要将我们的Mysql数据库配上去,然后启动,Soul会自动为我们创建数据库和对应的表。
Database changed
mysql> show tables
-> ;
+--------------------+
| Tables_in_soul |
+--------------------+
| app_auth |
| auth_param |
| auth_path |
| dashboard_user |
| meta_data |
| plugin |
| plugin_handle |
| rule |
| rule_condition |
| selector |
| selector_condition |
| soul_dict |
+--------------------+
12 rows in set (0.01 sec)
然后我们访问这个链接就可以进行访问了 http://localhost:9095/ 用户名和密码是admin/123456。
1.1.2 soul-bootstrap
soul-bootstrap 根据soul-admin的配置负责将请求转发的后面的服务
配置文件:
# 这里配置soul-bootstrap 与 soul-admin的交互的方式,我们先使用最简单的websocket,这里配置上soul-admin的地址
soul :
file:
enabled: true
corss:
enabled: true
dubbo :
parameter: multi
sync:
websocket :
urls: ws://localhost:9095/websocket
soul-bootstrap 的地址是 http://127.0.0.1:9195 访问结果如图
soul-admin 目前还没有任何信息,soul-bootstrap 可以集成dubbo SpringCloud SpringBoot等模块,我们接来一个个使用。
1.1.3 soul-test-dubbo
首先我们需要在插件管理开启Dubbo插件
然后我们在 spring-dubbo.xml 这个文件配置上zookeeper 就可以启动了
<dubbo:registry address="zookeeper://localhost:2181"/>
我们再看一下元数据管理就可以看到注册上soul-admin 的内容了
dubbo中暴露的接口信息如下
@Service("dubboTestService")
public class DubboTestServiceImpl implements DubboTestService {
@Override
@SoulDubboClient(path = "/findById", desc = "根据用户查询")
public DubboTest findById(final String id) {
DubboTest dubboTest = new DubboTest();
dubboTest.setId(id);
dubboTest.setName("hello world Soul Alibaba Dubbo, findById");
return dubboTest;
}
@Override
@SoulDubboClient(path = "/findAll", desc = "获取所有")
public DubboTest findAll() {
DubboTest dubboTest = new DubboTest();
dubboTest.setName("hello world Soul Alibaba Dubbo , findAll");
dubboTest.setId(String.valueOf(new Random().nextInt()));
return dubboTest;
}
@Override
@SoulDubboClient(path = "/insert", desc = "插入一条数据")
public DubboTest insert(final DubboTest dubboTest) {
dubboTest.setName("hello world Soul Alibaba Dubbo: " + dubboTest.getName());
return dubboTest;
}
}
我们需要重启一下 soul-admin 和 soul-bootstrap 然后就可以访问了。
压测得到以下数据
mac@AndydeMacBook-Pro ~ wrk -t 4 -c 20 http://localhost:9195/dubbo/findAll
Running 10s test @ http://localhost:9195/dubbo/findAll
4 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.26ms 2.80ms 48.48ms 94.34%
Req/Sec 1.71k 257.03 2.16k 78.00%
68000 requests in 10.01s, 12.13MB read
Requests/sec: 6789.82
Transfer/sec: 1.21MB
1.1.3 soul-test-http 测试SpringBoot 项目
soul 配置如下,设置contextPath 和 admin 的地址
soul:
http:
adminUrl: http://localhost:9095
port: 8188
contextPath: /http
appName: http
full: false
主要接口如下:只需要配置 @SoulSpringMvcClient 这个注解即可使用
@RestController
@RequestMapping("/test")
@SoulSpringMvcClient(path = "/test/**")
public class HttpTestController {
@PostMapping("/payment")
public UserDTO post(@RequestBody final UserDTO userDTO) {
return userDTO;
}
@GetMapping("/findByUserId")
public UserDTO findByUserId(@RequestParam("userId") final String userId) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(userId);
userDTO.setUserName("hello world");
return userDTO;
}
@GetMapping("/path/{id}")
public UserDTO getPathVariable(@PathVariable("id") final String id, @RequestParam("name") final String name) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
}
@GetMapping("/path/{id}/name")
public UserDTO testRestFul(@PathVariable("id") final String id) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
}
@PutMapping("/putPathBody/{id}")
public UserDTO putPathVariableAndBody(@PathVariable("id") final String id, @RequestBody final UserDTO userDTO) {
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
}
}
开启Divide 插件
如图已经能查看到元数据信息
访问情况
压测情况
mac@AndydeMacBook-Pro ~ wrk -t 4 -c 20 http://localhost:9195/http/test/findByUserId\?userId\=1
Running 10s test @ http://localhost:9195/http/test/findByUserId?userId=1
4 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 11.85ms 14.74ms 126.42ms 86.84%
Req/Sec 667.48 163.61 1.22k 72.25%
26616 requests in 10.02s, 2.79MB read
Requests/sec: 2656.05
Transfer/sec: 285.34KB