{"id":1951,"date":"2022-11-01T00:03:50","date_gmt":"2022-10-31T16:03:50","guid":{"rendered":"https:\/\/www.tra56.com\/?p=1951"},"modified":"2022-11-01T00:03:50","modified_gmt":"2022-10-31T16:03:50","slug":"1951","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/1951.html","title":{"rendered":"Docker Compose\u90e8\u7f72\u5fae\u670d\u52a1\u9879\u76ee\u4e0a\u7ebf\u529f\u80fd"},"content":{"rendered":"

<\/p>\n

\u76ee\u5f55<\/h2>\n
\n
  • \n

    \u4e00\u3001\u9700\u6c42\u8bf4\u660e<\/a><\/p>\n<\/li>\n

  • \n

    \u4e8c\u3001\u6548\u679c\u56fe<\/a><\/p>\n<\/li>\n

  • \n

    \u4e09\u3001\u9879\u76ee\u7ed3\u6784<\/a><\/p>\n<\/li>\n

  • \n

    \u56db\u3001\u6838\u5fc3\u6e90\u7801<\/a><\/p>\n<\/li>\n<\/ul>\n

  • \n

    ♻️Java\u4f9d\u8d56\u4e0e\u63a5\u53e3<\/a><\/p>\n<\/li>\n

  • \n

    ♨️Docker\u76f8\u5173\u6e90\u7801<\/a><\/p>\n<\/li>\n<\/ul>\n

  • \n

    \u4e94\u3001\u90e8\u7f72\u9879\u76ee<\/a><\/p>\n<\/li>\n

  • \n

    ⛵\u5c0f\u7ed3<\/a><\/p>\n<\/li>\n

    <\/ul>\n

    <\/a><\/p>\n

    \u4e00\u3001\u9700\u6c42\u8bf4\u660e<\/h2>\n

    \u7f16\u5199\u4e00\u4e2aSpringBoot + Redis \u7684\u5fae\u670d\u52a1\u9879\u76ee\uff0c\u5e76\u63d0\u4f9b hello\u63a5\u53e3\uff0c\u6bcf\u8bbf\u95ee\u4e00\u6b21\u63a5\u53e3\uff0c\u8ba1\u6570\u5668+1<\/p>\n

    <\/a><\/p>\n

    \u4e8c\u3001\u6548\u679c\u56fe<\/h2>\n

    \"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"<\/p>\n

    <\/a><\/p>\n

    \u4e09\u3001\u9879\u76ee\u7ed3\u6784<\/h2>\n

    \"image.png\"\/<\/p>\n

    \u76ee\u5f55\u8bf4\u660e<\/strong><\/p>\n

    docker-compose.yml<\/strong> \uff1a\u9879\u76ee\u7684\u542f\u52a8\u6587\u4ef6\uff0c\u914d\u7f6e\u7f16\u6392\u7b49<\/p>\n

    Dockerfile<\/strong>\uff1a\u9879\u76ee\u4e0a\u7ebf\u6240\u9700\u8981\u7684\u4f9d\u8d56\uff0c\u4ee5\u53ca\u542f\u52a8\u65b9\u5f0f<\/p>\n

    <\/a><\/p>\n

    \u56db\u3001\u6838\u5fc3\u6e90\u7801<\/h2>\n

    <\/a><\/p>\n

    ♻️Java\u4f9d\u8d56\u4e0e\u63a5\u53e3<\/h3>\n
    \n

    \u4f9d\u8d56\u6587\u4ef6<\/p>\n<\/blockquote>\n

    pom.xml<\/p>\n

    <?xml version="1.0" encoding="utf-8"?>\r\n<project xmlns="http:\/\/maven.apache.org\/POM\/4.0.0"\r\n         xmlns:xsi="http:\/\/www.w3.org\/2001\/XMLSchema-instance"\r\n         xsi:schemaLocation="http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd">\r\n    <modelVersion>4.0.0<\/modelVersion>\r\n    <parent>\r\n        <groupId>org.springframework.boot<\/groupId>\r\n        <artifactId>spring-boot-starter-parent<\/artifactId>\r\n        <version>2.5.5<\/version>\r\n        <relativePath\/> <!-- lookup parent from repository -->\r\n    <\/parent>\r\n    <groupId>org.example<\/groupId>\r\n    <artifactId>demo<\/artifactId>\r\n    <version>1.0-SNAPSHOT<\/version>\r\n\r\n    <dependencies>\r\n        <dependency>\r\n            <groupId>org.springframework.boot<\/groupId>\r\n            <artifactId>spring-boot-starter-web<\/artifactId>\r\n        <\/dependency>\r\n\r\n        <dependency>\r\n            <groupId>org.springframework.boot<\/groupId>\r\n            <artifactId>spring-boot-starter-data-redis<\/artifactId>\r\n            <exclusions>\r\n                <exclusion>\r\n                    <groupId>io.lettuce<\/groupId>\r\n                    <artifactId>lettuce-core<\/artifactId>\r\n                <\/exclusion>\r\n            <\/exclusions>\r\n        <\/dependency>\r\n        <dependency>\r\n            <groupId>redis.clients<\/groupId>\r\n            <artifactId>jedis<\/artifactId>\r\n        <\/dependency>\r\n\r\n    <\/dependencies>\r\n\r\n    <build>\r\n        <plugins>\r\n            <plugin>\r\n                <groupId>org.springframework.boot<\/groupId>\r\n                <artifactId>spring-boot-maven-plugin<\/artifactId>\r\n            <\/plugin>\r\n        <\/plugins>\r\n\r\n        <resources>\r\n            <resource>\r\n                <directory>src\/main\/java<\/directory>\r\n                <includes>\r\n                    <include>**\/*.xml<\/include>\r\n                <\/includes>\r\n                <filtering>false<\/filtering>\r\n            <\/resource>\r\n            <resource>\r\n                <directory>src\/main\/resources<\/directory>\r\n            <\/resource>\r\n            <resource>\r\n                <directory>libs\/<\/directory>\r\n                <targetPath>libs<\/targetPath>\r\n                <includes>\r\n                    <include>**\/*.jar<\/include>\r\n                <\/includes>\r\n            <\/resource>\r\n        <\/resources>\r\n    <\/build>\r\n\r\n<\/project><\/pre>\n
    \n

    \u63a5\u53e3<\/p>\n<\/blockquote>\n

    HelloController<\/p>\n

    package com.wanshi.controller;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.data.redis.core.StringRedisTemplate;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\/**\r\n * @author whc\r\n * @date 2022\/6\/9 10:06\r\n *\/\r\n@RestController\r\npublic class HelloController {\r\n    @Autowired\r\n    private StringRedisTemplate redisTemplate;\r\n    @GetMapping("\/hello")\r\n    public String hello() {\r\n        Long views = redisTemplate.opsForValue().increment("views");\r\n        return "hello, xiaowang, views:" + views;\r\n    }\r\n}<\/pre>\n
    \n

    \u914d\u7f6e\u6587\u4ef6<\/strong><\/p>\n<\/blockquote>\n

    application.yml<\/strong><\/p>\n

    server:\r\n  port: 8080\r\nspring:\r\n  redis:\r\n    host: redis<\/pre>\n

    ♨️Docker\u76f8\u5173\u6e90\u7801<\/h3>\n

    Dockerfile<\/p>\n

    FROM java:8\r\nCOPY *.jar \/app.jar\r\nCMD ["--server.port=8080"]\r\nEXPOSE 8080\r\nENTRYPOINT ["java", "-jar", "\/app.jar"]<\/pre>\n

    docker-compose.yml<\/span><\/p>\n

    version: '3.8'\r\nservices:\r\n  xiwoangapp:\r\n    build: .\r\n    image: xiaowangapp\r\n    depends_on:\r\n      - redis\r\n    ports:\r\n      - "8080:8080"\r\n  redis:\r\n    image: "redis:3.0.7"<\/pre>\n

    \u4e94\u3001\u90e8\u7f72\u9879\u76ee<\/h2>\n

    \u6253\u5305\u540e\u7aef\u9879\u76ee\u901a\u8fc7\u547d\u4ee4<\/p>\n

    mvn clean package<\/pre>\n

    jar\u5305\u4e0e\u914d\u7f6e\u6587\u4ef6\u4e0a\u4f20\u81f3Linux\u670d\u52a1\u5668\uff0c\u65b0\u5efa\u6307\u5b9a\u6587\u4ef6\u5939\uff08\u901a\u8fc7Filezilla\u4e0a\u4f20\u6587\u4ef6\uff09<\/span><\/p>\n

    \"image.png\"\/<\/p>\n

    \u6267\u884c\u547d\u4ee4\u542f\u52a8<\/span><\/p>\n

    docker-compose up<\/pre>\n

    \"image.png\"\/<\/p>\n

    \u90e8\u7f72\u6210\u529f\u540e\u6211\u4eec\u67e5\u770b\u670d\u52a1\u662f\u5426\u542f\u52a8<\/span><\/p>\n

    docker ps<\/pre>\n

    \"image.png\"\/<\/p>\n

    \u672c\u673a\u8fdb\u884c\u8bbf\u95ee<\/span><\/p>\n

    curl localhost:8080\/hello<\/pre>\n

    \"image.png\"\/<\/p>\n

    <\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Docker Compose\u90e8\u7f72\u5fae\u670d\u52a1\u9879\u76ee\u4e0a\u7ebf,\u9700\u6c42\u662f\u8fd9\u6837\u7684\u7f16\u5199\u4e00\u4e2aSpringBoot + Redis \u7684\u5fae\u670d\u52a1\u9879\u76ee\uff0c\u5e76\u63d0\u4f9b hello\u63a5\u53e3\uff0c\u6bcf\u8bbf\u95ee\u4e00\u6b21\u63a5\u53e3\uff0c\u8ba1\u6570\u5668+1<\/p>\n","protected":false},"author":1,"featured_media":1953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,18,124],"tags":[],"class_list":["post-1951","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","category-java","category-springboot"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1951"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=1951"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1951\/revisions"}],"predecessor-version":[{"id":1954,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1951\/revisions\/1954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media\/1953"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=1951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=1951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=1951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}