一、快速安装apollo 微服务配置中心 https://github.com/apolloconfig/apollo-build-scripts 下载 1.安装 jdk1.8+ 2.安装mysql 5.6.5+ 执行sql创建两个数据库 3.修改apollo配置连接 4.运行 使用bash打开(安装git后会带)
1 |
./demo.sh start |
访问 http://localhost:8070/ 登录 输入用户名apollo,密码admin 创建应用 新增配置 提交后 点击发布 二、asp.net core 连接apollo 1.添加引用
1 2 |
Microsoft.Extensions.Configuration Com.Ctrip.Framework.Apollo.Configuration |
2.创建webapi接口 Program.cs
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 |
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Com.Ctrip.Framework.Apollo; using Microsoft.AspNetCore; namespace WebApi_Apollo { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { //默认的application Namespace builder.AddApollo(builder.Build().GetSection("apollo")).AddDefault(); }) .UseStartup<Startup>(); } } |
Controller.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ApiController] [Route("[controller]")] public class ValuesController:ControllerBase { IConfiguration _configuration; public ValuesController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public string test() { var v = _configuration.GetValue<string>("time"); return v; } } |
appsettings.json 多添加一项
1 2 3 4 |
"apollo": { "AppId": "MES1", //这是我们上面添加的Apollo里面的Appid "MetaServer": "http://127.0.0.1:8080" //Apollo配置服务器地址,注意这里是8080,不是admin的8070 } |
二、集群搭建 1 下载 https://github.com/apolloconfig/apollo/releases apollo-portal:Web界面 端口8070 apollo-adminservice:提供配置管理接口,提供配置修改、发布等接口,接口服务对象为Portal,以及Eureka 端口 8090 apollo-configservice:提供配置获取接口,提供配置更新推送接口,接口服务对象为Apollo客户端 端口 8080 2 创建数据库 需要创建两个数据库 ApolloPortalDB和ApolloConfigDB https://github.com/apolloconfig/apollo/tree/master/scripts/sql ApolloPortalDB只需要在生产环境部署一个即可,而ApolloConfigDB需要在每个环境部署一套(如:生产一套,测试一套) ApolloPortalDB View Code ApolloConfigDB View Code 3 配置 apollo-configservice和apollo-adminservice
1 2 3 |
spring.datasource.url = jdbc:mysql://localhost:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8 spring.datasource.username = someuser spring.datasource.password = somepwd |
apollo-portal
1 2 3 |
spring.datasource.url = jdbc:mysql://localhost:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8 spring.datasource.username = someuser spring.datasource.password = somepwd |
4.安装git才能在windows上运行 .sh脚本
1 2 3 |
spring.datasource.url = jdbc:mysql://localhost:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8 spring.datasource.username = someuser spring.datasource.password = somepwd |
5
1 2 3 |
set global time_zone = '+8:00'; ##修改mysql全局时区为北京时间,即我们所在的东8区 set time_zone = '+8:00'; ##修改当前会话时区 flush privileges; #立即生效 |
启动apollo-configservice-1.9.2-github
1 |
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://l92.xxx.xxx.xxx:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=xxx -Dspring.datasource.password=xxx -Dsever.port=8080 -jar apollo-configservice-1.9.2.jar |
启动apollo-adminservice-1.9.2-github
1 |
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://192.xxx:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=xxx -Dspring.datasource.password=xxx -Dsever.port=8090 -jar apollo-adminservice-1.9.2.jar |
启动apollo-portal-1.9.2-github
1 |
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://192.xxx:3306/ApolloPortalDB?characterEncoding=utf8 -Dspring.datasource.username=xxx -Dspring.datasource.password=xxx -Dsever.port=8070 -jar apollo-portal-1.9.2.jar |
访问 http://127.0.0.1:8070/ from:https://www.cnblogs.com/buchizaodian/p/15474559.html
View DetailsJava Flight Recorder简称JFR,OpenJDK从11版本开始支持。它是一个低开销的数据收集框架,可用于在生产环境中分析Java应用和JVM运行状况及性能问题。
View Details第一步: 1、把module目录下的MATA-INF文件夹删除,如果没有MATA-INF文件夹则不用删除 2、Ctrl + Alt + Shift + S 打开 Project Structure 窗口 第二步: 新增Artifacts,操作如下图 第三步: 设置如下图: 1、选择要打包的module 2、选择Main-Class 3、JAR files from library选择第二项,这样可以把依赖的jar包分离出来 4、MANIFEST.MF的路径不要使用默认的src/main/java目录,改成module的目录 5、点击ok 第四步: 上一步(第三步)点击ok后看到如下结构无误,则再次点击ok 第五步: 构建JAR文件,操作如下图: 第六步: Ctrl + Alt + Shift + S 打开 Project Structure 窗口,查看输出目录,如下图: 在上图所示的Output Directory目录下即可看到项目的jar包及其依赖的其他jar包,如下: 第七步: 运行项目: 使用命令 java -jar demo3.jar 即可运行项目,如下图: from:https://www.cnblogs.com/oldpub-blog/p/13415821.html
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//准备序化列对象 XmlSerializer xs = new XmlSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); //设置序序化XML格式 XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; xws.OmitXmlDeclaration = true; xws.Encoding = Encoding.UTF8; XmlWriter xtw = XmlTextWriter.Create(ms, xws); //去掉要结点的 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 属性 XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces( new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "aa") }); xs.Serialize(xtw, obj,_namespaces); ms.Position = 0; xmlDoc = new XmlDocument(); xmlDoc.Load(ms); //给文档添加<?xml version="1.0" encoding="utf-8"?> XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.InsertBefore(xmlDecl, xmlDoc.DocumentElement); |
from:https://blog.csdn.net/a5251199/article/details/106546967
View Details
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 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace object2xml { public static class XmlExtension { public static string Serialize<T>(this T value) { if (value == null) return string.Empty; var xmlserializer = new XmlSerializer(typeof(T)); using (StringWriter stringWriter = new StringWriter()) { using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true })) { xmlserializer.Serialize(writer, value); return stringWriter.ToString(); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace object2xml { public class Persion { public string Name { get; set; public int Age { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace object2xml { class Program { static void Main(string[] args) { Persion p = new Persion(); p.Name = "张三"; p.Age = 18; var xml = p.Serialize<Persion>(); Console.WriteLine(xml); } } } |
from:https://blog.csdn.net/justgono/article/details/45887619
View Details去除命名空间:
1 2 3 4 5 6 7 8 |
//Create our own namespaces for the output XmlSerializerNamespaces ns = new XmlSerializerNamespaces (); //Add an empty namespace and empty value ns.Add ("", ""); //Create the serializer XmlSerializer slz = new XmlSerializer (someType); //Serialize the object with our own namespaces (notice the overload) slz.Serialize (myXmlTextWriter, someObject, ns); |
此外,在评论中还提到了去除开头的<?xml version="1.0" encoding="utf-8"?>的方法:
1 2 3 4 |
XmlWriterSettings settings = new XmlWriterSettings (); // Remove the <?xml version="1.0" encoding="utf-8"?> settings.OmitXmlDeclaration = true; XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings); |
另外,如果出现开头没有encoding="utf-8"时,应该使用:
1 2 3 |
XmlWriterSettings settings = new XmlWriterSettings (); settings.Encoding = Encoding.UTF8; XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings); |
参考文献: 1.在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi from:https://www.swack.cn/wiki/001559619644599d7563755fc544a19aebbb4eaf0dcb274000/0015889968933656748bbc3214e4f04a714c163b9227520000
View Details背景 工作中遇到一个需求,客户要求按照他们指定的入参和出参开发一套WebService接口。 出入参不能有丝毫的出入,以方便他们的业务系统调用。 目标已明确,我们看看入参和出参(为了保护客户的商业机密,出入参都是虚构的): 入参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<BusinessQuery> <pageIndex value="1"/> <pageSize value="20"/> <paramList> <slot> <name value="$businessId"/> <valueList> <value value="x3c3738a4946472da595c45eb781e46c"/> <value value="adff738a4946472da595c45eb781e46c"/> </valueList> </slot> <slot> <name value="$biState"/> <valueList> <value value="1"/> <value value="2"/> </valueList> </slot> </paramList> </BusinessQuery> |
出参(正常)
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 33 34 35 36 37 38 39 40 41 42 43 44 |
<BusinessQueryResponse> <business> <id>x3c3738a4946472da595c45eb781e46c</id> <name>智能策略1</name> <category> <code value="1"/> <name value="BI"/> </category> <extension> <name value="ext1"/> <value value="01"/> </extension> <extension> <name value="ext2"/> <value value="2022-04-15"/> </extension> <department> <code value="001"/> <name value="商务运营部1"/> </department> <addDate value="2022-04-15 18:10:10"/> </business> <business> <id>adff738a4946472da595c45eb781e46c</id> <name>智能策略2</name> <category> <code value="1"/> <name value="BI"/> </category> <extension> <name value="ext1"/> <value value="01"/> </extension> <extension> <name value="ext2"/> <value value="2022-04-15"/> </extension> <department> <code value="001"/> <name value="商务运营部2"/> </department> <addDate value="2022-04-15 18:10:10"/> </business> </BusinessQueryResponse> |
出参(错误)
1 2 3 4 5 |
<returnData> <funCode>500</funCode> <errorCode/> <detail>查询记录为空</detail> </returnData> |
出入参看上去都挺简单的,开工吧~ 项目创建 添加Web服务 项目框架 SoapUI获取的请求模板如图 去掉上一步的前缀 入参XML节点配置 出参根节点配置 出参列表节点名称配置 出参XML节点名称完整配置 最终效果:与客户要求入参出参完全一致 异常出参需要用HttpModule自定义输出 定义HttpModule 异常输出方法 异常测试,效果哪下图 忘了说,HttpModule要在web.config里注册一下 完整代码下载>>
View Details