我正在尝试使用 Java-13 和 Maven 创建 Java 模块项目。我的pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wtf.g4s8</groupId>
<artifactId>oot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<junit-platform.version>5.3.1</junit-platform.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>13</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<target>${jdk.version}</target>
<source>${jdk.version}</source>
<release>${jdk.version}</release>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
版本:
$ mvn help:system | grep -i jdk
sun.boot.library.path=/opt/jdk-13.0.1/lib
jdk.debug=release
java.home=/opt/jdk-13.0.1
java.runtime.name=OpenJDK Runtime Environment …Run Code Online (Sandbox Code Playgroud) 我正在通过docker-compose远程上下文部署一些 Docker 服务。我将其配置为使用 SSH:
docker context create remote --docker "host=ssh://user@my.remote.host"
docker context use remote
Run Code Online (Sandbox Code Playgroud)
在远程主机上,我有多个配置文件,我想将它们挂载到 Docker 中。当我尝试使用 CLI 时,它工作正常docker:
docker run -v /home/user/run:/test -it alpine:3.11
# ls -la /test
-> shows remote files correctly here
Run Code Online (Sandbox Code Playgroud)
docker-compose但是当我使用配置文件启动它时:
docker run -v /home/user/run:/test -it alpine:3.11
# ls -la /test
-> shows remote files correctly here
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它尝试挂载本地文件而不是远程文件,但失败并出现错误:
ERROR: for nginx Cannot start service nginx: OCI runtime create failed: container_linux.go:296: starting container process caused "process_linux.go:398: container init caused \"rootfs_linux.go:58: mounting …Run Code Online (Sandbox Code Playgroud) 我有一个yaml文件,其中一个字段可以由一种可能的结构表示。为了简化代码和 yaml 文件,假设我有这些 yaml 文件:
kind: "foo"
spec:
fooVal: 4
Run Code Online (Sandbox Code Playgroud)
kind: "bar"
spec:
barVal: 5
Run Code Online (Sandbox Code Playgroud)
这些用于解析的结构:
type Spec struct {
Kind string `yaml:"kind"`
Spec interface{} `yaml:"spec"`
}
type Foo struct {
FooVal int `yaml:"fooVal"`
}
type Bar struct {
BarVal int `yaml:"barVal"`
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以map[string]interface{}用作一种Spec字段。但是真实的例子更复杂,并且涉及更多可能的结构类型,不仅仅是Fooand Bar,这就是为什么我不喜欢解析spec到字段中。
我找到了一种解决方法:将 yaml 解组为中间结构,然后检查kind字段,并将map[string]interface{}字段编组为 yaml,并将其解组为具体类型:
var spec Spec
if err := yaml.Unmarshal([]byte(src), &spec); err != nil {
panic(err)
}
tmp, _ …Run Code Online (Sandbox Code Playgroud) 我正在使用 JDK 11 附带的新 HttpClient 发出许多请求(向 Github 的 API,但我认为这无关紧要),尤其是 GET。
对于每个请求,我构建并使用一个 HttpClient,如下所示:
final ExecutorService executor = Executors.newSingleThreadExecutor();
final HttpClient client = client = HttpClient
.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(10))
.executor(executor)
.build();
try {
//send request and return parsed response;
} finally {
//manually close the specified executor because HttpClient doesn't implement Closeable,
//so I'm not sure when it will release resources.
executor.shutdownNow();
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,除了时不时地,我收到以下异常,并且请求将不再有效,直到我重新启动应用程序:
Caused by: java.net.ConnectException: Cannot assign requested address
...
Caused by: java.net.BindException: Cannot assign requested address
at java.base/sun.nio.ch.Net.connect0(Native Method) …Run Code Online (Sandbox Code Playgroud) 我正在使用持久卷声明将数据存储在容器中:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc
labels:
type: amazonEBS
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Run Code Online (Sandbox Code Playgroud)
规范中的声明:
spec:
volumes:
- name: test-data-vol
persistentVolumeClaim:
claimName: test-pvc
containers:
- name: test
image: my.docker.registry/test:1.0
volumeMounts:
- mountPath: /var/data
name: test-data-vol
Run Code Online (Sandbox Code Playgroud)
当我第一次启动它时,这个卷安装正确。但是当我尝试更新容器映像时:
- image: my.docker.registry/test:1.0
+ image: my.docker.registry/test:1.1
Run Code Online (Sandbox Code Playgroud)
此卷无法挂载到新 pod:
# kubectl get pods
test-7655b79cb6-cgn5r 0/1 ContainerCreating 0 3m
test-bf6498559-42vvb 1/1 Running 0 11m
# kubectl describe test-7655b79cb6-cgn5r
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled …Run Code Online (Sandbox Code Playgroud) 我知道我可以使用手动向每个 HTTP 请求添加标头
cli := &http.Client{}
req, err := http.NewRequest("GET", "https://myhost", nil)
req.Header.Add("X-Test", "true")
if err != nil {
panic(err)
}
rsp, err := cli.Do(req)
Run Code Online (Sandbox Code Playgroud)
但我想为我的应用程序中的每个 HTTP 请求自动添加此标头。
最好的方法是什么?
我刚刚学习 Rust,所以也许我没有正确理解一些概念。
我有一些实现的特点:
trait Abstract {
fn name(&self) -> &str;
}
struct Foo {}
struct Bar {}
struct Baz {}
impl Abstract for Foo {
fn name(&self) -> &str { "foo" }
}
impl Abstract for Bar {
fn name(&self) -> &str { "bar" }
}
impl Abstract for Baz {
fn name(&self) -> &str { "baz" }
}
Run Code Online (Sandbox Code Playgroud)
我想向此特征添加一个静态方法,以按名称创建一些标准实现:
trait Abstract {
fn new(name: &str) -> Self {
match name {
"foo" => Foo{},
"bar" => Bar{},
"baz" => …Run Code Online (Sandbox Code Playgroud) I have an input string for my Golang CLI tool with some references to environment variables in bash syntax ($VAR and ${VAR}), e.g.:
$HOME/somedir/${SOME_VARIABLE}dir/anotherdir-${ANOTHER_VARIABLE}
Run Code Online (Sandbox Code Playgroud)
What is the most efficient way to interpolate this string by replacing environemnt variables references with actual values? For previous example it can be:
/home/user/somedir/4dir/anotherdir-5
Run Code Online (Sandbox Code Playgroud)
if HOME=/home/user, SOME_VARIABLE=4 and ANOTHER_VARIABLE=5.
Right now I'm using something like:
func interpolate(str string) string {
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", …Run Code Online (Sandbox Code Playgroud) 我是 JS 基础设施的新手,我正在尝试理解npm模块。我正在使用npm几个类创建 JavaScript 模块。我们这样称呼它Foo,Bar例如,Foo类位于./src/foo.js文件中,Bar类位于./src/bar.js:
// ./src/foo.js
export default class Foo {}
Run Code Online (Sandbox Code Playgroud)
和
// ./src/bar.js
export default class Bar {}
Run Code Online (Sandbox Code Playgroud)
另外,我有./src/index.js我想要导出Foo并Bar使其可以从其他模块访问的位置:
import Foo from './foo.js';
import Bar from './bar.js';
Run Code Online (Sandbox Code Playgroud)
我想要的是将我的模块命名foobar为例如,并将其发布到,然后使用from other module 并 import和from modulenpmjs安装它:npm install --save foobarFooBarfoobar
import {Foo, Bar} from 'foobar';
var foo = new Foo();
var bar …Run Code Online (Sandbox Code Playgroud) 我在 python 中有一个巨大的列表(~1_800_000 个项目),它是使用map()来自大约 1000 个 JSON 文件的函数构建的。我想检查几个第一项以确保脚本正常工作。我是这样做的:
items = map(lambda file: load_json(file), file_list)
print(list(items)[:5])
Run Code Online (Sandbox Code Playgroud)
将地图转换为列表大约需要 5-10 秒,是否可以在不将map结果转换为的情况下取几个第一项list?
go ×3
docker ×1
go-http ×1
http ×1
http-headers ×1
java ×1
java-11 ×1
java-13 ×1
javascript ×1
kubectl ×1
kubernetes ×1
list ×1
marshalling ×1
maven ×1
npm ×1
openjdk-11 ×1
parsing ×1
persistence ×1
python ×1
rust ×1
ssh ×1
volumes ×1
yaml ×1