10. Redis

zookeeper、sentinel解决redis高可用问题

搭建Redis 集群

version: '3.1'
services:
  master:
    restart: always
    image: redis
    container_name: redis-master
    ports:
      - 6379:6379


  slave1:
    restart: always
    image: redis
    container_name: redis-slave-1
    ports:
      - 6380:6379
    command: redis-server --slaveof redis-master 6379


  slave2:
    restart: always
    image: redis
    container_name: redis-slave-2
    ports:
      - 6381:6379
    command: redis-server --slaveof redis-master 637

搭建sentinel集群

version: '3.1'
services:
  sentinel1:
    restart: always
    image: redis
    container_name: redis-sentinel-1
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf


  sentinel2:
    restart: always
    image: redis
    container_name: redis-sentinel-2
    ports:
      - 26380:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf


  sentinel3:
    restart: always
    image: redis
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/etc/redis/sentinel.con

sentinel.conf

port 26379
dir /tmp
# 自定义集群名 其中127.0.0.1为redis-master的ip。 6379为redis-master的端口,2为最小投票数(因为有3台sentinel所以可以设
置成2)
sentinel monitor mymaster 192.168.91.134 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

复制三份

cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf

检查是否成功

docker exec -it redis-sentinel-1 /bin/bash
redis-cli -p 26379

sentinel master mymaster

连接Sentinel

创建redis服务提供者Server-Redis

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xd</groupId>
        <artifactId>Common-Dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../Common-Dependencies/pom.xml</relativePath>
    </parent>
    <artifactId>Server-Redis</artifactId>
    <packaging>jar</packaging>


    <!--配置私服-->
    <distributionManagement>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.91.131:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.91.131:8081/repository/maven-releases/</url>
        </repository>
    </distributionManagement>


    <dependencies>
        <!--Spring Boot Begin-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Spring Boot End-->
        <!--Spring Cloud Begin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--Spring Cloud end-->
        <!--domain-->
        <dependency>
            <groupId>com.xd</groupId>
            <artifactId>Common-Domain</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <!--加入zipkin,config和admin的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <!--springcloud结束-->
        <!-- 添加mysql驱动包  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--<version>5.1.45</version>-->
        </dependency>


        <!-- 添加阿里连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <!--<version>1.0.31</version>-->
        </dependency>


        <!-- mybatis和springBoot整合  -->
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>1.3.2</version>-->
<!--        </dependency>-->
        <!--swagger-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger2</artifactId>-->
<!--            <version>${swagger2.version}</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger-ui</artifactId>-->
<!--            <version>${swagger2.version}</version>-->
<!--        </dependency>-->


<!--        Redis-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--        Reids-->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.xd.redis.RedisApplication</mainClass><!--指向应用程序的入口,否则打jar包,java -jar无用 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <name>Aliyum Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!--        配置nexus代理仓库-->
        <repository>
            <id>nexus</id>
            <name>Nexus Repository</name>
            <url>http://192.168.91.131:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>Nexus Plugin Repository</name>
            <url>http://192.168.91.131:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</proj

启动类

package com.xd.redis;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


/**
 * Created by zlj on 2020/3/10.
 * Project Name : SpringCloudDemo
 * Package Name : com.xd.redis
 */
@SpringBootApplication
@EnableEurekaClient
public class RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class,args);
    }

service

package com.xd.redis.service;




/**
 * Created by zlj on 2020/3/10.
 * Project Name : SpringCloudDemo
 * Package Name : com.xd.redis.service
 */
public interface RedisService {


    public void put(String key,Object value,long seconds);


    public Object get(String key)

serviceImpl

package com.xd.redis.serviceImpl;


import com.xd.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.concurrent.TimeUnit;




/**
 * Created by zlj on 2020/3/10.
 * Project Name : SpringCloudDemo
 * Package Name : com.xd.redis.serviceImpl
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void put(String key, Object value, long seconds) {
        redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }


    @Override
    public Object get(String key) {
      return  redisTemplate.opsForValue().get(key);
  

controller

package com.xd.redis.controller;


import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
import com.xd.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by zlj on 2020/3/10.
 * Project Name : SpringCloudDemo
 * Package Name : com.xd.redis.controller
 */
@RestController
public class RedisController {
    @Autowired
    private RedisService redisService;


    @RequestMapping(value = "put",method = RequestMethod.POST)
    public String put(String key, String value, long seconds){
        redisService.put(key,value,seconds);
        return "ok";
    }


    @RequestMapping(value = "get",method = RequestMethod.GET)
    public String get(String key){
        Object o = redisService.get(key);
       if (o !=null){
           String json = String.valueOf(o);
           return json;
       }
       else {
           return "wrong";
       }
    

bootstrap.yml

spring:
  cloud:
    config:
      label: master   #配置仓库的分支
      uri: http://localhost:8888
      name: Server-Redis
      profile: dev

bootstrap-prod.yml

spring:
  cloud:
    config:
      label: master   #配置仓库的分支
      uri: http://192.168.91.130:8888
      name: Server-Redis
      profile: prod

配置config

Server-Redis-dev.yml

#配置服务名称
spring:
  application:
    name: Server-Redis
  #    配置链路追踪
  zipkin:
    base-url: http://localhost:9411
  boot:
    admin:
      client:
        url: http://localhost:8084
  redis:
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1ms
        min-idle: 0
    sentinel:
      master: mymaster
      nodes: 192.168.91.134:26379,192.168.91.134:26380,192.168.91.134:26381
#端口
server:
  port: 8766


eureka:
  client:  #表示此时是客户端
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/   #表示客服端向服务端进行注册,注册的地址是服务端的地址


#监控
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*

Server-Redis-prod.yml

#配置服务名称
spring:
  application:
    name: Server-Redis
  #    配置链路追踪
  zipkin:
    base-url: http://192.168.91.130:9411
  boot:
    admin:
      client:
        url: http://192.168.91.130:8084
  redis:
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1ms
        min-idle: 0
    sentinel:
      master: mymaster
      nodes: 192.168.91.134:26379,192.168.91.134:26380,192.168.91.134:26381




#端口
server:
  port: 8766


eureka:
  client:  #表示此时是客户端
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://192.168.91.130:8761/eureka/,http://192.168.91.130:8861/eureka/,http://192.168.91.130:8961/eureka/   #表示客服端向服务端进行注册,注册的地址是服务端的地址
#监控
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值