SpringCloud Sidecar 整合.Net WebApi
在整合.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. 错误:
- 如果不设置sidecar异构系统的ip地址,也就是ip-address项,有可能读取不同网卡上(比如:虚拟机)的地址,导致请求发送不到目标系统上。
- 为目标系统创建health.json静态文件,并保证health-uri指定的地址可以被访问到,Eureka上状态为DOWN的服务是访问不了的。IIS服务器需要开启MiME设置。
- 查看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的数据,首次访问基本不成功,多刷新几次。
SpringCloud Sidecar 整合.Net WebApi的更多相关文章
- SpringCloud Alibaba整合Sentinel
SpringCloud Alibaba整合Sentinel Sentinel 控制台 1. 概述 Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理.监控(单机和集群),规则 ...
- spring cloud中利用sidecar整合异构语言(转)
用spring cloud sidecar的整合异构语言,以前做过没有做笔记,现在再做由于各种坑又浪费了一天,这里记一下 首先是官网:http://cloud.spring.io/spring-clo ...
- SpringCloud系列-整合Hystrix的两种方式
Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力.本文所说的Hystrix是Netflix开源的一款容错框架,同样具有自我保护能力. 本文目录 一.H ...
- 新版本SpringCloud sleuth整合zipkin
SpringCloud Sleuth 简介 Spring Cloud Sleuth为Spring Cloud实现了分布式跟踪解决方案. Spring Cloud Sleuth借鉴了Dapper的术语. ...
- 整合.NET WebAPI和 Vuejs——在.NET单体应用中使用 Vuejs 和 ElementUI
.NET简介 .NET 是一种用于构建多种应用的免费开源开发平台,例如: Web 应用.Web API 和微服务 云中的无服务器函数 云原生应用 移动应用 桌面应用 1). Windows WPF 2 ...
- SpringCloud Stream整合RabbitMQ3.5.0
前言 点击进入Spring官网文档 本文章为单体项目,将消费者和生产者写在同一个项目中,介意者不用向下看了. 本文介绍三种应用方式: 1:普通整合RabbitMQ 2:消息分区 3:按条件消费(多个消 ...
- SpringCloud之整合Zipkin+Sleuth(十四)
1.添加依赖 在项目的pom.xml文件中添加下面依赖 <!--里面包含两个依赖--> <dependency> <groupId>org.springframew ...
- springcloud gateway整合sentinel
1.引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- SpringCloud之整合Feign
假设提供者有如下服务接口方法 @RestController @RequestMapping("/person") public class PersonController { ...
随机推荐
- day7 python字符串的操作及方法
1.字符串 1.1 字符串的操作 # 1.字符串的拼接 strvar = "我爱" + "中国" # 2.字符串的重复 strvar = "今天下午2 ...
- 深入理解JVM(③)再谈线程安全
前言 我们在编写程序的时候,一般是有个顺序的,就是先实现再优化,并不是所有的牛P程序都是一次就写出来的,肯定都是不断的优化完善来持续实现的.因此我们在考虑实现高并发程序的时候,要先保证并发的正确性,然 ...
- swagger -- 前后端分离的API接口
文章目录 一.背景 二.swagger介绍 三.在maven+springboot项目中使用swagger 四.swagger在项目中的好处 五.美化界面 参考链接:5分钟学会swagger配置 参考 ...
- [spring] -- 事务篇
关于Transactional注解 五个表示隔离级别的常量 TransactionDefinition.ISOLATION_DEFAULT:使用后端数据库默认的隔离级别,Mysql 默认采用的 REP ...
- js JQ动态添加div标签
function renderList(data){ var str = ''; for(var i = 0; i < data.length; i++){ // 动态添加li str += ' ...
- js异步执行原理
我们都知道js是一个单线程的语言,所以没办法同时执行俩个进程.所以我们就会用到异步. 异步的形式有哪些那,es5的回调函数.es6的promis等 异步的运行原理我们可以先看下面这段代码 应该很多人都 ...
- 阿里云OSS服务器的使用
关于文件上传,我们一般使用OSS服务器.大致为两种上传方式: 详情官网参考:https://help.aliyun.com/document_detail/31927.html?spm=a2c4g.1 ...
- python中的and与or
一.问题起源: main=None main=main or sys.modules["__main__"].main main返回的是后面一个值,即 sys.modules[&q ...
- Win10下ImageMagick及php-imageck扩展的安装
安装ImageMagick https://imagemagick.org/script/download.php 选择符合自己电脑的版本进行安装即可.安装的时候注意勾选下面的选项自动加入环境变量,否 ...
- 利用Python的装饰器一键开启多线程
记录一下自己写的烂代码 import time import threading def WithThread(obj): """这是一个开启线程的装饰器"&q ...