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