Skip to content

Commit 911517e

Browse files
committed
fix(api): encapsulate routing logic in RouteService
1 parent 5efdf16 commit 911517e

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

scalable-ot-api/src/main/java/com/brotherjing/controller/DocController.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
import org.springframework.web.bind.annotation.ResponseBody;
1616
import org.springframework.web.bind.annotation.RestController;
1717

18-
import com.brotherjing.core.loadbalance.LoadBalancer;
1918
import com.brotherjing.core.loadbalance.ServerEntity;
2019
import com.brotherjing.core.service.DocService;
2120
import com.brotherjing.producer.OpSender;
2221
import com.brotherjing.proto.BaseProto;
23-
import com.brotherjing.service.DiscoveryService;
22+
import com.brotherjing.service.RouteService;
2423

2524
@Slf4j
2625
@RestController
@@ -34,10 +33,7 @@ public class DocController {
3433
private OpSender opSender;
3534

3635
@Autowired
37-
private DiscoveryService discoveryService;
38-
39-
@Autowired
40-
private LoadBalancer loadBalancer;
36+
private RouteService routeService;
4137

4238
@RequestMapping(value = "/create", method = RequestMethod.POST)
4339
@ResponseBody
@@ -85,8 +81,7 @@ void save(@PathVariable String docId, @RequestBody BaseProto.Command command) {
8581
*/
8682
@GetMapping(value = "/{docId}/channel")
8783
String getChannel(@PathVariable String docId) {
88-
List<ServerEntity> allServers = discoveryService.getAllServers();
89-
ServerEntity entity = loadBalancer.select(allServers, docId);
84+
ServerEntity entity = routeService.getRoute(docId);
9085
log.info("Select channel {} for docId {}", entity.toString(), docId);
9186
return entity.getServerAddress();
9287
}

scalable-ot-api/src/main/java/com/brotherjing/grpc/BroadcastServiceImpl.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212

1313
import com.brotherjing.Const;
1414
import com.brotherjing.api.Broadcast;
15-
import com.brotherjing.core.loadbalance.LoadBalancer;
1615
import com.brotherjing.core.loadbalance.ServerEntity;
1716
import com.brotherjing.proto.BaseProto;
1817
import com.brotherjing.proto.BroadcastServiceGrpc;
19-
import com.brotherjing.service.DiscoveryService;
18+
import com.brotherjing.service.RouteService;
2019
import com.google.protobuf.AbstractMessageLite;
2120

2221
@GrpcService
@@ -26,10 +25,7 @@ public class BroadcastServiceImpl extends BroadcastServiceGrpc.BroadcastServiceI
2625
private Broadcast broadcast;
2726

2827
@Autowired
29-
private DiscoveryService discoveryService;
30-
31-
@Autowired
32-
private LoadBalancer loadBalancer;
28+
private RouteService routeService;
3329

3430
@Override
3531
public void sendTo(BaseProto.SendRequest request, StreamObserver<BaseProto.BroadcastResponse> responseObserver) {
@@ -60,7 +56,7 @@ public void sendToAll(BaseProto.BroadcastRequest request,
6056
* Get broadcast server ip by docId, and put in RPC context.
6157
*/
6258
private void setBroadcastIP(String docId) {
63-
ServerEntity entity = loadBalancer.select(discoveryService.getAllServers(), docId);
59+
ServerEntity entity = routeService.getRoute(docId);
6460
RpcContext.getContext().set(Const.BROADCAST_ADDR_CONTEXT, entity);
6561
}
6662
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.brotherjing.service;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import com.brotherjing.core.loadbalance.LoadBalancer;
9+
import com.brotherjing.core.loadbalance.ServerEntity;
10+
11+
@Service
12+
public class RouteService {
13+
14+
@Autowired
15+
private DiscoveryService discoveryService;
16+
17+
@Autowired
18+
private LoadBalancer loadBalancer;
19+
20+
public ServerEntity getRoute(String docId) {
21+
List<ServerEntity> allServers = discoveryService.getAllServers();
22+
return loadBalancer.select(allServers, docId);
23+
}
24+
}

0 commit comments

Comments
 (0)