端点
定义一个标识用于对外公开有用信息,可以用mvc去做映射也可以用别的形式,下面介绍的一种就是通过mvc去做映射的。
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--不加这个调用时会提示无权限-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml
server:
port: 8082
management:
security:
enabled: false
security:
basic:
enabled: false
manager:
endpoints:
refreshCache:
enabled: true
sensitive: false
package com.recon.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Map;
/**
* @author : lxk
* @Date : 2019/1/23 15:07
* @Description :刷新缓存端点 通过 post 域名+端口+refreshCache(端点id) 调用
*/
@RefreshScope
@Component
@ConfigurationProperties(prefix = "manager.endpoints.refreshCache")
public class RefreshCacheEndPoint extends AbstractEndpoint<Map<String, Object>> {
private static final Map<String, Object> REFRESH_CACHE_MESSAGE = Collections
.unmodifiableMap(Collections.<String, Object>singletonMap("message",
"has refresh, say you later..."));
public RefreshCacheEndPoint() {
super("refreshCache", true, false);
}
@Override
public Map<String, Object> invoke() {
//逻辑代码
return REFRESH_CACHE_MESSAGE;
}
@Bean
@ConditionalOnBean(RefreshCacheEndPoint.class)
@ConditionalOnEnabledEndpoint(value = "refreshCache",enabledByDefault = true)
public RefreshCacheMvcEndpoint refreshCacheMvcEndpoint(RefreshCacheEndPoint delegate){
return new RefreshCacheMvcEndpoint(delegate);
}
}
package com.recon.configclient;
import org.springframework.boot.actuate.endpoint.mvc.ActuatorMediaTypes;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Collections;
import java.util.Map;
/**
* Adapter to expose {@link RefreshCacheEndPoint} as an {@link MvcEndpoint}.
* @author : lxk
* @Date : 2019/1/22 10:59
* @Description : 对http做代理,将端点和http url做映射
*/
@ConfigurationProperties(prefix = "manager.endpoints.refreshCache")
public class RefreshCacheMvcEndpoint extends EndpointMvcAdapter {
public RefreshCacheMvcEndpoint(RefreshCacheEndPoint delegate) {
super(delegate);
}
@PostMapping(produces = { ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON_VALUE,
MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
@Override
public Object invoke() {
if (!getDelegate().isEnabled()) {
return new ResponseEntity<Map<String, String>>(
Collections.singletonMap("message", "This endpoint is disabled"),
HttpStatus.NOT_FOUND);
}
return super.invoke();
}
}
调用 crul -X POST http://localhost:8082/refreshCache