Skip to content

Commit

Permalink
feat(scaffold): support integration with skywalking
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Oct 11, 2019
1 parent 5958058 commit 5801e2e
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ $ yo deepexi-spring-cloud --help
|:-:|--|--|--|--|--|
|Java版本|✅jdk8|
|开发框架|✅springfox(swagger)|✅lombok|✅guava|✅common-lang3|
|测试框架|jacoco|☑️mockserver|☑️mockneat|
|测试框架|junit|✅mockito|✅assertj|☑️jacoco|☑️mockserver|☑️mockneat|
|开发相关|✅devtools|
|部署相关|✅docker|✅filebeat|
|其它|✅actuator|
Expand Down
27 changes: 27 additions & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ const obj = {
// },
// option: { desc: '日志框架', type: String, default: 'logback' }
// },
apm: {
prompting: {
type: 'list',
choices: [
'skywalking',
// 'pinpoint',
'none'
],
message: '请选择你使用的APM类型'
},
option: { desc: '缓存', type: String, default: 'skywalking' },
child: {
swVersion: {
prompting: { type: 'input', default: '6.4.0', message: '请填写你使用的skywalking版本' },
option: { desc: 'skywalking版本', type: String, default: '6.4.0' },
callbacks: {
trigger: [
new Trigger.AnyAnswerTrigger('apm', 'skywalking')
]
}
}
}
},
demo: {
prompting: {
type: 'confirm',
Expand Down Expand Up @@ -262,6 +285,10 @@ module.exports = require('yo-power-generator').getGenerator(obj, {
props.conditions[props.cache] = true;
}

if (props.apm !== 'none') {
props.conditions[props.apm] = true;
}

props.openfeign = props.discovery === 'eureka';
}
});
6 changes: 6 additions & 0 deletions generators/app/templates/.tmpl.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ FROM deepexi/java:v0.0.1

WORKDIR /home

<%
if (apm === 'skywalking') {
print(`COPY ./agent/skywalking /home/agent/skywalking`)
}
%>

COPY ./${artifactId}-provider/target/app.jar /home

ADD entrypoint.sh /
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Integrate Skywalking

## Download agent

要与skywalking集成,你需要使用java agent。

你可以通过执行以下命令下载skywalking的java agent

### linux/macos

```shell
curl -# -o agent.tar.gz http://mirrors.tuna.tsinghua.edu.cn/apache/skywalking/${swVersion}/apache-skywalking-apm-${swVersion}.tar.gz
mkdir -p agent
tar -zxvf agent.tar.gz
rm -rf agent/skywalking
mv apache-skywalking-apm-bin/agent agent/skywalking
rm agent.tar.gz
rm -rf apache-skywalking-apm-bin
```

### windows

<%
if (false) {
// print(`
//```shell
//curl -# -o agent.zip http://mirrors.tuna.tsinghua.edu.cn/apache/skywalking/${swVersion}/apache-skywalking-apm-${swVersion}.zip
//TODO::
//```
// `)
} else {
print(`暂不支持windows系统,请前往http://skywalking.apache.org/downloads/手动下载相关相应版本,并将压缩包agent目录下的内容放至项目agent/skywalking下`)
}
%>

## Run with IDE

请在IDE的启动VM参数中加入以下配置
```text
-javaagent:agent/skywalking/skywalking-agent.jar
-Dskywalking.agent.service_name=${artifactId}
-Dskywalking.collector.backend_service={sw_service_address}
```

## Run with *.jar

请参考[Start via process](quickly_start.md#start-via-process),并加入以下启动参数

```text
-javaagent:{project_home}/agent/skywalking/skywalking-agent.jar
-Dskywalking.agent.service_name=${artifactId}
-Dskywalking.collector.backend_service={sw_service_address}
```

## Run with Docker

请参考[Start via container](quickly_start.md#start-via-container),并通过以下环境变量控制skywalking相关配置

|**环境变量**|**描述**|**默认值**|
|--|--|--|
|SW_SERVICE_NAME|注册到skywalking时使用的服务名|${artifactId}|
|SW_SERVICE_ADDR|skywalking服务地址||

> 注意:以上环境变量应修改`run.sh`脚本,通过`-e`参数传入
5 changes: 5 additions & 0 deletions generators/app/templates/README.tmpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
## Guides

- [快速开始](/1.docs/guides/quickly_start.md)
<%
if(apm === 'skywalking'){
print(`- [接入SkyWalking](/1.docs/guides/integrate_skywalking.md)`);
}
%>
- [使用参考文档](/1.docs/guides/reference.md)
- [开发参考文档](/1.docs/guides/dev_reference.md)
12 changes: 11 additions & 1 deletion generators/app/templates/entrypoint.tmpl.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/bin/bash

java -jar app.jar $@
<%
if (apm === 'skywalking') {
print(`java \\
-javaagent:agent/skywalking/skywalking-agent.jar \\
-Dskywalking.agent.service_name=\${SW_SERVICE_NAME:-${artifactId}} \\
-Dskywalking.collector.backend_service=\${SW_SERVICE_ADDR} \\
-jar app.jar $@`)
} else {
print(`java -jar app.jar $@`)
}
%>
26 changes: 17 additions & 9 deletions generators/app/templates/run.tmpl.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#!/bin/bash

docker run \
--name $CONTAINER_NAME \
--restart=on-failure:5 \
--privileged \
-p $PORT:8080 \
-v $PWD/logs:/home/logs \
-d $IMG_NAME \
--spring.profiles.active=prod
<%
let script = `
docker run \\
--name $CONTAINER_NAME \\
--restart=on-failure:5 \\
--privileged \\`
if (apm === 'skywalking') {
script += `
-e SW_SERVICE_ADDR=127.0.0.1:11800 \\`
}
script += `
-p $PORT:8080 \\
-v $PWD/logs:/home/logs \\
-d $IMG_NAME \\
--spring.profiles.active=prod`
print(script);
%>
37 changes: 36 additions & 1 deletion generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ const expects = {
apollo: new Expect(),
jwtShiro: new Expect(),
thymeleaf: new Expect(),
redis: new Expect()
redis: new Expect(),
skywalking: new Expect()
};

const required = expects.required;
Expand Down Expand Up @@ -597,6 +598,27 @@ redis.assertProperties = () => {
});
}

const skywalking = expects.skywalking;
skywalking.addProjectFiles([
'1.docs/guides/integrate_skywalking.md'
])
skywalking.assertREADME = () => {
it('should contain content', () => {
assert.fileContent('README.md', /接入SkyWalking/)
assert.fileContent('entrypoint.sh', /javaagent/)
assert.fileContent('entrypoint.sh', /skywalking/)
assert.fileContent('run.sh', /-e SW_SERVICE_ADDR=/)
});
}
skywalking.assertNoREADME = () => {
it('should not contain content', () => {
assert.noFileContent('README.md', /接入SkyWalking/)
assert.noFileContent('entrypoint.sh', /javaagent/)
assert.noFileContent('entrypoint.sh', /skywalking/)
assert.noFileContent('run.sh', /-e SW_SERVICE_ADDR=/)
});
}

function assertByExpected (expected, expects) {
describe('required files or classes', () => {
for (const key in expects) {
Expand Down Expand Up @@ -775,4 +797,17 @@ describe('optional dependencies', () => {
assertByExpected(['required', 'demo', 'redis'], expects)
});
});

describe('apm', () => {
describe('skywalking', () => {
before(() => {
return generate({
apm: 'skywalking',
demo: true
})
});

assertByExpected(['required', 'demo', 'skywalking'], expects)
});
});
});

0 comments on commit 5801e2e

Please sign in to comment.