The first gateway component of the Martian framework (Martia-Gateway 1.0.0) is less featureless and currently only implements the following features:
- You can obtain the microservice interface from ZooKeeper to forward the request
- Request forwarding is carried out in load balancing mode
Specific use method
1. Introduce the following Maven coordinates
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>mars-gateway-starter</artifactId>
<version>Latest version, see "Components introduction"</version>
</dependency>
Copy the code
Create a config class that inherits MarsGateWayConfig
Override the methods in it to implement custom configurations, see the comments below
public class GateWayConfig extends MarsGateWayConfig {
public CloudConfig getGateWayConfig(a) {
CloudConfig cloudConfig = new CloudConfig();
// Service name
cloudConfig.setName("");
// Make it as long as possible to avoid too many interfaces
cloudConfig.setSessionTimeout(10000L);
// Request the timeout of the Mars-Cloud interface
cloudConfig.setTimeOut(10000L);
// ZooKeeper address. Multiple addresses are separated by commas
cloudConfig.setRegister("");
// Load balancing policy (currently only polling and random)
cloudConfig.setStrategy(Strategy.POLLING);
return cloudConfig;
}
/ * * * * * * * * the following methods have a default value, if the default values, can not rewrite the * * * * * * * * / * * * port number *@return* /
public int port(a){
return 8080;
}
/** * Thread pool configuration *@returnThread pool configuration */
public ThreadPoolConfig getThreadPoolConfig(a){
return new ThreadPoolConfig();
}
/** * Cross-domain configuration *@returnCross-domain configuration */
public CrossDomainConfig crossDomainConfig(a){
return newCrossDomainConfig(); }}Copy the code
Create a startup class
Call the start method in StartGateWay. Don’t be wrong
public class ExpStart {
public static void main(String[] args) {
StartGateWay.start(ExpStart.class, newGateWayConfig()); }}Copy the code
Complete the above three steps, a gateway is completed
How do you use it next
Very simple, generally make a request, divided into two scenarios:
- Expect the server to return data in JSON format (for most scenarios)
- Expect the server to return a file stream (file download, Excel export, etc.)
Returns data in JSON format
The request is as follows
http://IP: port number, or domain name/ router/ micro service to request name/ MarsApi method name on the micro service
Copy the code
Returns a file stream
The request is as follows
http://IP: port number, or domain name/ Download/Micro service to request name/ MarsApi method name on micro service
Copy the code
Matters needing attention
Because it is only the first version, so the function is not very perfect, so caused the following defects:
- The file stream returned does not have a suffix for the file name, so it needs to be renamed at the front
- Currently only request forwarding is implemented, but this forwarding is load-balanced
The project’s website
mars-framework.com