SpringCloud - consul 服务注册生产(2)
1、 添加maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、 添加 testwebapp/src/main/java/com/jscenter/testwebapp/TestWebAppApplication.java
# package com.jscenter.testwebapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class TestWebAppApplication {
public static void main(String[] args) {
SpringApplication.run(TestWebAppApplication.class, args);
}
}
3、 添加 testwebapp/src/main/java/com/jscenter/testwebapp/HelloController.java
# package com.jscenter.testwebapp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String home() {
return "hello home";
}
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
@RequestMapping("/health")
public String health() {
return "ok";
}
}
4、 添加 testwebapp/src/main/resources/application.properties
spring.application.name=testwebapp
server.port=9000
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=testwebapp
5、 访问 http://localhost:9000/health
ok
6、访问 consul


本文详细介绍了如何使用SpringCloud整合Consul实现服务注册与发现的过程。从添加Maven依赖开始,逐步展示了配置应用、创建控制器以及注册服务到Consul的具体步骤。通过实例演示了如何验证服务注册状态及健康检查。
1023

被折叠的 条评论
为什么被折叠?



