由于grails4使用的是spring boot2.1版本,所以需要注意引入的spring cloud版本,具体版本对照可以自行百度 ,以及consul组件的版本也需要注意对应springboot版本
consul安装启动自行百度
1、引包build.gradle
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
compile group: 'org.springframework.cloud', name: 'spring-cloud-dependencies', version: 'Greenwich.SR4', ext: 'pom'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-consul-discovery', version: '2.1.4.RELEASE'
2、启动类上添加注解
@EnableDiscoveryClient
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
3、添加配置 application.yml
server:
port: 8085
spring:
application:
name: "consulServer2"
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
service-name: ${spring.application.name}
register: true
instance-id: ${spring.application.name}:${server.port}
这里需要注意:一定要增加:instance-id,否则会报错
4、编写一个DemoController测试
@RestController
@RequestMapping(value = "/demo")
class DemoController {
static responseFormats = ['json', 'xml']
@GetMapping(value = "index")
def index() {
return "test"
}
}
5、启动结果
这里需要注意一下Checks如果是报错的,需要开启健康检查配置【actuator】
management:
endpoints:
enabled-by-default: true
改完重启就添加完成了,服务注册成功