使用Java操作mongdb,redis,rabbitmq

Java操作mongodb

public static void main(String[] args) {
    MongoClient mongoClient = new MongoClient("192.168.220.130", 27017);
    MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
    MongoCollection<Document> collection = mongoDatabase.getCollection("user");
    Document document = new Document("name","张三")
        .append("sex", "男")
        .append("age", 18);
    collection.insertOne(document)  ;
    FindIterable findIterable = collection.find();
    MongoCursor cursor = findIterable.iterator();
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }
}

需要用到的jar包:mongo-java-driver-3.4.3.jar

Java操作redis

public static void main(String[] args) {
    Jedis jedis = new Jedis("192.168.220.130",6379);
    jedis.set("username", "ljbao");
    System.out.println(jedis.ping());
    System.out.println(jedis.get("username"));
}

需要用到的jar包:jedis-2.9.0.jar

Java操作rabbitmq

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.220.130");
    factory.setPort(5672);
    factory.setUsername("guest");
    factory.setPassword("guest");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    System.out.println("----------connected successful!----------");
}

需要用到的jar包:amqp-client-5.8.0.jar

依赖包:

slf4j-api-1.7.26.jarslf4j-simple-1.7.26.jar