Service aggregation of API gateways
- Routes to different services through the same portal
Service aggregation implementation
- When a server is deployed in a cluster, the same service content has multiple service IP addresses. Therefore, you cannot directly access the service through the IP address
- When accessing the API service, obtain the IP address of the service before accessing the service
- The service aggregation project is called the service provider (implementing the service interface), and the SpringApplication of the service provider adds Main.main(args) to provide the remote invocation interface to the API gateway
- You can obtain the IP address of the service
- How do I obtain the service IP address?
Context information: The context stores the environment information required during the current call. All configuration information is converted to the PARAMETERS of the URL. The RpcContext is a temporary state logger of a ThreadLocal. The state of the RpcContext changes when an RPC request is received or initiated. For example, if A is tuned TO B and B is tuned to C, then on machine B, RpcContext records the information of A tuned to B before B tuned to C and RpcContext records the information service consumer of B tuned to C after B tuned to C: ------------------------------------------------------------------------------------------------------------------------ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / remote call xxxService. XXX (); Boolean isConsumerSide = rpcContext.getContext ().isConsumerSide(); String serverIP = rpcContext.getContext ().getremoteHost (); // Get the provider IP address of the last call. String application = rpcContext.getContext ().geturl ().getParameter("application"); // Note that the context state is yyyService.yyy() with each RPC call; ------------------------------------------------------------------------------------------------------------------------ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the service provider: ------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------------------------------------------- public class XxxServiceImpl Implements XxxService {public void XXX () {// Whether the local end is a provider, This returns true Boolean isProviderSide = rpcContext.getContext ().isProviderSide(); String clientIP = rpcContext.getContext ().getremoteHost (); // Get the caller's IP address. String application = rpcContext.getContext ().geturl ().getParameter("application"); // Note that the context state is yyyService.yyy() with each RPC call; False Boolean isProviderSide = rpcContext.getContext ().isProviderSide(); }} ------------------------------------------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------------Copy the code
- Configure the services and versions to be aggregated in the service of the configuration file
- Creating a Routing Controller
@Controller
public class RouterController{
@Value(value=${services.ports.user})
private String userPort;
@Value(value=${services.ports.content})
private String contentPort;
@Reference
private UserConsumerService userConsumerService;
@Reference
private ContentConsumerService contentConsumerService;
@RequestMapping(value="user",method=RequestMethod.GET)
public String user(String path){
userConsumerService.info();
return getRequest(userPort,path);
}
@RequestMapping(value="content",method=RequestMethod.GET)
public String content(String path){
contentConsumerService.info();
return getRequest(contentPort,path);
}
public String getRouter(String serverPort,String path){
// Determine if you are a consumer
boolean isConsumerSide=RpcContext.getContext().isConsumerSide();
if(isConsumerSide){
// Get the IP address of the provider of the last call
String serverIP=RpcContext.getContext().getRemoteHost()
return String.format("redirect:http://%s:%s%s",serverIP,serverPort,path);
}
return null; }}Copy the code