官方文档地址: https://dromara.org/zh-cn/docs/soul/user-http.html
http服务就是我们自己的项目。
1.引入 soul-examples/soul-examples-http 到soul.pom中
application.yml中添加自己的soul-adminuri
server: port: 8188 address: 0.0.0.0 soul: http: adminUrl: http://localhost:9095 port: 8188 contextPath: /http appName: http full: false
2.启动服务 SoulTestHttpApplication
3.http服务上带有注解
2021-01-15 17:00:44.180 INFO 2688 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8188,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false} 2021-01-15 17:00:47.011 INFO 2688 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/save","pathDesc":"Save order","rpcType":"http","host":"192.168.100.52","port":8188,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false} 2021-01-15 17:00:48.455 INFO 2688 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8188,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false} 2021-01-15 17:00:48.930 INFO 2688 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**/name","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8188,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false} 2021-01-15 17:00:49.294 INFO 2688 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/findById","pathDesc":"Find by id","rpcType":"http","host":"192.168.100.52","port":8188,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false}
后台的divide插件可以进行选择器,和规则的添加,进行流量的匹配筛选。
在divide插件中可以看到http服务已经被注册到网关中。
4.启动两个服务进行均衡负载
勾选 Allow parrallel run
修改 applicaion.yml
server: port: 8189 address: 0.0.0.0 soul: http: adminUrl: http://localhost:9095 port: 8189 contextPath: /http appName: http full: false
启动服务 SoulTestHttpApplication
可以看到 8189端口被注册到网关之中。
2021-01-15 18:56:12.795 INFO 14432 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/test/**","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8189,"ruleName":"/http/test/**","enabled":true,"registerMetaData":false} 2021-01-15 18:56:12.889 INFO 14432 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/save","pathDesc":"Save order","rpcType":"http","host":"192.168.100.52","port":8189,"ruleName":"/http/order/save","enabled":true,"registerMetaData":false} 2021-01-15 18:56:13.041 INFO 14432 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8189,"ruleName":"/http/order/path/**","enabled":true,"registerMetaData":false} 2021-01-15 18:56:13.193 INFO 14432 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/path/**/name","pathDesc":"","rpcType":"http","host":"192.168.100.52","port":8189,"ruleName":"/http/order/path/**/name","enabled":true,"registerMetaData":false} 2021-01-15 18:56:13.605 INFO 14432 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : http client register success: {"appName":"http","context":"/http","path":"/http/order/findById","pathDesc":"Find by id","rpcType":"http","host":"192.168.100.52","port":8189,"ruleName":"/http/order/findById","enabled":true,"registerMetaData":false} 2021-01-15 18:56:13.634 INFO 14432 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8189
网关通过appName作为服务的唯一标识
当我们打开编辑选择器的按钮,就能看到我们启动的两个http服务。
我们就可以通过机器的内存/CPU等硬件考量,设置两个服务器的权重。
服务注册这块的核心代码
org.dromara.soul.client.springmvc.init.SpringMvcClientBeanPostProcessor#postProcessAfterInitialization
public Object postProcessAfterInitialization(@NonNull final Object bean, @NonNull final String beanName) throws BeansException { if (soulSpringMvcConfig.isFull()) { return bean; } Controller controller = AnnotationUtils.findAnnotation(bean.getClass(), Controller.class); RestController restController = AnnotationUtils.findAnnotation(bean.getClass(), RestController.class); RequestMapping requestMapping = AnnotationUtils.findAnnotation(bean.getClass(), RequestMapping.class); if (controller != null || restController != null || requestMapping != null) { SoulSpringMvcClient clazzAnnotation = AnnotationUtils.findAnnotation(bean.getClass(), SoulSpringMvcClient.class); String prePath = ""; if (Objects.nonNull(clazzAnnotation)) { if (clazzAnnotation.path().indexOf("*") > 1) { String finalPrePath = prePath; executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(clazzAnnotation, finalPrePath), url, RpcTypeEnum.HTTP)); return bean; } prePath = clazzAnnotation.path(); } final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass()); for (Method method : methods) { SoulSpringMvcClient soulSpringMvcClient = AnnotationUtils.findAnnotation(method, SoulSpringMvcClient.class); if (Objects.nonNull(soulSpringMvcClient)) { String finalPrePath = prePath; executorService.execute(() -> RegisterUtils.doRegister(buildJsonParams(soulSpringMvcClient, finalPrePath), url, RpcTypeEnum.HTTP)); } } } return bean; }