一、编译发布Asp.net core 应用 直接使用vs2019编译发布后,通过ftp上传到centos的 /www/ 目录下,不再赘述。 二、centos安装asp.net core runtime和nginx 1、安装asp.net core runtime# Copy
1 2 3 4 5 6 |
#注册 Microsoft 密钥。注册产品存储库。安装必需的依赖项。 sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm #安装 .NET Core 运行时 sudo yum install aspnetcore-runtime-3.1 |
2、安装nginx# 添加源:#
1 |
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm |
安装 nginx# Copy
1 |
$ sudo yum -y install nginx |
Nginx常用命令# Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 卸载 nginx $ sudo yum remove nginx # 设置开机启动 $ sudo systemctl enable nginx # 启动 nginx 服务 $ sudo service nginx start # 停止 nginx 服务 $ sudo service nginx stop # 重启 nginx 服务 $ sudo service nginx restart # 重新加载配置,一般是在修改过 nginx 配置文件时使用。 $ sudo service nginx reload #查看nginx版本 $ nginx -v |
3、使用nginx反向代理# 在 /etc/nginx/ 目录下新建AspnetCoreDemo.conf,内容如下 Copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
server { listen 80; server_name example.com; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } |
4、重新加载nginx配置#
1 2 3 4 |
# 验证配置文件的语法 $ sudo nginx -t # 强制 Nginx 选取更改。 $ sudo nginx -s reload |
三、添加Systemd守护 1、Systemd service内容如下# 路径 /etc/systemd/system/AspnetCoreDemo.service 新建文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[Unit] Description=AspnetCoreDemo running on Centos [Service] WorkingDirectory=/www ExecStart=/usr/bin/dotnet /www/AspnetCoreDemo.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=AspnetCoreDemo #User=www-data #Production:生产环境 Development:开发环境 Environment=ASPNETCORE_ENVIRONMENT=Development Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target |
2、Systemd基本操作命令#
1 2 3 4 5 6 7 8 9 10 |
#启用 systemctl enable AspnetCoreDemo.service #启动 systemctl start AspnetCoreDemo.service #状态 systemctl status AspnetCoreDemo.service #重启 systemctl restart AspnetCoreDemo.service #关闭 systemctl stop AspnetCoreDemo.service |
四、防火墙设置(不需要端口访问,可忽略此步)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1、开放端口 firewall-cmd --zone=public --add-port=5000/tcp --permanent # 开放5000端口 firewall-cmd --zone=public --remove-port=5000/tcp --permanent #关闭5000端口 firewall-cmd --reload # 配置立即生效 2、查看防火墙所有开放的端口 firewall-cmd --zone=public --list-ports 3.、关闭防火墙 如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估 systemctl stop firewalld.service 4、查看防火墙状态 firewall-cmd --state |
from:https://www.cnblogs.com/wxb8/p/12359521.html
View Details系统: 阿里云的默认 CentOS 7.5 镜像 项目环境:.Net Core 2.2 一、安装.Net Core的运行环境 第一步,如果是一台新的服务器,可以升级一下系统的基础软件。如果没有必要也可以跳过这一步。 执行命令
1 |
sudo yum update |
第二步,注册Microsoft签名密钥,每个机器只要注册一次就可以 注册命令
1 |
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm |
执行结果 第三步:安装.Net Core SDK ,这里根据项目环境,需要安装2.2版的。不同的开发环境选择对应的运行时版本即可 执行命令
1 |
sudo yum install dotnet-sdk-2.2 |
完成后,通过命令,可以看出.net core的版本
1 |
dotnet --version |
第四步:上传一个.Net Core的程序,进入到程序包所在目录 执行命令
1 |
dotnet HelloWrold.dll #项目的dll文件 |
到这里,就可以通过服务器IP加上程序设定的端口号(一般默认是5000)进行访问了。如果你的服务器是在云端,有安全防护,需要开启对应的端口访问权限 二、通过 nginx 进行转发 第一步、安装nginx 执行命令
1 |
sudo yum install nginx |
第二步,修改 nginx 的配置文件 执行命令
1 |
vim /etc/nginx/nginx.conf |
修改 location的值,修改的内容如下:
1 2 3 4 5 |
# 传递真实IP到后端 proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:5000; |
这个配置的意思是监听80端口,如果有人访问80端口就把请求转到5000端口,这里的5000端口就是你 .Net Core 程序的端口,默认为5000可以更改,如果有更改换掉即可。 第三步、重启 nginx 的服务。启动 .Net Core 的程序
1 2 3 4 5 6 7 8 |
#重启 nginx systemctl restart nginx #进行dotnet 项目目录 cd /home/www/hello/ #运行dotnet 项目 dotnet Helloworld.dll |
这个时候,我们就可以通过服务器的80端口,访问我们的 .Net Core 程序了。 第四步、配置多个.net core的转发 如果你的服务器的nginx需要转发多个正在运行的 .Net Core 项目,可以通过在nginx的 /etc/nginx/conf.d/ 目录下,为每个项目建立一个转发的规则文件
1 2 |
# 打开配置文件目录 cd /etc/nginx/conf.d/ |
这里的配置文件目录和文件格式 在 nginx.conf 中有指定。 进入目录后,新建一个文件
1 |
vim hello.conf #名称没有要求,可以和自己的项目名称一样,方便查找 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { listen 80; server_name hello.leodev.cn; index index.html; location / { # 传递真实IP到后端 proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:5000; } } |
[…]
View Details一、Dubbo是什么? Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC(一种远程调用) 分布式服务框架(SOA),致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。 二、为什么要用Dubbo? 因为是阿里开源项目,国内很多互联网公司都在用,已经经过很多线上考验。内部使用了 Netty、Zookeeper,保证了高性能高可用性。
1 2 3 |
1、使用Dubbo可以将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,可用于提高业务复用 灵活扩展,使前端应用能更快速的响应多变的市场需求。 2、分布式架构可以承受更大规模的并发流量。 |
三、Dubbo 和 Spring Cloud 有什么区别?
1 2 3 4 5 |
1、通信方式不同:Dubbo 使用的是 RPC 通信,而Spring Cloud 使用的是HTTP RESTFul 方式。 2、组成不一样: dubbo的服务注册中心为Zookeerper,服务监控中心为dubbo-monitor,无消息总线,服务跟踪、批量任务等组件; spring-cloud的服务注册中心为spring-cloud netflix enruka,服务监控中心为spring-boot admin,有消息总线,数据流、服务跟踪、批量任务等组件; 四、Dubbo需要 Web 容器吗? |
不需要,如果硬要用Web 容器,只会增加复杂性,也浪费资源。 五、Dubbo内置了哪几种服务容器?
1 2 3 4 |
三种服务容器: 1、Spring Container 2、Jetty Container 3、Log4j Container |
Dubbo 的服务容器只是一个简单的 Main 方法,并加载一个简单的 Spring 容器,用于暴露服务。 六、dubbo都支持什么协议,推荐用哪种?
1 2 3 4 5 |
1、dubbo://(推荐) 2、http:// 3、rest:// 4、redis:// 5、memcached:// |
七、Dubbo里面有哪几种节点角色?
1 2 3 4 5 |
1、provide:暴露服务的服务提供方 2、consumer:调用远程服务的服务消费方 3、registry:服务注册于发现的注册中心 4、monitor:统计服务调用次数和调用时间的监控中心 5、container:服务运行容器 |
八、dubbo服务注册与发现的流程图 dubbo服务注册与发现的流程图 九、Dubbo默认使用什么注册中心,还有别的选择吗? 推荐使用zookeeper作为注册中心,还有redis、multicast、simple注册中心。 十、Dubbo 核心的配置有哪些? Dubbo 核心的配置 十一、在 Provider 上可以配置的 Consumer 端的属性有哪些?
1 2 3 4 |
1、timeout:方法调用超时 2、retries:失败重试次数,默认重试 2 次 3、loadbalance:负载均衡算法,默认随机 4、actives 消费者端,最大并发调用限制 |
十二、Dubbo有哪几种负载均衡策略,默认是哪种?
1 2 3 4 |
1、random loadbalance:安权重设置随机概率(默认); 2、roundrobin loadbalance:轮寻,按照公约后权重设置轮训比例; 3、lastactive loadbalance:最少活跃调用数,若相同则随机; 4、consistenthash loadbalance:一致性hash,相同参数的请求总是发送到同一提供者。 |
十三、Dubbo启动时如果依赖的服务不可用会怎样? Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,默认 check="true",可以通过 check="false" 关闭检查。 十四、Dubbo推荐使用什么序列化框架,你知道的还有哪些?
1 |
推荐使用Hessian序列化,还有Duddo、FastJson、Java自带序列化; |
十五、Dubbo默认使用的是什么通信框架,还有别的选择吗?
1 |
Dubbo 默认使用 Netty 框架,也是推荐的选择,另外内容还集成有Mina、Grizzly。 |
十六、Dubbo有哪几种集群容错方案,默认是哪种? Dubbo集群容错方案 十七、服务提供者能实现失效踢出是什么原理?
1 |
服务失效踢出基于zookeeper的临时节点原理。 |
十八、Dubbo服务之间的调用是阻塞的吗?
1 2 3 |
默认是同步等待结果阻塞的,支持异步调用。 Dubbo 是基于 NIO 的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对 多线程开销较小,异步调用会返回一个 Future 对象。 |
Dubbo暂时不支持分布式事务。 十九、Dubbo的管理控制台能做什么?
1 2 |
管理控制台主要包含:路由规则,动态配置,服务降级,访问控制,权重调整,负载均衡,等管理功能。 注:dubbo源码中的dubbo-admin模块打成war包,发布运行即可得到dubbo控制管理界面。 |
二十、Dubbo 服务暴露的过程
1 2 3 4 |
Dubbo 会在 Spring 实例化完 bean 之后,在刷新容器最后一步发布 ContextRefreshEvent 事件的时候,通知 实现了 ApplicationListener 的 ServiceBean 类进行回调 onApplicationEvent 事件方法,Dubbo 会在这个方法 中调用 ServiceBean 父类 ServiceConfig 的 export 方法,而该方法真正实现了服务的(异步或者非异步)发 布。 |
二十一、当一个服务接口有多种实现时怎么做? 当一个接口有多种实现时,可以用 group 属性来分组,服务提供方和消费方都指定同一个 group […]
View Details直接上代码了。 Java控制台代码: package Test; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import org.apache.commons.codec.binary.Base64; public class Test { private static final String encoding = "UTF-8"; public static void main(String[] args) { try { String text = "20200121";// 明文 String key = "Tt3rLPrDIVIhXqAz";// 长度控制为16,作为3DES加密用的key String encryptStr = EncryptData(text, key);// 3DES加密结果 System.out.println("明文:" + text); System.out.println("密钥:" + key); System.out.println("密文:" + encryptStr); System.out.println("解密:" + DecryptData(encryptStr, key)); } catch (Exception e) { e.printStackTrace(); } } /** * DESede加密,key长度为16 * * @param plainText 明文 * @param key 密钥 * @return DESede加密结果 * […]
View Details使用注解可以更方便对参数进行验证,但是也会存在非必须参数如:https://aaa.com?id=1&name=&age=;或https://aaa.com?id=1&name&age的请求。这时ModelState.IsValid过滤器将会拦截请求提示"值是必需的。"或"有一个值是必需的,但请求中不存在该值。"异常。 若接口使用model接收参数,可将值类型参数改为可空类型解决此问题;如:
1 |
int?id |
若接口不使用model接收参数,暂无没有找到解决方案; 经过调试可以使用一种笨拙的取巧方案解决:在过滤器.ModelState.IsValid==false内部对值的错误内容进行排除
1 2 3 4 |
if (Msg != "有一个值是必需的,但请求中不存在该值。" && Msg != "值是必需的。") { return; } |
from:https://blog.csdn.net/niuc321/article/details/88694793
View Details导致该问题的原因在于没有配置curl.cainfo,该配置位于php.ini中。 解决方案: 1)下载cacert.pem https://curl.haxx.se/ca/cacert.pem 2)配置php.ini [curl] ; A default value for the CURLOPT_CAINFO option. This is required to be an ; absolute path. curl.cainfo = 【你的绝对路径】 ———————————————— 版权声明:本文为CSDN博主「loophome」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/loophome/java/article/details/83112364
View Details
1 2 3 4 5 6 7 8 9 10 11 12 |
/// <summary> /// get ip /// </summary> /// <returns></returns> public static string GetIp() { var context = HttpContext.Current; return context.Request.ServerVariables["HTTP_X_REAL_IP"] ?? context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? context.Request.ServerVariables["REMOTE_ADDR"] ?? "unknow"; } |
View Details
public HttpResponseMessage getHtml() { string uri = "http://docs.google.com/gview?embedded=true&url=www.pdf995.com/samples/pdf.pdf"; WebClient wc = new WebClient(); Stream resStream = wc.OpenRead(uri); StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default); string ContentHtml = sr.ReadToEnd(); var response = new HttpResponseMessage(); response.Content = new StringContent(ContentHtml); response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return response; } ———————————————— 版权声明:本文为CSDN博主「小咪蜂」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/xiaomifengmaidi1/java/article/details/84665109
View DetailsASP.NET Web API实现简单的文件下载与上传。首先创建一个ASP.NET Web API项目,然后在项目下创建FileRoot目录并在该目录下创建ReportTemplate.xlsx文件,用于下面示例的使用。 1、文件下载 示例:实现报表模板文件下载功能。 1.1 后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/// <summary> /// 下载文件 /// </summary> [HttpGet] public HttpResponseMessage DownloadFile() { string fileName = "报表模板.xlsx"; string filePath = HttpContext.Current.Server.MapPath("~/") + "FileRoot\\" + "ReportTemplate.xlsx"; FileStream stream = new FileStream(filePath, FileMode.Open); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlEncode(fileName) }; response.Headers.Add("Access-Control-Expose-Headers", "FileName"); response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName)); return response; } |
1.2 前端代码
1 |
<a href="http://localhost:51170/api/File/DownloadFile">下载模板</a> |
2、文件上传 示例:实现上传报表文件功能。 2.1 后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/// <summary> /// 上传文件 /// </summary> [HttpPost] public HttpResponseMessage UploadFile() { try { //获取参数信息 HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; HttpRequestBase request = context.Request; //定义传统request对象 string monthly = request.Form["Monthly"]; //获取请求参数:月度 string reportName = request.Form["ReportName"]; //获取请求参数:报表名称 //保存文件 string fileName = String.Format("{0}_{1}.xlsx", monthly, reportName); string filePath = HttpContext.Current.Server.MapPath("~/") + "FileRoot\\" + fileName; request.Files[0].SaveAs(filePath); //返回结果 var result = new HttpResponseMessage { Content = new StringContent("上传成功", Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } catch (Exception ex) { throw ex; }; } |
2.2 前端代码
1 2 3 4 5 6 |
<form action='http://localhost:51170/api/File/UploadFile' method="post" enctype="multipart/form-data"> 报表月份:<input type="text" name="Monthly" value="2018-11" /><br /> 报表名称:<input type="text" name="ReportName" value="财务报表" /><br /> 报表文件:<input type="file" name="file" /><br /> <input type="submit" value="提交" /> </form> |
from:https://blog.csdn.net/pan_junbiao/article/details/84065952
View Details由上可见,MIME_MapTable是所有文件的后缀名所对应的MIME类型的一个String数组: Java代码 final String[][] MIME_MapTable={ //{后缀名,MIME类型} {".3gp", "video/3gpp"}, {".apk", "application/vnd.android.package-archive"}, {".asf", "video/x-ms-asf"}, {".avi", "video/x-msvideo"}, {".bin", "application/octet-stream"}, {".bmp", "image/bmp"}, {".c", "text/plain"}, {".class", "application/octet-stream"}, {".conf", "text/plain"}, {".cpp", "text/plain"}, {".doc", "application/msword"}, {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, {".xls", "application/vnd.ms-excel"}, {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, {".exe", "application/octet-stream"}, {".gif", "image/gif"}, {".gtar", "application/x-gtar"}, {".gz", "application/x-gzip"}, {".h", "text/plain"}, {".htm", "text/html"}, {".html", "text/html"}, {".jar", "application/java-archive"}, {".java", "text/plain"}, {".jpeg", "image/jpeg"}, {".jpg", "image/jpeg"}, {".js", "application/x-javascript"}, {".log", "text/plain"}, {".m3u", "audio/x-mpegurl"}, {".m4a", "audio/mp4a-latm"}, {".m4b", "audio/mp4a-latm"}, {".m4p", "audio/mp4a-latm"}, {".m4u", "video/vnd.mpegurl"}, {".m4v", "video/x-m4v"}, {".mov", "video/quicktime"}, {".mp2", "audio/x-mpeg"}, {".mp3", "audio/x-mpeg"}, {".mp4", "video/mp4"}, {".mpc", "application/vnd.mpohun.certificate"}, {".mpe", "video/mpeg"}, {".mpeg", "video/mpeg"}, {".mpg", "video/mpeg"}, {".mpg4", "video/mp4"}, {".mpga", "audio/mpeg"}, {".msg", "application/vnd.ms-outlook"}, {".ogg", "audio/ogg"}, {".pdf", "application/pdf"}, {".png", "image/png"}, {".pps", "application/vnd.ms-powerpoint"}, {".ppt", "application/vnd.ms-powerpoint"}, {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, {".prop", "text/plain"}, {".rc", "text/plain"}, {".rmvb", "audio/x-pn-realaudio"}, {".rtf", "application/rtf"}, {".sh", "text/plain"}, {".tar", "application/x-tar"}, {".tgz", "application/x-compressed"}, {".txt", "text/plain"}, {".wav", "audio/x-wav"}, {".wma", "audio/x-ms-wma"}, {".wmv", "audio/x-ms-wmv"}, {".wps", "application/vnd.ms-works"}, {".xml", "text/plain"}, {".z", "application/x-compress"}, {".zip", "application/x-zip-compressed"}, {"", "*/*"} }; ———————————————— 版权声明:本文为CSDN博主「零下忆度」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/sinat_30474567/java/article/details/53411146
View Details