Spring Boot + docker +mongo
启动mongo镜像
docker run --name mongo-container -d -P mongo
连接到容器内
docker exec -it eb sh
输入:mongo

输入:show dbs

输入:db.stats()

下载mongo客户端:https://robomongo.org/download
按ctrl+c 和 exit 退出容器,输入:docker ps

这里自动映射的端口为32768,打开Robo 3T,输入地址,点击test


在左侧就能看到库了

新建spring-boot项目,添加pom引用
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.pegdown/pegdown -->
<dependency>
<groupId>org.pegdown</groupId>
<artifactId>pegdown</artifactId>
<version>1.6.</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
定义User
package org.myshsky.mongodemo; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document; import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set; @Document(collection = "user")
public class User {
public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Date getRegistrationDate() {
return registrationDate;
} public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
} public Set<String> getRoles() {
return roles;
} public void setRoles(Set<String> roles) {
this.roles = roles;
} @Id private String userId;
@NotNull @Indexed(unique = true)
private String username;
@NotNull
private String password;
@NotNull
private String name;
@NotNull
private String email;
@NotNull
private Date registrationDate=new Date();
private Set<String> roles=new HashSet<>();
@PersistenceConstructor
public User(String userId, String username, String password, String name, String email, Date registrationDate, Set<String> roles) {
this.userId = userId;
this.username = username;
this.password = password;
this.name = name;
this.email = email;
this.registrationDate = registrationDate;
this.roles = roles;
} public User() { }
}
查询接口UserRepository
package org.myshsky.mongodemo;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User,String> {
User findByUsername(String username);
}
配置类
package org.myshsky.mongodemo; import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import java.util.ArrayList;
import java.util.List; @Configuration
@EnableMongoRepositories
@PropertySource("classpath:test.properties")
public class TestDataSourceConfig extends AbstractMongoConfiguration {
@Autowired
private Environment env;
@Override
protected String getDatabaseName() {
return env.getRequiredProperty("mongo.name");
} @Override
public Mongo mongo() throws Exception {
ServerAddress serverAddress = new ServerAddress(env.getRequiredProperty("mongo.host"));
List<MongoCredential> credentials=new ArrayList<>();
return new MongoClient(serverAddress,credentials);
}
}
配置文件test.properties
#MongoDb
mongo.host=192.168.31.146:${mongo.port}
mongo.name=local
mongo.port=
单元测试
package org.myshsky.mongodemo; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert; import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set; @RunWith(SpringRunner.class)
@SpringBootTest
public class MongoDemoApplicationTests {
private static Logger logger= LoggerFactory.getLogger(MongoDemoApplicationTests.class);
@Autowired
UserRepository userRepository;
@Before
public void setup(){
Set<String> roles=new HashSet<>();
roles.add("manage");
User user=new User("","user","","name","email@com.cn",new Date(),roles);
userRepository.save(user);
} @Test
public void findAll(){
List<User> users=userRepository.findAll();
Assert.notNull(users);
for(User user:users){
logger.info("===user=== userid:{},username:{},pass:{},registrationDate:{}",
user.getUserId(),user.getName(),user.getPassword(),user.getRegistrationDate());
}
}
}
运行测试

通过客户端查看

Spring Boot + docker +mongo的更多相关文章
- Spring Boot Docker 实战
Spring Boot Docker 开发环境 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_192 maven: ...
- spring boot docker 初尝试
Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口. 在centos7.2下,可以进行这 ...
- Spring Boot使用mongo的GridFS模块
1. GridFS简介 GridFS是Mongo的一个子模块,使用GridFS可以基于MongoDB来持久存储文件.并且支持分布式应用(文件分布存储和读取).作为MongoDB中二进制数据存储在数据库 ...
- Spring Boot + Docker + K8S 简单示例
前言 最近看了看k8s,感觉用这个管理docker确实比自己写一坨脚本进步太多了,简直不是一个次原的东西. 看着k8s的官方文档随手写了个小Demo,一个基于k8s的spring boot服务. 代码 ...
- Spring Boot Docker
1. IDEA中配置Docker Docker默认只接受本地客户端的请求,为了能够远程访问它,首先要开放Docker的监听端口,运行外部应用可以访问 修改 /lib/systemd/system/d ...
- idea spring boot docker 多项目 maven 编译
1,重复的model [INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while proc ...
- java Spring boot Docker打包
https://blog.csdn.net/Stephanie_1/article/details/88831993
- 【原创】Docker容器及Spring Boot微服务应用
Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复杂问题 传统项目实施过程中经常会出现“程序在我这跑得好好的,在你那怎么就不 ...
- Java微服务之Spring Boot on Docker
本文学习前提:Java, Spring Boot, Docker, Spring Cloud 一.准备工作 1.1 安装Docker环境 这一部分请参考我的另一篇文章<ASP.NET Core ...
随机推荐
- 整合Spring+Struts2+Mybatis加spring单元测试等
前言 自己是在CentOS7的IntelliJ IDEA里开发的,里面中文输入法有问题经常用不了,所以这里用了很多chinglish,希望不要介意: 一:pom依赖 <?xml version= ...
- 安卓开机logo和开机动画的几种实现方法
安卓4.2可用方法2-4,第一种方法未验证. 从理论上来说,android 有4个开机启动画面. 第一个应该是U-BOOT的启动画面,有些设备为了满足按动电源即有显示,在UBOOT里加了开机画面,实现 ...
- issubclass ,isinstance,反射
issubclass() 函数 issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类. 语法 以下是 issubclass() 方法的语法: issubc ...
- C#-ado.net学习笔记-会有更新
ado.net 通用类对象.在本地内存暂存数据 托管类对象.让本地通用类对象连接数据库,让本地通用类对象和数据库同步 连接数据库 new connection(connectstring) comma ...
- Hdu2204 Eddy's爱好 2017-06-27 16:11 43人阅读 评论(0) 收藏
Eddy's爱好 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Subm ...
- Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...
- codeforces877c
C. Slava and tanks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [ 9.10 ]CF每日一题系列—— 186A模拟处理字符串
Description: 跟你两个不相同的字符串,问你能否将第一个字符串任意两个字母交换一次使得两字符串相同,YES or NO Solution: 一维模拟就好了 #include <iost ...
- CUDA cufftPlanMany的用法_31通道32*8像素的FFT
#include <cufft.h> #include <iostream> #include <cuda_runtime.h> #include <help ...
- 使用ActionFilterAttribute实现MVC后台授权
授权就是我们在用户未登录的情况下不允许访问一些页面,只有登录后才能进行访问一些页面. 在mvc中我们可以使用ActionFilterAttribute来进行授权验证来阻止一些未经授权的直接访问的页面. ...