Tars快速开始

本文档将演示如何快速使用Tars接入Soul网关。您可以直接在工程下找到本文档的示例代码

环境准备

请参考配置网关环境并启动soul-adminsoul-bootstrap

注:soul-bootstrap需要引入tars依赖

  1. <dependency>
  2. <groupId>org.dromara</groupId>
  3. <artifactId>soul-spring-boot-starter-plugin-tars</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.tencent.tars</groupId>
  8. <artifactId>tars-client</artifactId>
  9. <version>1.7.2</version>
  10. </dependency>

运行soul-examples-tars项目

下载soul-examples-tars

修改application.yml中的host为你本地ip

修改配置src/main/resources/SoulExampleServer.SoulExampleApp.config.conf

  • 建议弄清楚 config 的主要配置项含义, 参考开发指南
  • config 中的 ip 要注意提供成本机的
  • local=…, 表示开放的本机给 tarsnode 连接的端口, 如果没有 tarsnode, 可以掉这项配置
  • locator: registry服务的地址,必须是有ip port的,如果不需要registry来定位服务,则不需要配置;
  • node=tars.tarsnode.ServerObj@xxxx,表示连接的 tarsnode 的地址,如果本地没有 tarsnode, 这项配置可以去掉

更多config配置说明请参考Tars官方文档

运行org.dromara.soul.examples.tars.SoulTestTarsApplicationmain方法启动项目。

注: 服务启动时需要在启动命令中指定配置文件地址 -Dconfig=xxx/SoulExampleServer.SoulExampleApp.config.conf

如果不加-Dconfig参数配置会可能会如下抛异常:

  1. com.qq.tars.server.config.ConfigurationException: error occurred on load server config
  2. at com.qq.tars.server.config.ConfigurationManager.loadServerConfig(ConfigurationManager.java:113)
  3. at com.qq.tars.server.config.ConfigurationManager.init(ConfigurationManager.java:57)
  4. at com.qq.tars.server.core.Server.loadServerConfig(Server.java:90)
  5. at com.qq.tars.server.core.Server.<init>(Server.java:42)
  6. at com.qq.tars.server.core.Server.<clinit>(Server.java:38)
  7. at com.qq.tars.spring.bean.PropertiesListener.onApplicationEvent(PropertiesListener.java:37)
  8. at com.qq.tars.spring.bean.PropertiesListener.onApplicationEvent(PropertiesListener.java:31)
  9. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
  10. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
  11. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
  12. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
  13. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
  14. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
  15. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
  16. at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
  17. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
  18. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
  19. at org.dromara.soul.examples.tars.SoulTestTarsApplication.main(SoulTestTarsApplication.java:38)
  20. Caused by: java.lang.NullPointerException
  21. at java.io.FileInputStream.<init>(FileInputStream.java:130)
  22. at java.io.FileInputStream.<init>(FileInputStream.java:93)
  23. at com.qq.tars.common.util.Config.parseFile(Config.java:211)
  24. at com.qq.tars.server.config.ConfigurationManager.loadServerConfig(ConfigurationManager.java:63)
  25. ... 17 more
  26. The exception occurred at load server config

成功启动会有如下日志:

  1. [SERVER] server starting at tcp -h 127.0.0.1 -p 21715 -t 60000...
  2. [SERVER] server started at tcp -h 127.0.0.1 -p 21715 -t 60000...
  3. [SERVER] server starting at tcp -h 127.0.0.1 -p 21714 -t 3000...
  4. [SERVER] server started at tcp -h 127.0.0.1 -p 21714 -t 3000...
  5. [SERVER] The application started successfully.
  6. The session manager service started...
  7. [SERVER] server is ready...
  8. 2021-02-09 13:28:24.643 INFO 16016 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 55290 (http) with context path ''
  9. 2021-02-09 13:28:24.645 INFO 16016 --- [ main] o.d.s.e.tars.SoulTestTarsApplication : Started SoulTestTarsApplication in 4.232 seconds (JVM running for 5.1)
  10. 2021-02-09 13:28:24.828 INFO 16016 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : tars client register success: {"appName":"127.0.0.1:21715","contextPath":"/tars","path":"/tars/helloInt","pathDesc":"","rpcType":"tars","serviceName":"SoulExampleServer.SoulExampleApp.HelloObj","methodName":"helloInt","ruleName":"/tars/helloInt","parameterTypes":"int,java.lang.String","rpcExt":"{\"methodInfo\":[{\"methodName\":\"helloInt\",\"params\":[{},{}],\"returnType\":\"java.lang.Integer\"},{\"methodName\":\"hello\",\"params\":[{},{}],\"returnType\":\"java.lang.String\"}]}","enabled":true}
  11. 2021-02-09 13:28:24.837 INFO 16016 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils : tars client register success: {"appName":"127.0.0.1:21715","contextPath":"/tars","path":"/tars/hello","pathDesc":"","rpcType":"tars","serviceName":"SoulExampleServer.SoulExampleApp.HelloObj","methodName":"hello","ruleName":"/tars/hello","parameterTypes":"int,java.lang.String","rpcExt":"{\"methodInfo\":[{\"methodName\":\"helloInt\",\"params\":[{},{}],\"returnType\":\"java.lang.Integer\"},{\"methodName\":\"hello\",\"params\":[{},{}],\"returnType\":\"java.lang.String\"}]}","enabled":true}

tars 插件设置

  • soul-admin 插件管理中,把tars 插件设置为开启。

测试

soul-examples-tars项目成功启动之后会自动把加 @SoulTarsClient 注解的接口方法注册到网关。

打开插件管理->tars可以看到插件规则配置列表

Tars快速开始 - 图1

下面使用postman模拟http的方式来请求你的tars服务

Tars快速开始 - 图2