springcloud-服务消费者者注册进consul

  1.创建模块

  2.改pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.aib.springcloud</groupId>
            <artifactId>springclud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  3.改配置

server:
  port: 80

# 服务别名---zookeeper注册中心名称
spring:
  application:
    name: consul-consumer-payment
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

  4.主启动

@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderConsulApplication.class, args);
    }
}

  5.写业务

@RestController
public class OrderController {

    //支付服务地址
    public static final String ZK_URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/consul")
    public String getpaymentInfo(){

        String returnValue = restTemplate.getForObject(ZK_URL+"/payment/consul", String.class);

        return returnValue;
    }
}

  6.测试

posted @ 2021-01-06 11:29  爱编程DE文兄  阅读(158)  评论(0编辑  收藏  举报