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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值