我在我的spring引导应用程序上使用jmx安装了jolokia,这样我就可以通过HTTP获得jmx信息。
但是,我无法通过javascript获得数据,错误是“没有‘访问-控制-允许-原产地’标题存在于请求的资源上,因此不允许访问http://localhost:8080。”
我想让jolokia端点返回‘访问-控制-允许-起源:*’头来允许任何来源,或者至少返回我在jolokia Access.xml上指定的允许来源的头,但是我不知道响应时有什么标题,我也不确定我错过了什么。
我正在运行的监控javascript不在使用我的spring引导应用程序的同一台服务器上,因为我想让脚本远程运行。
下面是我的春季引导应用程序设置。
application.properties和jolokia access .xml都在类路径中,因此我可以看到我在application.properties上为management.port所做的更改以及对jolokia access. are的远程后访问限制。
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>
:application.properties
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}然而,在下面,javascipt失败了,因为从jolokia端点重新定位没有访问控制-允许-原产地‘头。即使我设置了‘endpoints.cors.lable-源=*’属性,但我没有在响应中看到头。
监控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正如您在上面看到的,根本没有“访问-控制-允许-原产地”头。
发布于 2015-06-12 20:27:37
默认情况下,Jolokia允许任何跨源访问。所以根本不需要一个jolokia-access.xml。
另一方面,我认为你的模式应该行得通。但是,请在GitHub上提出一个问题,以便我们可以继续解决这个问题。
https://stackoverflow.com/questions/30794572
复制相似问题