zookeeper作注册中心 - 服务消费者代码

1建工程cloud-consumer-zookeeper-order80

2pom与cloud-privider-payment工程一致

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>com.shishi.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-zookeeper-order80</artifactId>

    <dependencies>
        <!--spring整合进入zookeeper-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.shishi.springcloud</groupId>
            <artifactId>cloud-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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

application.yml:

server:
  port: 80
# 服务别名,注册zookeeper到注册中心名称
spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: 192.168.53.179:2181

主启动类:

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

ApplicationConfig:

@Configuration
public class ApplicationConfig {
    @Bean
    @LoadBalanced //这个注解如果没有则会报错:I/O error on GET request for "http://cloud-provider-payment/payment/zk": cloud-provider-payment; nes
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

Controller:

@RestController
@Slf4j
public class OrderController {
    @Resource
    private RestTemplate restTemplate;
    private static final String url = "http://cloud-provider-payment";

    @GetMapping(value="/consumer/payment/zk")
    public String getPaymentInfo() {
        String result = restTemplate.getForObject(url + "/payment/zk", String.class);
        return result;
    }
}

 

2.测试注册进zookeeper:

 

 3.测试程序访问成功:

 

posted @ 2021-05-25 11:38  圣金巫灵  阅读(89)  评论(0编辑  收藏  举报