springboot 集成 elasticsearch 和 redis 入门

springboot 集成 elasticsearch 入门

//1. 定义配置类 ElasticSearchClientConfig class
@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http")));
        return client;
    }
}

// 测试索引的创建
@SpringBootTest
class Es01ApplicationTests {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    // 测试索引的创建 Request kuang_index
    @Test
    void testCreateIndex() throws IOException {
        // 1. 创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("kuang_index");

        // 2. 客户端执行请求 indicesClient ,请求后获得响应
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);


    }

}

// 结果:org.elasticsearch.client.indices.CreateIndexResponse@a2a3b2a2

springboot 集成 Redis入门

  1. Jedis
//1.  ping
public class TestPing {
    public static void main(String[] args) {
        // new jedis 对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //
        System.out.println(jedis.ping());
    }
}

//2. Jedis 写一个Redis 事务

public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("name","zhangsan");

        // 开启事务
        Transaction multi = jedis.multi();

        String result = jsonObject.toJSONString();
        try {
            multi.set("user1",result);
            multi.set("user2",result);
            //int i = 1/0; // 模拟失败
            multi.exec(); // 执行事务
        } catch (Exception e) {
            multi.discard(); // 放弃事务
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close(); // 关闭连接
        }
    }
}
  1. Redis
//1. Redis 配置类
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate();
//        Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>();

        //配置具体的序列化方式
//        template.setKeySerializer(objectJackson2JsonRedisSerializer);
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

// 2. pojo user class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {
    private String name;
    private int age;
}


#配置文件application.properties

# 配置springboot所有的配置类都有一个自动配置类 RedisAutoConfiguration

# 自动配置类会绑定一个properties 配置文件 RedisProperties

# 配置Redis

spring.redis.host=127.0.0.1
spring.redis.port=6379
// 3. Redis test
@SpringBootTest
class Redis02SpringbootApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("offer","baidu");
        System.out.println(redisTemplate.opsForValue().get("offer"));
    }

    @Test
    public void test() throws JsonProcessingException {
        User user = new User("张三", 3);
        String jsonUser = new ObjectMapper().writeValueAsString(user);

        redisTemplate.opsForValue().set("user",jsonUser);

        System.out.println(redisTemplate.opsForValue().get("user"));
    }

}

posted @ 2020-08-28 00:21  li修远  阅读(429)  评论(0编辑  收藏  举报