java - Scala、Akka-Http 和 mysql - 太慢了
问题描述
我试图将 MySQL 连接到我的 scala-Akka 代码。在接收方法中,我有 3 个 URL,对于这些 URL,我实现了一个简单的计数器。所以每当我访问那个特定的 URL 时,我都会在一个表中插入一行,这将是(URL、计数、它的时间戳)。但我注意到它太慢了。10 秒内仅完成 180 个请求!我可以做些什么来提高性能?请帮忙!
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.pattern.ask
import akka.stream.ActorMaterializer
import akka.util.Timeout
import scala.concurrent.duration._
import scala.io.StdIn
import java.sql.{Connection, DriverManager, PreparedStatement, Timestamp}
import java.time.{LocalDateTime, LocalTime}
import java.util.Date
import akka.http.javadsl.server.Route
object DBCP3 {
final case class fetch1(param:String)
val sql = "INSERT INTO URLInfo (url, time, count)" + "VALUES (?, ?, ?)"
val url = "jdbc:mysql://127.0.0.1/url"
val driver = "com.mysql.jdbc.Driver"
val username = "root"
val password = "SomePassword"
Class.forName(driver)
var connection = DriverManager.getConnection(url, username, password)
val date = new Date
private var number1: Int = 0
private var number3: Int = 0
private var number2: Int = 0
def insertIntoDB(path: String, num: Int) = {
val stm: PreparedStatement = connection.prepareStatement(sql)
stm.setString(1, path)
stm.setTimestamp(2, new Timestamp(date.getTime))
stm.setInt(3, num)
stm.execute()
}
class ActorA extends Actor with ActorLogging {
def receive = {
case fetch1(param) =>
if(param=="path1") {
number1+=1
insertIntoDB("http://localhost:8080/path1",number1)
context.sender() ! number1
}
if(param=="path2") {
number2+=1
insertIntoDB("http://localhost:8080/path2",number2)
context.sender() ! number2
}
if(param=="path3") {
number3+=1
insertIntoDB("http://localhost:8080/path3",number3)
context.sender() ! number3
}
}
}
def main(args: Array[String]) {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
implicit val timeout: Timeout = 1.seconds
val actor1 = system.actorOf(Props[ActorA], "SimpleActor1")
val route = concat(
path("path1") {
get {
onComplete((actor1 ? fetch1("path1")).mapTo[Int])
{
number => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"<h1>You visited $number times</h1>"))
}
}
},
path("path2") {
onComplete((actor1 ? fetch1("path2")).mapTo[Int])
{
number => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"<h1>You visited $number times</h1>"))
}
},
path("path3") {
onComplete((actor1 ? fetch1("path3")).mapTo[Int])
{
number => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"<h1>You visited $number times</h1>"))
}
}
)
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
val _ = bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
connection.close()
}
}
解决方案
推荐阅读
- ios - 如何以秒为单位获取当前时间并在 Swift 中添加玩家持续时间?
- c++ - 有效地检查一个字符串是否是另一个字符串的近似子字符串(近似约束),直到给定的错误阈值?
- makefile - GNU 制作 | 可以在执行时更改规则的目标和先决条件吗?
- python - 条带源创建失败
- icecast - 无论如何,当安装点停止时提醒我
- javascript - 在useEffect中使用setState时如何防止我的React代码中的无限循环
- mysql - 联合同一张表中的同一列
- c++ - 类声明中的“is_base_of”
- git - 不能。切换项目
- python - 在从python获得的nodejs中打印csv字符串