win10 docker 部署微服务
编写好项目后:
改写application.yml
服务注册:
之前是这样的
改写之后我改成了${EUREKA_URL} ,用这个变量替换了它,之前我部署的时候发现用本地的ip好像发现不了服务
路由:
server: port: 8090 servlet: session: cookie: http-only: true spring: application: name: sc-gateway-server redis: database: 0 host: ${REDIS_HOST} port: 6379 password: 123 session: store-type: redis zuul: sensitiveHeaders: #是否开启重试功能 retryable: true prefix: /api routes: spv-auth: path: /auth/** serviceId: spv-auth strip-prefix: false gate: ignore: manageUrls: /auth/spv/login, ribbon: max-auto-retries: 3 max-auto-retries-next-server: 0 eureka: instance: instance-id: ${spring.cloud.client.ip-address}:${server.port} prefer-ip-address: true client: service-url: defaultZone: ${EUREKA_URL} logging: level: org.springframework.cloud.gateway: debug swagger.enabled: true
我的路由用到了redis,redis的host也弄成变量形式
登录验证:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://${MYSQL_HOST}:3306/demo?useUnicode=true&characterEncoding=utf-8 username: root password: q1w2e3r4 jpa: show-sql: true application: name: spv-auth redis: database: 0 host: ${REDIS_HOST} port: 6379 password: 123 jedis: pool: max-active: 8 session: store-type: redis server: port: 8099 eureka: client: serviceUrl: defaultZone: ${EUREKA_URL} hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000
mysql 和 redis 的host也弄成变量的形式
最后打包
我没有在项目里面写Dockerfile 和编排,我把jar都复制到了另一个目录
先拉取redis、 mysql:5.6 、java:8 镜像
目录里面是各种的jar和Dockerfile
Dockerfile:
From java:8 VOLUME /springcloud ADD eureka-server-0.0.1-SNAPSHOT.jar /app.jar EXPOSE 8761 #端口 ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml:
version: '3' services: eureka-service: restart: on-failure build: context: eureka dockerfile: Dockerfile container_name: eureka ports: - "8761:8761" environment: EUREKA_URL: "http://172.19.0.1:8761/eureka/" networks: - eureka-net gateway-service: build: context: gateway dockerfile: Dockerfile container_name: gateway depends_on: - mysql - redis - eureka-service - auth-service ports: - "8090:8090" environment: EUREKA_URL: "http://172.19.0.1:8761/eureka/" REDIS_HOST: "172.19.0.1" networks: - eureka-net auth-service: build: context: auth dockerfile: Dockerfile container_name: auth depends_on: - mysql - redis - eureka-service ports: - "8099:8099" environment: EUREKA_URL: "http://172.19.0.1:8761/eureka/" REDIS_HOST: "172.19.0.1" MYSQL_HOST: "172.19.0.1" networks: - eureka-net mysql: image: mysql:5.6 ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: q1w2e3r4 MYSQL_DATABASE: demo container_name: test-mysql restart: always volumes: - "./mysql/db/:/var/lib/mysql" command: mysqld --lower_case_table_names=1 --default-authentication-plugin=mysql_native_password networks: - eureka-net redis: image: redis:latest ports: - "6379:6379" container_name: test-redis restart: always command: redis-server --requirepass 123 --appendonly yes networks: - eureka-net networks: eureka-net: driver: bridge
究竟 EUREKA_URL、REDIS_HOST、MYSQL_HOST 是怎么确定的呢
我把他们都弄在了一个网络端里面了,因为我也是刚刚学习docker没多久,所以我就这样弄了
我在docker里面创建了一个网络端
docker network create --subnet=172.18.0.0/24 eureka-net
一开始的时候 :EUREKA_URL、REDIS_HOST、MYSQL_HOST 我用的是127.0.0.1 em........
运行:docker-compose up --build
很明显只启动了eureka、redis和mysql
然后我查看了一下网络段看看情况
docker network ls
em......它好像又自动创建了一个网络段,查看一下信息
docker network inspect dockercompose_eureka-net
然后修改docker-compose.yml
把127.0.0.1改为172.19.0.1
就变成这样了,
重新拉取镜像
#先移除 docker-compose rm #再拉取 docker-compose up --bulid
好像getway是红字 但是没有报错信息 看一下注册服务注册了没有
好像可以了。
连接一下redis和mysql
导入表
运行本地前端
进来了
看一下登录注册模块的运行日志
docker logs -f b923813dfaeb
没得问题了,看看其他的方式