2020最新版Springcloud-Alibaba ZooKeeper篇(三)

一. 安装Zookeeper

zk的安装过程什么的就不废话了,这里有我之前安装的分布式的一个zk,可以参照下zk集群安装,本篇不再强调分布式,而是使用zk作为服务中心,大家可以不用配zk集群,用单机一样的~

1.1 压缩包

百度网盘Zookeeper

提取码 :ds2b

1.2 安装解压到服务器上

这一步不做过多讲解

1.3 关闭防火墙

  • sudo systemctl stop firewalld 临时关闭

  • sudo systemctl disable firewalld ,然后reboot 永久关闭

  • sudo systemctl status firewalld 查看防火墙状态。

二. 创建新模块 cloud-provider-payment8004

2.1 修改POM文件引入依赖

<?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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8004</artifactId>


    <dependencies>
        <!--springboot整合zookeeper客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!--Springboot整合web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入自定义的api通用包,可以使用payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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>

2.2 创建 application.yml 配置文件,IP你要填你自己服务器的IP哦

#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004

#服务别名
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.65.10:2181

2.3 主启动类

/**
 * @Author: cws
 * @Date: 12:56 2020/8/13
 * @Description:
 * @Version v1.0
 */
@SpringBootApplication
@EnableDiscoveryClient   //该注解用于想使用consul或者zookeeper作为注册中心时注册服务
public class Payment8004 {
    public static void main(String[] args) {
        SpringApplication.run(Payment8004.class, args);
    }
}

2.4 业务类 controller层

/**
 * @Author: cws
 * @Date: 13:10 2020/8/13
 * @Description:
 * @Version v1.0
 */
@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/zk/get")
    public String paymentzk() {
        return "Spring cloud with zk port: " + serverPort + "\t" + UUID.randomUUID().toString();
    }
}

 2.5 测试

连上我们的zkclient后,查找我们注册进来的服务,可以找到就代表成功注册进来

三. 创建订单服务module cloud-consumerzk-order80

4.1 创建模块(重复步骤不讲)

4.2 改pom

    <dependencies>
        <!--springboot整合zookeeper客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--排除自带的zookeeper-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加zookeeper3.4.14版本-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--springboot整合web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

4.3 建 application.yml

server:
  port: 80
spring:
  application:
    name: cloud-consumer-order
  cloud:
    #注册到zookeeper地址
    zookeeper:
      connect-string: 公网IP地址:2181

4.4 主启动

/**
 * @Author: cws
 * @Date: 15:24 2020/8/13
 * @Description:
 * @Version v1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZKMain80.class, args);
    }
}

4.5 业务类

 消费者需要调用 RestTemplate 来调用远程服务

/**
 * @Author: cws
 * @Date: 15:27 2020/8/13
 * @Description:
 * @Version v1.0
 */
@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced   //负载均衡的能力
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
/**
 * @Author: cws
 * @Date: 15:37 2020/8/13
 * @Description:
 * @Version v1.0
 */
@RestController
@Slf4j
public class OrderZKController {

    public static final String INVOKE_URL = "http://cloud-provider-payment";
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/zk/get")
    public String paymentInfo() {
        String result = restTemplate.getForObject(INVOKE_URL + "/zk/get", String.class);
        return result;
    }
}

 

posted @ 2020-08-13 15:54  白描先生  阅读(712)  评论(0编辑  收藏  举报
}); });