项目名称:cloud-provide-payment8004

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

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

1.添加pom.xml

  1. <dependencies>
  2. <!-- SpringBoot整合Web组件 -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!-- SpringBoot整合zookeeper客户端 -->
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  11. <!--先排除自带的zookeeper3.5.3-->
  12. <exclusions>
  13. <exclusion>
  14. <groupId>org.apache.zookeeper</groupId>
  15. <artifactId>zookeeper</artifactId>
  16. </exclusion>
  17. </exclusions>
  18. </dependency>
  19. <!--添加zookeeper3.4.9版本-->
  20. <dependency>
  21. <groupId>org.apache.zookeeper</groupId>
  22. <artifactId>zookeeper</artifactId>
  23. <version>3.4.9</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-devtools</artifactId>
  28. <scope>runtime</scope>
  29. <optional>true</optional>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.projectlombok</groupId>
  33. <artifactId>lombok</artifactId>
  34. <optional>true</optional>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>

2.添加application.yml配置文件

  1. #8004表示注册到zookeeper服务器的支付服务提供者端口号
  2. server:
  3. port: 8004
  4. #服务别名----注册zookeeper到注册中心名称
  5. spring:
  6. application:
  7. name: cloud-provider-payment
  8. cloud:
  9. zookeeper:
  10. connect-string: 192.168.28.132:2181

2.添加主启动类

/**
 * @EnableDiscoveryClient 该注解用于向使用consul或者zookeeper作为注册中心时注册服务
 * @author 11752
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8004Application {
    public static void main(String[] args) {
        SpringApplication.run(Payment8004Application.class, args);
    }
}

3.添加PaymentController

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

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

4.启动cloud-provide-payment8004

启动成功后可以看到Zookeeper中已经有了我们的cloud-provider-payment服务了
image-20200312220738476.png

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

springcloud with zookeeper: 8004 9af08348-34c1-474a-9ba6-e0e0433d5b8a