SpringCloud Sidecar 整合.Net WebApi
整合过程中遇到不少问题,一般网上的例子只是调用一个简单的NodeJS示例,并未有详细的介绍及采坑过程。
首先,我的项目结构是:Vue前端 + SpringCloud后端 + .Net的WebApi后端
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。项目改造目标:Vue前端访问后端的网关Zuul,由Zuul配置请求转发,是由SpringCloud的服务提供响应,还是转发到.Net的服务上。
要准备的SpringCloud工程有三个:Eureka服务端、Zuul网关服务、Sidecar的异构系统服务
1、Eureka就不多废话了
2、Zuul为了调试方便,加上跨域访问的配置、并配置地址映射:
@Configuration public class CorsConfig { @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedMethod("*"); source.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(source); } }
application.yml
zuul:
ignoredServices: '*'
routes:
oldsys:
path: /api/**
serviceId: oldsys
- 此处有一个跨域访问的坑:
浏览器会返回200但是得不到数据,并抛出异常: The 'Access-Control-Allow-Origin' header contains multiple values but only one
意思就是不能处理多个跨域的返回值,首先Zuul的跨域不能关闭,否则不能通过浏览器正常访问,那么到.Net的代码里关闭跨域配置:
//config.EnableCors(new EnableCorsAttribute(WebSettingsConfig.CorsIp, "*", "*") { SupportsCredentials = true });
3、sidecar服务
启动程序,开启sidecar:
@SpringBootApplication @EnableSidecar public class CapaOldSysApp { public static void main(String[] args) { SpringApplication.run(CapaOldSysApp.class, args); } }
application.yml
sidecar:
ip-address: localhost
port: 17793
health-uri: http://localhost:17793/health.json
- 此处有很多坑:
- 如果不设置sidecar异构系统的ip地址,也就是ip-address项,有可能读取不同网卡上的地址,导致请求发送不到目标系统上。
- 为目标系统创建health.json静态文件,并保证可以被访问到。IIS服务器需要开启MiME设置。
- Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid. 错误,查看http://localhost:8080/hosts/oldsys
{
host: "localhost",
port: 17793,
metadata: {},
uri: "http://localhost:17793",
serviceId: "OLDSYS",
secure: false,
instanceInfo: {
instanceId: "192.168.161.1:8080",
app: "OLDSYS",
appGroupName: null,
ipAddr: "localhost",
sid: "na",
homePageUrl: "http://localhost:17793/",
statusPageUrl: "http://localhost:8080/info",
healthCheckUrl: "http://localhost:8080/health",
secureHealthCheckUrl: null,
vipAddress: "oldsys",
secureVipAddress: "oldsys",
countryId: 1,
dataCenterInfo: {
@class: "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
name: "MyOwn"
},
hostName: "localhost",
status: "UP",
leaseInfo: {
renewalIntervalInSecs: 30,
durationInSecs: 90,
registrationTimestamp: 1557805465685,
lastRenewalTimestamp: 1557805733435,
evictionTimestamp: 0,
serviceUpTimestamp: 1557805343392
},
isCoordinatingDiscoveryServer: false,
metadata: {},
lastUpdatedTimestamp: 1557805465686,
lastDirtyTimestamp: 1557805465682,
actionType: "ADDED",
asgName: null,
overriddenStatus: "UNKNOWN"
}
}
host为目标系统的ip地址,保证此ip加端口可以访问。更改.net程序的applicationhost.conf可以增加地址映射:
<bindings>
<binding protocol="http" bindingInformation="*:17793:localhost" />
<binding protocol="http" bindingInformation="*:17793:127.0.0.1" />
</bindings>
其他的就没有什么可以注意的了,Eureka应用列表服务状态不能为DOWN,有时候Zuul需要同步Eureka的数据,首次访问基本不成功,多刷新几次。
