项目名称:cloud-providerconsul-payment8006

鼠标点击父工程项目右击 —> New —> Module

具体创建步骤参考【搭建EurekaServer端服务注册中心

1.添加pom.xml

  1. <dependencies>
  2. <!--SpringCloud consul-server -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  6. </dependency>
  7. <!-- SpringBoot整合Web组件 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-actuator</artifactId>
  15. </dependency>
  16. <!--日常通用jar包配置-->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-devtools</artifactId>
  20. <scope>runtime</scope>
  21. <optional>true</optional>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <optional>true</optional>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>cn.hutool</groupId>
  35. <artifactId>hutool-all</artifactId>
  36. <version>RELEASE</version>
  37. <scope>test</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>cn.hutool</groupId>
  41. <artifactId>hutool-all</artifactId>
  42. <version>RELEASE</version>
  43. <scope>test</scope>
  44. </dependency>
  45. </dependencies>

2.添加application.yml配置文件

###consul服务端口号,consul会发送健康检查请求到8006端口
server:
  port: 8006

spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      ####consul注册中心地址
      host: 192.168.28.132
      port: 8500
      discovery:
        service-name: ${spring.application.name}
       #consul发送健康检查的地址,配置当前项目所在服务器的IP地址,前提是consul和当前项目不在同一台服务器
        hostname: 192.168.0.108
        #健康检查的url,默认会根据hostname配置的IP + server.port/actuator/health
        #health-check-url: http://192.168.0.108:8006/actuator/health

3.添加主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentConsul8006Application {
    public static void main(String[] args) {
        SpringApplication.run(PaymentConsul8006Application.class, args);
    }
}

4.添加PaymentController

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/payment/consul")
    public String paymentConsul() {
        return "springcloud with consul: " + serverPort + "\t   " + UUID.randomUUID().toString();
    }
}

5.启动cloud-providerconsul-payment8006


浏览器输入:http://192.168.28.132:8500/

可以看到服务已经成功注册了
image-20200313132309560.png

浏览器输入:http://localhost:8006/payment/consul 返回如下结果

springcloud with consul: 8006 4630ed1d-26e8-4434-a831-3891a587011b