时间:2021-07-01 10:21:17 帮助过:8人阅读
2) DemoApplication.java
@SpringBootApplication public class DemoApplication { public static void main(String... args) { SpringApplication.run(DemoApplication.class, args); } }
3) RedisConfig.java
@Configuration public class RedisConfig { @Autowired private RedisConnectionFactory connectionFactory; @Bean public RedisTemplate<String, Integer> redisTemplate() { RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>(); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } }
4) CommandController.java
@RestController public class CommandController { @Autowired private CommandService commandService; @GetMapping("/commands") @ResponseBody public List<Command> getCommands() { return commandService.getCommands(); } }
5) RedisController.java
@RestController public class RedisController { @Autowired private RedisTemplate<String, Integer> redisTemplate; @GetMapping("/redis") @ResponseBody public String getValue(HttpServletRequest request) { String localAddress = request.getLocalAddr(); Integer counter = redisTemplate.opsForValue().get("counter"); if(counter == null) { counter = 0; } counter++; redisTemplate.opsForValue().set("counter", counter); return localAddress + " => " + counter; } }
6) CommandDao.java
@Mapper public interface CommandDao { @Select("select id, command, description from command order by id") List<Command> getCommands(); }
7) Command.java
public class Command { private int id; private String command; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return command; } public void setName(String name) { this.command = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
8) CommandService.java
@Service public class CommandService { @Autowired private CommandDao commandDao; public List<Command> getCommands() { return commandDao.getCommands(); } }
9) application-compose.yml (mysql是指docker-compose.yml里的mysql服务名)
server.port: 10101 spring: datasource: url: jdbc:mysql://mysql:3306/compose?serverTimezone=GMT%2B8 username: boot password: boot123 redis: host: redis port: 6379 password: redis123
10) application-dev.yml (本地开发时用)
server.port: 10101 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/compose?serverTimezone=GMT%2B8 username: root password: redis: host: 127.0.0.1 port: 6379 password: redis123
11) application.yml (开发时用)
spring.profiles.active: dev
12) build.bat 这个文件稍加说明一下:使用这个脚本,是为了方便。只要鼠标双击这个文件即可在同一文件夹里生成 jar文件。
如果不用这个脚本,要在pom.xml所在目录, cmd里运行 mvn package, 生成的jar会在 target 文件夹里,喜欢用哪个自己选。
del target\compose-demo-1.0.jar call mvn package copy target\compose-demo-1.0.jar . pause
13) compose.sql 初始化数据库用,这个在第二部分讲
-- ---------------------------- -- Create database -- ---------------------------- DROP DATABASE IF EXISTS compose; CREATE DATABASE compose DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; use compose; SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for command -- ---------------------------- DROP TABLE IF EXISTS `command`; CREATE TABLE `command` ( `id` int(11) NOT NULL AUTO_INCREMENT, `command` varchar(30) DEFAULT NULL, `description` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of command -- ---------------------------- INSERT INTO `command` VALUES (‘1‘, ‘build‘, ‘Build or rebuild services‘); INSERT INTO `command` VALUES (‘2‘, ‘bundle‘, ‘Generate a Docker bundle from the Compose file‘); INSERT INTO `command` VALUES (‘3‘, ‘config‘, ‘Validate and view the Compose file‘); INSERT INTO `command` VALUES (‘4‘, ‘create‘, ‘Create services‘); INSERT INTO `command` VALUES (‘5‘, ‘down‘, ‘Stop and remove containers, networks, images, and volumns‘); INSERT INTO `command` VALUES (‘6‘, ‘events‘, ‘Receivve real time events from containers‘); INSERT INTO `command` VALUES (‘7‘, ‘exec‘, ‘Excecute a command in a running container‘); INSERT INTO `command` VALUES (‘8‘, ‘help‘, ‘Get help on a command‘);
14) docker-compose.yml 第三部分讲
version: ‘3‘ services: nginx: image: nginx:1.15 volumes: - ./:/etc/nginx/conf.d ports: - 80:80 - 443:443 links: - spring-boot-1 - spring-boot-2 depends_on: - spring-boot-1 - spring-boot-2 mysql: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_HOST: ‘%‘ MYSQL_ROOT_PASSWORD: root123 MYSQL_DATABASE: compose MYSQL_USER: boot MYSQL_PASSWORD: boot123 redis: image: redis container_name: my_redis command: redis-server --requirepass redis123 ports: - "6379:6379" spring-boot-1: image: compose-demo:1.0 # build: . # ports: # - "10101:10101" expose: - "10101" restart: always depends_on: - mysql - redis spring-boot-2: image: compose-demo:1.0 # build: . expose: - "10101" restart: always depends_on: - mysql - redis volumes: db_data:
15) Dockerfile
FROM java:8 COPY compose-demo-1.0.jar compose-demo-1.0.jar ENV JAVA_OPTS="" ENTRYPOINT exec java $JAVA_OPTS -Dspring.profiles.active=compose -jar compose-demo-1.0.jar
16) nginx.conf
upstream boots { server spring-boot-1:10101; server spring-boot-2:10101; } server { listen 80; charset utf-8; access_log off; location / { proxy_pass http://boots; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
3. 测试
先用compose.sql 生成相应的数据库。 然后本地运行一下。
4. 打包
为了
好了,本篇基本上是boot应用相关。 docker相关的部分在第二部分中讲解。
docker-compose + spring boot + mysql + redis + nginx 发布web应用: part 1
标签:pen 有一个 down eal tab pause undle strong src