SpringCloud注册进Consul

  1. 启动Consul
    下载Consul https://www.consul.io/downloads
    在consul.exe路径下,启动conusl
    命令:consul agent -dev

    SpringCloud注册进Consul_第1张图片

  2. 客户端程序
    添加依赖 pom.xml

    	
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-consul-discoveryartifactId>
        dependency>
    
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
    

    application.yml

    # Springcloud 微服务注册进Consul
    # 启动Consul ,进入consul.exe目录:consul agent -dev
    # 查看Consul首页: http://localhost:8500/
    server:
      port: 8006
    spring:
      application:
        name: consul-provider-payment
      cloud:
        # consul 注册中心地址
        consul:
          host: localhost
          port: 8500
          discovery:
            #hostname: 127.0.0.1
            service-name: ${
           spring.application.name}
    

    启动类

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

    测试类

    @RestController
    public class PaymentController {
           
    
        @Value("${server.port}")
        private String serverPort;
    
        @GetMapping("/payment/consul")
        public String paymentTestConsul(){
           
            return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
        }
    
    }
    

    启动主程序

  3. 测试
    SpringCloud注册进Consul_第2张图片SpringCloud注册进Consul_第3张图片

你可能感兴趣的:(Springcloud,consul,spring,boot,springcloud,注册中心)