我在我的 Spring Boot 应用程序上使用 jmx 设置了 jolokia,所以我可以通过 HTTP 获取 jmx 信息。
但是,我无法通过 javascript ajax 获取数据,并出现错误“请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此,不允许访问Origin ' http://localhost:8080 '。”
我想让 jolokia 端点返回“Access-Control-Allow-Origin:*”标头以允许任何来源,或者至少返回具有我在 jolokia-access.xml 上指定的允许来源的标头,但我没有得到标头回应,我不确定我错过了什么
我正在运行的监控 javascript 不在具有我的 spring boot 应用程序的同一台服务器上,因为我想让脚本远程运行。
下面是我的 Spring Boot 应用程序设置。
application.properties 和 jolokia-access.xml 都在类路径中,所以我可以看到我对 application.properties 上的 management.port 所做的更改以及对 jolokia-access.xml 的远程发布访问限制已应用
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
:
应用程序属性
management.port=8888
endpoints.jolokia.path=/jolokia
endpoints.jolokia.enabled=true
endpoints.jmx.enabled=true
endpoints.cors.allowed-origins=*
endpoints.cors.allowed-methods=*
endpoints.cors.allowed-headers=*
jolokia-access.xml
<?xml version="1.0" encoding="UTF-8"?>
<restrict>
<remote>
<host>127.0.0.1</host>
<host>localhost</host>
</remote>
<http>
<method>post</method>
<method>get</method>
</http>
<commands>
<command>read</command>
<command>list</command>
<command>version</command>
</commands>
<cors>
<allow-origin>http://localhost:*</allow-origin>
<allow-origin>http://127.0.0.1:*</allow-origin>
</cors>
</restrict>
当我通过 curl 访问 jolokia 端点时,我可以将 jmx 数据作为 json 响应,如下所示
$ curl -X POST http://127.0.0.1:8888/jolokia -d '{
"type":"read",
"mbean":"org.springframework.boot:type=Endpoint,name=healthEndpoint",
"attribute":"Data"
}'
{"request":{"mbean":"org.springframework.boot:name=healthEndpoint,type=Endpoint","attribute":"Data","type":"read"},
"value":{"diskSpace":{"threshold":10485760,"free":204441415680,"status":"UP"},"db":{"database":"H2","hello":1,"status":"UP"},"status":"UP"},
"timestamp":1433908260,"status":200}
$ curl -X POST http://127.0.0.1:8888/jolokia -d '{
"type":"read",
"mbean":"java.lang:type=Memory",
"attribute":"HeapMemoryUsage",
"path":"used"
}'
{"request":{"path":"used","mbean":"java.lang:type=Memory","attribute":"HeapMemoryUsage","type":"read"},
"value":69036712,"timestamp":1434075946,"status":200}
但是,由于 jolokia 端点的响应没有 Access-Control-Allow-Origin 标头,因此 javascipt 下面失败了。即使我设置了 'endpoints.cors.allowed-origins=*' 属性,但我没有看到响应的标题
监控 javascript
var j4p = new Jolokia({url: "http://127.0.0.1:8888/jolokia", fetchInterval: 1000});
var context = cubism.context()
.serverDelay(0)
.clientDelay(0)
.step(1000)
.size(594);
var jolokia = context.jolokia(j4p);
var memory = jolokia.metric(
function (resp1, resp2) {
return Number(resp1.value) / Number(resp2.value);
},
{type:"read", mbean:"java.lang:type=Memory", attribute:"HeapMemoryUsage", path:"used"},
{type:"read", mbean:"java.lang:type=Memory", attribute:"HeapMemoryUsage", path:"max"}, "Heap-Memory"
);
发送的 javascript 请求标头
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:127.0.0.1:8888
Origin:http://127.0.0.1:8080
Referer:http://127.0.0.1:8080/monitor/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
收到的 javascript 响应标头
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Fri, 12 Jun 2015 02:27:17 GMT
Expires:0
Pragma:no-cache
Server:Jetty(9.2.10.v20150310)
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
正如您在上面看到的,根本没有“Access-Control-Allow-Origin”标头。