SpringCloud - consul 服務註冊生產(2)

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

consul

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章