Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Services can be configured and configuration file can be configured ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Jul 7, 2019
1 parent 0f697cd commit b1e41bb
Show file tree
Hide file tree
Showing 48 changed files with 252 additions and 199 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Already Implemented functions
- Multi-module project base on SBT
- Pressure measurement base on JMH
- Configuration injection base on typesafe Config
- Services can be configured and configuration file can be configured



- loading
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Dependencies.Versions
//工程通用配置
lazy val commonSettings = Seq(
organization := "io.growing",
version := "1.0.13",
version := "1.1.0",
scalaVersion := Versions.scala212,
Dependencies.commons
)
Expand Down
71 changes: 55 additions & 16 deletions dls-benchmark/src/main/java/io/growing/dlsrpc/benchmark/说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,84 @@
* 需要压测就安装 IDEA 的 JMH插件
* 不要使用我核心包的models,那是测试用的!
* 基于JDK代理的RPC,接口和实现需要同包或者实现类在子包!
* 在哪启动哪个 dlsRPC.conf 文件就生效,若在 core 启动开发时的测试类,那么生效的配置是 core 模块下的 dlsRPC.conf
* dlsRPC.conf 可配置consul是否开启,默认开!
* 服务端启动后马上停止且状态码 0 ,一般是端口通道打开失败!
* 如果不会使用 sbt publish 那就使用 resources 下的jar
* 必须将 dlsrpc.consul.registry.package-service 属性设置为自己项目的顶级包名!


下面是最小配置项

```
//使用此RPC的项目需要提供dlsRpc.conf文件,否则使用默认配置。
//调用Configuration.config(filePath),可以使用其他config文件夹下的配置文件
//这配置文件给本测试用的,也是本地测试时最少配置
//默认注册的包下的service,需要改为自己项目的顶级包
dlsrpc.consul.registry.package-service = "io.growing.dlsrpc.test"
//若忘记配dlsrpc.consul.server.address或未开启consul,则认为是本地调用,将使用本地服务地址
//需要与服务端启动时的端口相同
dlsrpc.server.address.default = "127.0.0.1:8080"
```

下面 是 benchmark 模块的一个可用的 dlsRPC.conf 配置文件,直接放到自己项目的 resources 下面即可

```
//consul ip,本地不用改
//这是默认的
//使用此RPC的项目需要提供dlsRpc.conf文件,否则使用默认配置。
//调用Configuration.config(filePath),可以使用其他config文件夹下的配置文件
//consul ip,需要抽出去为用户配置
dlsrpc.consul.host = "127.0.0.1"
//consul 端口,本地不用改
//consul 端口,需要抽出去为用户配置
dlsrpc.consul.port = 8500
//服务器端所有可用ip,用于负载均衡,本地不需要改
//consul 服务健康检查间隔时间 1秒
dlsrpc.consul.interval = 1s
//默认开启服务注册
dlsrpc.consul.enable = true
//服务器端所有可用ip,用于负载均衡,需要抽出去为用户配置
//此配置不能完全决定服务地址的选取,但是在此配置的ip将被负载均衡优先选择
dlsrpc.consul.server.address = ["127.0.0.1"]
//默认注册的包下的service,需要改为自己项目的顶级包
dlsrpc.consul.registry.package-service = "io.growing.dlsrpc.benchmark"
//ip:port验证
dlsrpc.consul.adress-pattern = "(?:(?:[0,1]?\\d?\\d|2[0-4]\\d|25[0-5])\\.){3}(?:[0,1]?\\d?\\d|2[0-4]\\d|25[0-5]):\\d{0,5}"
//仅ip验证
dlsrpc.consul.ip-pattern = "(?:(?:[0,1]?\\d?\\d|2[0-4]\\d|25[0-5])\\.){3}(?:[0,1]?\\d?\\d|2[0-4]\\d|25[0-5])"
//客户端超时时间
//默认注册的包下的service,需要抽出去为用户配置
dlsrpc.consul.registry.package-service = "io.growing.dlsrpc"
//客户端超时时间,需要抽出去为用户配置
dlsrpc.client.timeout = 30000
//若忘记配dlsrpc.consul.server.address,则认为是本地调用,将使用本地服务地址
dlsrpc.server.address.default = "127.0.0.1:8080"
//请求id,一般从 0 开始自增
dlsrpc.client.request-start-value = 0
//HTTP 消息长度,TCP解码用
dlsrpc.http.message-length = 4
//默认是服务调用地址就是本地,需要抽出去为用户配置或改为缓存地址到本地
dlsrpc.server.address.default = "localhost:8080"
//(调用方会根据此地址建立连接)
//web服务默认端口
dlsrpc.server.port = 9998
//web服务默认端口,需要抽出去为用户配置
//需要与服务启动时的端口相同
dlsrpc.server.port = 8080
//默认WEB服务在本地,本不应该配置在此,但是本地测试都是在单机上,服务实际地址也是本地
//默认WEB服务在本地,需要抽出去为用户配置
dlsrpc.server.ip = "127.0.0.1"
//负载均衡默认权值,建议[1,10],不可小于0
//负载均衡默认权值
dlsrpc.server.balancer-weight = 5
//默认启用cglib,但是会根据是否有接口进一步确定
//默认启用cglib
dlsrpc.proxy.mode.cglib-proxy = true
//是否强制启用CGLIB,若是final修饰的类或方法将抛出代理异常
//是否强制
dlsrpc.proxy.mode.force-cglib-proxy = false
```
35 changes: 0 additions & 35 deletions dls-benchmark/src/main/resources/dlsRpc.conf

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 8 additions & 1 deletion dlsRpc-common/src/main/resources/dlsRpc.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//默这是默认的
//这是默认的
//使用此RPC的项目需要提供dlsRpc.conf文件,否则使用默认配置。
//调用Configuration.config(filePath),可以使用其他config文件夹下的配置文件

//consul ip,需要抽出去为用户配置
dlsrpc.consul.host = "127.0.0.1"

Expand All @@ -8,6 +11,9 @@ dlsrpc.consul.port = 8500
//consul 服务健康检查间隔时间 1秒
dlsrpc.consul.interval = 1s

//默认开启服务注册
dlsrpc.consul.enable = true

//服务器端所有可用ip,用于负载均衡,需要抽出去为用户配置
//此配置不能完全决定服务地址的选取,但是在此配置的ip将被负载均衡优先选择
dlsrpc.consul.server.address = ["127.0.0.1"]
Expand All @@ -34,6 +40,7 @@ dlsrpc.http.message-length = 4
dlsrpc.server.address.default = "localhost:8080"

//web服务默认端口,需要抽出去为用户配置
//需要与服务启动时的端口相同
dlsrpc.server.port = 8080

//默认WEB服务在本地,需要抽出去为用户配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,72 @@ import java.util
import java.util.{List => JList}

import com.typesafe.config.{Config, ConfigFactory}
import io.growing.dlsrpc.common.exception.RpcException

import scala.util.Try

/**
* 加载配置
*
* TODO 后续应当检测,若有配置文件则应该覆盖dlsRpc.conf
* TODO 后续应当检测,若有配置文件则应该覆盖dlsRpc.conf,Config组件默认支持
*
* @author 梦境迷离
* @version 1.0, 2019-06-13
* @version 1.1, 2019-06-13
*/
object Configuration {
//去掉 extends App 不然拿不到值

private final var config: Config = _

/**
* 若调用过该方法,则使用此配置
*
* @param userConfigName 配置必须在config目录下,若有子目录需要加/folder/userConfigName.conf
*/
def config(userConfigName: String) {
if (userConfigName == null || "".equals(userConfigName)) {
throw RpcException("Config name can't be null")
}
config = ConfigFactory.load(userConfigName)
}

if (config == null) {
config = ConfigFactory.load("dlsRpc.conf")
}
//TODO 路径处理
var config: Config = ConfigFactory.load("dlsRpc.conf")

//读取配置文件
final val CONSUL_ADDRESS_IP: String = Try(config.getString("dlsrpc.consul.host")).getOrElse(Constants.CONSUL_ADDRESS_IP)
final val CONSUL_ADDRESS_PORT: Int = Try(config.getInt("dlsrpc.consul.port")).getOrElse(Constants.CONSUL_ADDRESS_PORT)
final val TIME_AWAIT: Int = Try(config.getInt("dlsrpc.client.timeout")).getOrElse(Constants.TIME_AWAIT)
final val REQUEST_START_VALUE: Int = Try(config.getInt("dlsrpc.client.request-start-value")).getOrElse(Constants.REQUEST_START_VALUE)
final val MESSAGE_LENGTH: Int = Try(config.getInt(" dlsrpc.http.message-length")).getOrElse(Constants.MESSAGE_LENGTH)
final val PATTERN: String = Try(config.getString("dlsrpc.consul.adress-pattern")).getOrElse(Constants.PATTERN)
final val IP_PATTERN: String = Try(config.getString("dlsrpc.consul.ip-pattern")).getOrElse(Constants.IP_PATTERN)
final val PACKAGE_SERVICE: String = Try(config.getString("dlsrpc.consul.registry.package-service")).getOrElse(Constants.PACKAGE_SERVICE)
final val DEFAULT_DISCOVER_ADDRESS: String = Try(config.getString("dlsrpc.server.address.default")).getOrElse(Constants.DEFAULT_DISCOVER_ADDRESS)
final val CGLIB_PROXY: Boolean = Try(config.getBoolean("dlsrpc.proxy.mode.cglib-proxy")).getOrElse(Constants.CGLIB_PROXY)
final val TO_CGLIB_PROXY: Boolean = Try(config.getBoolean("dlsrpc.proxy.mode.force-cglib-proxy")).getOrElse(Constants.TO_CGLIB_PROXY)
final val WEB_SERVER_PORT: Int = Try(config.getInt("dlsrpc.server.port")).getOrElse(Constants.WEB_SERVER_PORT)
final val WEB_SERVER_IP: String = Try(config.getString("dlsrpc.server.ip")).getOrElse(Constants.WEB_SERVER_IP)
final val DEFAULT_WEIGHT: Int = Try(config.getInt("dlsrpc.server.balancer-weight")).getOrElse(Constants.DEFAULT_WEIGHT)
final val CONSUL_INTERVAL: String = Try(config.getString("dlsrpc.consul.interval")).getOrElse(Constants.CONSUL_INTERVAL)
final val CONSUL_ADDRESS_IP: String = Try(config.getString("dlsrpc.consul.host")).getOrElse(DefaultConstants.CONSUL_ADDRESS_IP)
final val CONSUL_ADDRESS_PORT: Int = Try(config.getInt("dlsrpc.consul.port")).getOrElse(DefaultConstants.CONSUL_ADDRESS_PORT)
final val CONSUL_ENABLE: Boolean = Try(config.getBoolean("dlsrpc.consul.enable")).getOrElse(DefaultConstants.CONSUL_ENABLE)
final val TIME_WAIT: Int = Try(config.getInt("dlsrpc.client.timeout")).getOrElse(DefaultConstants.TIME_WAIT)
final val REQUEST_START_VALUE: Int = Try(config.getInt("dlsrpc.client.request-start-value")).getOrElse(DefaultConstants.REQUEST_START_VALUE)
final val MESSAGE_LENGTH: Int = Try(config.getInt(" dlsrpc.http.message-length")).getOrElse(DefaultConstants.MESSAGE_LENGTH)
final val PATTERN: String = Try(config.getString("dlsrpc.consul.adress-pattern")).getOrElse(DefaultConstants.PATTERN)
final val IP_PATTERN: String = Try(config.getString("dlsrpc.consul.ip-pattern")).getOrElse(DefaultConstants.IP_PATTERN)
final val PACKAGE_SERVICE: String = Try(config.getString("dlsrpc.consul.registry.package-service")).getOrElse(DefaultConstants.PACKAGE_SERVICE)
final val DEFAULT_DISCOVER_ADDRESS: String = Try(config.getString("dlsrpc.server.address.default")).getOrElse(DefaultConstants.DEFAULT_DISCOVER_ADDRESS)
final val CGLIB_PROXY: Boolean = Try(config.getBoolean("dlsrpc.proxy.mode.cglib-proxy")).getOrElse(DefaultConstants.CGLIB_PROXY)
final val FORCE_CGLIB_PROXY: Boolean = Try(config.getBoolean("dlsrpc.proxy.mode.force-cglib-proxy")).getOrElse(DefaultConstants.FORCE_CGLIB_PROXY)
final val WEB_SERVER_PORT: Int = Try(config.getInt("dlsrpc.server.port")).getOrElse(DefaultConstants.WEB_SERVER_PORT)
final val WEB_SERVER_IP: String = Try(config.getString("dlsrpc.server.ip")).getOrElse(DefaultConstants.WEB_SERVER_IP)
final val DEFAULT_WEIGHT: Int = Try(config.getInt("dlsrpc.server.balancer-weight")).getOrElse(DefaultConstants.DEFAULT_WEIGHT)
final val CONSUL_INTERVAL: String = Try(config.getString("dlsrpc.consul.interval")).getOrElse(DefaultConstants.CONSUL_INTERVAL)

private final lazy val default: JList[String] = new util.ArrayList[String]()
default.add(DEFAULT_DISCOVER_ADDRESS)

final val SERVICE_IP_LIST: JList[String] = Try(config.getStringList("dlsrpc.consul.server.address")).getOrElse(default)

//默认配置,外界不可访问
private object Constants {
private object DefaultConstants {

final val CONSUL_INTERVAL = "1s"

final val DEFAULT_WEIGHT = 5

//客户端超时时间
final val TIME_AWAIT = 30 * 1000
final val TIME_WAIT = 30 * 1000

//请求id,一般从 0 开始自增
final val REQUEST_START_VALUE = 0
Expand All @@ -65,26 +83,29 @@ object Configuration {
//默认注册的包下的service
final val PACKAGE_SERVICE = "io.growing.dlsrpc"

//服务发现与注册中心,默认ip
//服务发现与注册中心,默认ip是本地consul
final val CONSUL_ADDRESS_IP = "127.0.0.1"

//默认端口
//consul默认端口
final val CONSUL_ADDRESS_PORT = 8500

//默认是服务调用地址就是本地,暂时不用
//是否开启服务注册
final val CONSUL_ENABLE = true

//默认是服务调用地址就是本地,当不开启consul时,使用此地址作为服务提供者的暴露地址
final val DEFAULT_DISCOVER_ADDRESS = "localhost:8080"

//默认服务调用端口
//服务提供者暴露的端口
final val WEB_SERVER_PORT = 8080

//默认服务都在本机,使用本地,且暴露8080端口
//服务提供者所在的地址
final val WEB_SERVER_IP = "127.0.0.1"

//默认启用cglib
final val CGLIB_PROXY = true

//是否强制
final val TO_CGLIB_PROXY = false
final val FORCE_CGLIB_PROXY = false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ case class NormalServiceAddress(ip: String, port: Int) extends ServiceAddress(ip
override def getPort: Int = port

override def toString: String = s"$ip:$port"

/**
* 直接将tcp参数映射为可用的服务端调用地址
*
* @param tcp localhost:8080
*/
def this(tcp: String) = {
this(tcp.split(":")(0), tcp.split(":")(1).toInt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.growing.dlsrpc.common.exception.RpcException
* @author 梦境迷离
* @version 1.0, 2019-06-07
*/
object IsCondition extends LazyLogging {
object CheckCondition extends LazyLogging {

/**
* 满足判断条件就打印出警告
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import java.util.{List => JList}
* @author 梦境迷离
* @version 1.0, 2019-06-18
*/
object SubClassUtils extends App {
@deprecated
object SubClassUtils {

/**
* 获取接口的实现类的类名
Expand All @@ -21,9 +22,9 @@ object SubClassUtils extends App {
*/
def getSubClassName[T](clazz: Class[T]): String = {
val ret: JList[Class[_]] = ClassUtil.getSubClassByInterface(clazz)
IsCondition.conditionException(ret == null, "this interface don't have any subClass")
CheckCondition.conditionException(ret == null, "this interface don't have any subClass")
//TODO 多类通过显示指定类名
IsCondition.conditionException(ret.size() > 1, "this interface have multiple implementation classes")
CheckCondition.conditionException(ret.size() > 1, "this interface have multiple implementation classes")
ret.get(0).getSimpleName
}

Expand All @@ -36,9 +37,9 @@ object SubClassUtils extends App {
*/
def getSubClass[T](clazz: Class[T]) = {
val ret: JList[Class[_]] = ClassUtil.getSubClassByInterface(clazz)
IsCondition.conditionException(ret == null || ret.size() == 0, "this interface don't have any subClass")
CheckCondition.conditionException(ret == null || ret.size() == 0, "this interface don't have any subClass")
//TODO 多类通过显示指定类名
IsCondition.conditionException(ret.size() > 1, "this interface have multiple implementation classes")
CheckCondition.conditionException(ret.size() > 1, "this interface have multiple implementation classes")
ret.get(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object SuperClassUtils {
* @tparam B
* @return
*/
def CheckSuperInterfaces[S, B](clazz: Class[S], interface: Class[B]): Class[_] = {
def checkSuperInterfaces[S, B](clazz: Class[S], interface: Class[B]): Class[_] = {
val names = clazz.getInterfaces
if (names.contains(interface)) {
clazz
Expand Down Expand Up @@ -85,7 +85,7 @@ object SuperClassUtils {
//不是接口,有实现接口,使用jdk;是接口使用jdk
if (clazz.isInterface || !clazz.isInterface && (getVaildSuperInterface[T](clazz) != null && getVaildSuperInterface[T](clazz).nonEmpty)) {
if (!clazz.isInterface) {
if (CGLIB_PROXY && TO_CGLIB_PROXY) {
if (CGLIB_PROXY && FORCE_CGLIB_PROXY) {
return ProxyType.CGLIB
}
} else {
Expand Down
Loading

0 comments on commit b1e41bb

Please sign in to comment.