SpringBoot实例:医院统一信息平台(api gatewa
2018-11-15 本文已影响592人
碧波之心
前面已经在平台中使用了spring cloud。每个小的服务中各自实现相关业务,提供API。这些服务的访问地址都可能不一样。这样给使用都造成困扰,而且服务器接口管理也复杂了。
api-gateway就是把这些api通过一个服务提供出去。在这个服务中代理其它服务的API。对于服务的使用都就像是访问一台服务器。
这里用spring zuul实现api-gateway。
创建项目
创建一个项目(服务),专门做api代理。服务名称huip-router。
项目结构
pom
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.biboheart</groupId>
<artifactId>huip</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>huip-router</artifactId>
<name>huip-router</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<maimClass>com.biboheart.huip.router.RouterApplication</maimClass>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
RouterApplication
package com.biboheart.huip.router;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class RouterApplication {
public static void main(String[] args) {
SpringApplication.run(RouterApplication.class, args);
}
}
为了支持跨域,增加一个Filter
package com.biboheart.huip.router.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "X-Auth-Token, x-csrf-token, X-XSRF-TOKEN, X-Token, Origin, X-Requested-With, Content-Type, Accept, Authorization, access_secret, access_token");
response.setHeader("Access-Control-Max-Age", "3600");
if(!request.getMethod().equals("OPTIONS")) {
chain.doFilter(req, res);
}
}
@Override
public void destroy() {}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
配置
server:
# 服务端口号
port: 8080
spring:
mvc:
favicon:
enabled: false
output:
ansi:
enabled: DETECT
http:
encoding:
charset: UTF-8
multipart:
maxFileSize: -1
maxRequestSize: 500MB
zuul:
addProxyHeaders: true
host:
socket-timeout-millis: 600000
connect-timeout-millis: 600000
routes:
########## 用户服务(user) ##########
user-service:
path: /huipuser/**
sensitiveHeaders:
serviceId: huip-user-server
stripPrefix: true
patient-service:
path: /huippatient/**
sensitiveHeaders:
serviceId: huip-patient-server
stripPrefix: true
hystrix:
command:
myusers-service:
execution:
timeout:
enabled: false
isolation:
thread:
timeoutInMilliseconds: 600000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
eureka:
enabled: true
# LOGGING
logging:
level:
root: info
在API请求开头为/huipuser/时访问的是user服务的API,如果开头/huippatient/时访问的是patient服务的API。比如请求http://localhost:8080/huippatient/loadUser相当于http://localhost:8280/loadUser
请求测试。
SpringBoot实例:医院统一信息平台(服务间通讯)的访问流程改成用代理。
代理访问
结果是一致的。
直接访问
代理访问
结果一致。
服务端API文档加上前缀就可以了。统一提供给API用户。