我正在尝试使用Spring功能的micronaut,但无法访问我的执行器端点Spring Boot v2.3.0 release和micronaut 2.0.0.M3。
我创建了一个基本的micronaut项目,使用的是 mn create-app xxx --lang groovy
.
然后我添加了spring的依赖关系和micronaut-spring以及相关的注解处理器。
现在我的build.gradle看起来如下
plugins {
id "groovy"
id "com.github.johnrengelman.shadow" version "5.2.0"
id "application"
id "net.ltgt.apt" version "0.21" //enables annotation preproccessing support
id "net.ltgt.apt-idea" version "0.21"
id "org.springframework.boot" version "2.3.0.RELEASE"
id "io.spring.dependency-management" version "1.0.9.RELEASE"
}
ext['groovy.version'] = '3.0.4'
version "0.1"
group "com.softwood"
repositories {
mavenCentral()
maven { url "https://jcenter.bintray.com" }
}
configurations {
// for dependencies that are needed for development only
developmentOnly
}
dependencies {
//annotation processors
annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
annotationProcessor "io.micronaut.spring:micronaut-spring-boot-annotation"
annotationProcessor "io.micronaut.spring:micronaut-spring-web-annotation"
testAnnotationProcessor "io.micronaut.spring:micronaut-spring-web-annotation:2.0.1"
//latest groovy v3
implementation group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.4' //, ext: 'pom'
//spring
compileOnly "io.micronaut.spring:micronaut-spring-annotation:2.0.1"
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation group: 'io.micronaut.spring', name: 'micronaut-spring-web', version: '2.0.1'
runtime ("io.micronaut.spring:micronaut-spring-boot:2.0.1")
compileOnly platform("io.micronaut:micronaut-bom:$micronautVersion")
compileOnly "io.micronaut:micronaut-inject-groovy"
implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
implementation "io.micronaut:micronaut-runtime-groovy"
implementation "io.micronaut:micronaut-validation"
implementation "javax.annotation:javax.annotation-api"
implementation "io.micronaut:micronaut-http-server-netty"
implementation "io.micronaut:micronaut-http-client"
runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
testCompileOnly platform("io.micronaut:micronaut-bom:$micronautVersion")
testImplementation platform("io.micronaut:micronaut-bom:$micronautVersion")
testImplementation("org.spockframework:spock-core:2.0-M2-groovy-3.0") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
testImplementation "io.micronaut:micronaut-inject-groovy"
testImplementation "io.micronaut.test:micronaut-test-spock"
testImplementation "io.micronaut.test:micronaut-test-junit5"
}
test.classpath += configurations.developmentOnly
mainClassName = "com.softwood.Application"
//added - enable incremetal compile feature - can't enable with annotation processors !
/*tasks.withType(GroovyCompile).configureEach {
options.incremental = true
}*/
tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.jvmArgs.add('-Dgroovy.parameters=true')
}
shadowJar {
mergeServiceFiles()
}
tasks.withType(JavaExec) {
classpath += configurations.developmentOnly
jvmArgs('-noverify', '-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')
}
我调整了一个HelloController,如下所示
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.HttpStatus
import org.springframework.boot.actuate.endpoint.annotation.Endpoint
@Controller("/hello")
@Endpoint
class HelloController {
@Get("/")
String index() {
return 'hello william'
}
}
并将Application类标记为SpringbootApplication--但调用Micronaut.run()来构建MN上下文等。
import io.micronaut.runtime.Micronaut
import groovy.transform.CompileStatic
import org.springframework.boot.autoconfigure.SpringBootApplication
@CompileStatic
@SpringBootApplication
class Application {
static void main(String[] args) {
Micronaut.run(Application)
}
}
我试着调整application.yml,让所有的端点都暴露出来,如下所示。
micronaut:
application:
name: micronaut-std-app
management:
endpoints:
web:
exposure:
include=*:
这一切都可以编译和运行。localhost:8080/hello
你会得到预期的字符串
如果你尝试访问执行器端点--没有任何东西被生成,浏览器会显示
{"message":"Page Not Found","_links":{"self":{"href":"/actuator/health","templated":false}}}
如果你想做一个混合的micronaut和spring项目,那么你如何为你的MN预处理控制器等启用任何执行器端点?
如果你只是想要一个健康检查端点,Micronaut有内置的健康检查端点。
在build.gradle中添加下面的依赖关系来添加像健康这样的端点。
implementation("io.micronaut:micronaut-management")
你可以在application.yml中添加
endpoints:
health:
enabled: true
然后把健康检查的网址改成 "健康"。