Grails4 整合spring cloud consul服务注册

由于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

改完重启就添加完成了,服务注册成功

扫描二维码关注公众号,回复: 8735333 查看本文章
发布了72 篇原创文章 · 获赞 38 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_16165281/article/details/103925902
今日推荐