springCould中的zookeeper-从小白开始【3】

目录

1.启动zookeeper❤️❤️❤️

2.创建8004模块❤️❤️❤️

3.临时节点还是永久节点❤️❤️❤️

4.创建zk80消费模块❤️❤️❤️


1.启动zookeeper❤️❤️❤️

  • 进入自己zookeeper的bin目录下 分别使用命令:
php 复制代码
./zkServer.sh start   和 ./zkCli.sh -server 主机名

2.创建8004模块 ❤️❤️❤️

1.建模块

在父工程下创建模块

注意:jdk版本和,maven版本

2.添加pom

1.springboot依赖

2.mybatis依赖

3.druid依赖

4.mysql依赖

5.zookeeper依赖

XML 复制代码
 <dependencies>
        <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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</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>

        <!--引入自己的api通用包-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--springboot整合zookeeper客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>

3.添加yml

1.服务端口

2.服务名称

3.数据库连接池

4.zookeeper连接地址

5.mybatis配置

Groovy 复制代码
server:
  port: 8004


spring:
  application:
    #服务名称
    name: cloud-provider-payment

  datasource:
    #druid连接池
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: 123456
    #zookeeper服务注册中心
  cloud:
    zookeeper:
      connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.xz.springcloud.entity

4.添加主启动类

@EnableDiscoveryClient:将服务注册到服务注册中心(zookeeper)

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient//作为服务中心
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class);
    }
}

5.编写业务类

java 复制代码
@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/getById/{id}")
    public CommonResult getById(@PathVariable("id") Integer id){
        Payment payment = paymentService.getPaymentById(id);
        if (payment!=null){
            return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
        }else {
            return new CommonResult(404,"查询无果,serverPort:"+serverPort,null);
        }
    }

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

6.测试

  • 1.启动主启动类,刷新浏览器
  • 2.访问zookeeper

说明zookeeper配置成功

3.临时节点还是永久节点❤️❤️❤️

当关闭8004服务时,在查询zookeeper,发现节点剔除

所以:zookeeper存储的是临时节点,当服务消失时,会马上剔除

4.创建zk80消费模块❤️❤️❤️

1.建模块

在父工程下创建模块

注意jdk版本和maven版本

2.写pom

1.springboot依赖

2.通用依赖

3.zookeeper依赖

XML 复制代码
<dependencies>
        <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.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>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--spring boot整合zookeeper-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

    </dependencies>

3.改yml

1.服务端口

2.服务名

3.zookeeper连接ip(我这里使用集群)

java 复制代码
server:
  port: 80
  
spring:
  application:
    name: cloud-consumer-order
    
  cloud:
    zookeeper:
      connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181

4.主启动类

@EnableDiscoveryClient添加上

java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZkMain80.class);
    }
}

5.业务代码

  • 1.先将Rest Template注入到spring中

注意:因为访问的是服务名,而不是具体的ip,所以加上@LoadBalance注解

java 复制代码
@Configuration
public class ApplicationConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  • 2.controller

1.声明常量,表示zookeeper中8004的服务名

2.注入RestTemplate

3.调用restTemplate访问

java 复制代码
@RestController
public class OrderController {

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

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/payment/getById/{id}")
    public CommonResult<Payment>  getPayment(@PathVariable("id") Integer id){
        return restTemplate.getForObject(INVOKE_URL+"/payment/getById/"+id,CommonResult.class);
    }

    @GetMapping("/payment/zk")
    public String getDiscovery(){
        return restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
    }
}

6.测试

  • 1.使用80访问,刷新浏览器,依然成功
  • 2.查看zookeeper节点 ,节点创建成功

相关推荐
大数据编程之光11 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
我言秋日胜春朝★20 分钟前
【Linux】进程地址空间
linux·运维·服务器
爪哇学长25 分钟前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
ExiFengs29 分钟前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
paj12345678930 分钟前
JDK1.8新增特性
java·开发语言
捂月36 分钟前
Spring Boot 深度解析:快速构建高效、现代化的 Web 应用程序
前端·spring boot·后端
繁依Fanyi41 分钟前
简易安卓句分器实现
java·服务器·开发语言·算法·eclipse
C-cat.41 分钟前
Linux|环境变量
linux·运维·服务器
慧都小妮子1 小时前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net
yunfanleo1 小时前
docker run m3e 配置网络,自动重启,GPU等 配置渠道要点
linux·运维·docker