在整合.Net的过程中遇到不少问题,一般网上的例子只是调用一个简单的NodeJS示例,并未有详细的介绍及采坑过程。

首先,我的项目结构是:Vue前端 + SpringCloud后端 + .Net的WebApi后端

项目改造目标:Vue前端访问后端的网关Zuul,由Zuul配置请求转发,给SpringCloud微服务或者.Net的服务上。

要准备的SpringCloud工程有三个:Eureka服务端、Zuul网关服务、Sidecar的异构系统服务

1、Eureka就不多废话了

2、Zuul为了调试方便,加上Cors跨域访问的配置:

@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);
}
}
  • 此处有一个跨域访问的坑:

浏览器会返回200但是得不到数据,并抛出异常: The 'Access-Control-Allow-Origin'  header contains multiple values  but only one

意思就是不能处理多个跨域的返回值,首先Zuul的跨域不能关闭,否则不能通过浏览器正常访问,那么到.Net的代码里关闭跨域配置:

 //config.EnableCors(new EnableCorsAttribute(WebSettingsConfig.CorsIp, "*", "*") { SupportsCredentials = true });

Zuul的application.yml

zuul:
ignoredServices: '*'
routes:
oldsys:
path: /api/**
serviceId: oldsys
strip-prefix: false
  • 此处一个请求转发的坑:

zuul转发给内部服务的时候会把Url的前缀剪掉,到达.net服务的Url变成了 http://localhost:17793/xxx

可以通过设置 strip-prefix: false 把Url还原成 http://localhost:17793/api/xxx  这样保留原来请求的完整请求路径。

顺便提一下原理,在SimpleRouteLocator.java类中,有这样一个方法就是此配置生效的原因:

     if (route.isStripPrefix()) {  //这里有个判断
int index = route.getPath().indexOf("*") - 1;
if (index > 0) {
String routePrefix = route.getPath().substring(0, index);
targetPath = targetPath.replaceFirst(routePrefix, "");
prefix = prefix + routePrefix;
}
}

3、sidecar服务

启动程序,开启sidecar:

@SpringBootApplication
@EnableSidecar
public class CapaOldSysApp {
public static void main(String[] args) {
SpringApplication.run(CapaOldSysApp.class, args);
}
}

Sidecar的application.yml

sidecar:
ip-address: localhost
port: 17793
health-uri: http://localhost:17793/health.json
  • 此处有很多坑,大多数是 Bad Request - Invalid Hostname  HTTP Error 400. The request hostname is invalid. 错误:
  1. 如果不设置sidecar异构系统的ip地址,也就是ip-address项,有可能读取不同网卡上(比如:虚拟机)的地址,导致请求发送不到目标系统上。
  2. 为目标系统创建health.json静态文件,并保证health-uri指定的地址可以被访问到,Eureka上状态为DOWN的服务是访问不了的。IIS服务器需要开启MiME设置。
  3. 查看http://localhost:8080/hosts/oldsys 的信息,确定host及port是不是目标系统的正确地址。
{
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"
}
}

IIS需要增加地址绑定,才可以用localhost以外的地址访问,更改.net程序的applicationhost.conf(调试环境在任务栏中找到IIS Express)可以增加地址映射:

<bindings>
    <binding protocol="http" bindingInformation="*:17793:localhost" />
    <binding protocol="http" bindingInformation="*:17793:127.0.0.1" />
</bindings>

其他的就没有什么可以注意的了,Eureka应用列表服务状态不能为DOWN,有时候Zuul需要同步Eureka的数据,首次访问基本不成功,多刷新几次。

最新文章

  1. 【重磅开源】Hawk-数据抓取工具:简明教程
  2. jQuery基本操作
  3. Go文件操作
  4. [LeetCode] Longest Palindromic Substring(manacher algorithm)
  5. flash 和 第三方程序交互
  6. (剑指Offer)面试题14:调整数组顺序使奇数位于偶数前面
  7. Afianl加载网络图片(延续)
  8. hdu 1028 母函数 一个数有几种相加方式
  9. HTC与英特尔联手打造无线VR解决方案
  10. linux下归档、解压缩工具:tar命令
  11. 【开发技术】Get请求和Post请求区别
  12. LR之error(一)
  13. 【心得】Lattice和Xilinx工具关键特性对比(Diamond、ISE)
  14. Android安全——加固原理
  15. 解析库之re,Beautifulsoup
  16. 为什么对string调用swap会导致迭代器失效
  17. Spring再接触 整合Hibernate
  18. CF101D Castle 树形DP、贪心
  19. linux系统切换用户
  20. nginx负载均衡二:配置

热门文章

  1. bzoj3367[Usaco2004 Feb]The Big Game 球赛*
  2. bzoj2134单选错位
  3. 第十章:Android消息机制
  4. OSCP Learning Notes - Enumeration(1)
  5. OSCP Learning Notes - Capstone(3)
  6. MapReduce之自定义分区器Partitioner
  7. BUUCTF-web NiZhuanSiWei
  8. sscanf,sprintf(思修课的收获)
  9. Fortify Audit Workbench 笔记 Path Manipulation
  10. 环境篇:DolphinScheduler-1.3.1安装部署及使用技巧