Zookeeper的Linux下载与springcloud的配置

1、在Linux中配置zookeeper

1)解压到安装到 /opt/module 下

2)进入zookeeper

3)进入conf

4)执行:

cp zoo_sample.cfg zoo.cfg

vim zoo.cfg

修改

Zookeeper的Linux下载与springcloud的配置_第1张图片

5)改成自己的目录

6)之后修改 /etc/profile中的信息

 2、运行

进入到 zookeeper/bin目录下

zkServer.sh start  

zkServer.sh status 查看状态

3、配置springcloud-zookeeper-provider-9003

父类的pom.xml

    
        Hoxton.SR12
        2.3.12.RELEASE
        8.0.26
        2.2.0
        1.2.3
    


    
        

            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring.cloud.version}
                pom
                import
            

            
                org.springframework.boot
                spring-boot-dependencies
                ${spring.boot.version}
                pom
                import
            

            
                mysql
                mysql-connector-java
                ${mysql.version}
            

            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                ${mybatis.version}
            

            
                com.alibaba
                druid
                ${druid.version}
            

        
    

Zookeeper的Linux下载与springcloud的配置_第2张图片

子类的pom.xml

    

        
            org.example
            springcloud-api
            1.0-SNAPSHOT
        

        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.11
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        


        
            mysql
            mysql-connector-java
            8.0.26
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            com.alibaba
            druid
            1.2.3
        


        
            tk.mybatis
            mapper-spring-boot-starter
            2.1.5
        

    

application.yaml

实体类:


/**
 * (Movie)实体类
 *
 * @author makejava
 * @since 2021-12-17 09:12:14
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "movie")
public class Movie implements Serializable {
    private static final long serialVersionUID = -30737887108345962L;
    @Id
    private Integer id;

    private String movieName;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date movieData;


}

mapper:


import com.ycz.springCloud.entity.Movie;
import org.apache.ibatis.annotations.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * (Movie)表数据库访问层
 *
 * @author makejava
 * @since 2021-12-17 09:12:13
 */
@Mapper
public interface MovieMapper extends tk.mybatis.mapper.common.Mapper,MySqlMapper {


}

service:

package com.ycz.springCloud.service;


/**
 * (Movie)表服务接口
 *
 * @author makejava
 * @since 2021-12-17 09:12:14
 */
public interface MovieService {


    Object getAll();

    Object getOne(Integer id);
}

serviceImpl:

import com.ycz.springCloud.mapper.MovieMapper;
import com.ycz.springCloud.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Movie)表服务实现类
 *
 * @author makejava
 * @since 2021-12-17 09:12:15
 */
@Service("movieService")
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;


    public Object getAll() {
        return movieMapper.selectAll();
    }

    public Object getOne(Integer id) {
        return movieMapper.selectByPrimaryKey(id);
    }
}
import com.ycz.springCloud.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * (Movie)表控制层
 *
 * @author makejava
 * @since 2021-12-17 09:12:12
 */
@RestController
@RequestMapping("movie")
public class MovieController {
    /**
     * 服务对象
     */
    @Autowired
    private MovieService movieService;

    @RequestMapping("getAll")
    public Object getAll(){
       return movieService.getAll();
    }

    @RequestMapping("/getOne/{id}")
    public Object getOne(@PathVariable Integer id){
        return movieService.getOne(id);
    }


}

启动类:


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

4、配置consumer中的zookeeper

Zookeeper的Linux下载与springcloud的配置_第3张图片

pom.xml文件:

    
        
            org.example
            springcloud-api
            1.0-SNAPSHOT
            
                
                    tk.mybatis
                    mapper-spring-boot-starter

                
            
        


        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
            
                
                    org.apache.zookeeper
                    zookeeper
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.11
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        

    

配置:

server:
  port: 9004
spring:
  application:
    name: zookeeper-consumer-9004
  cloud:
    zookeeper:
      connect-string: ip:port #配置zookeeper的端口号与ip

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


}

UserController

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL="http://springcloud-zookeeper-provider-9003";

    @RequestMapping("/getAll")
    public Object getAll(){
        ResponseEntity forEntity = restTemplate.getForEntity(REST_URL + "/movie/getAll", Object.class);
        return forEntity;
    }

    @RequestMapping("/get/{id}")
    public Object getOne(@PathVariable("id") Integer id){
        ResponseEntity forEntity = restTemplate.getForEntity(REST_URL + "/movie/getOne/"+id, Object.class);
        return forEntity;
    }


}
 
  

启动类:

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

5、启动

Zookeeper的Linux下载与springcloud的配置_第4张图片

 正常!!!

你可能感兴趣的:(笔记,linux,zookeeper,spring,cloud)