先说明一下docker需要搭建的微服务的基本情况:
- 项目情况:一个demo_parent项目,下面三个子模块:demo_eureka(eureka服务)、demo_article(文章服务)、demo_zuul(网关服务)。
- 用到的资源有:jdk1.8、mysql5.7。
- 需要用到的docker容器有:mysql(mysql单独一个容器)、jdk1.8(article、eureka、zuul三个服务运行在该jdk1.8容器中)
一,用idea新建父项目
File->New->Project->Maven,选择Project SDK为jdk8->GroupId:com.demo,ArtifactId:demo_parent
修改pom.xml文件,完整如下:
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.demogroupId> <artifactId>demo_parentartifactId> <packaging>pompackaging> <version>1.0-SNAPSHOTversion>
<parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.0.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <lombok.version>1.16.10lombok.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>Finchley.M9version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency>
<dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>${lombok.version}version> dependency> dependencies> <repositories> <repository> <id>spring-snapshotsid> <name>Spring Snapshotsname> <url>https://repo.spring.io/snapshoturl> <snapshots> <enabled>trueenabled> snapshots> repository> <repository> <id>spring-milestonesid> <name>Spring Milestonesname> <url>https://repo.spring.io/milestoneurl> <snapshots> <enabled>falseenabled> snapshots> repository> repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshotsid> <name>Spring Snapshotsname> <url>https://repo.spring.io/snapshoturl> <snapshots> <enabled>trueenabled> snapshots> pluginRepository> <pluginRepository> <id>spring-milestonesid> <name>Spring Milestonesname> <url>https://repo.spring.io/milestoneurl> <snapshots> <enabled>falseenabled> snapshots> pluginRepository> pluginRepositories> project>
二、新增demo_eureka模块,并编写代码
右键demo_parent->new->Module->Maven,选择Module SK为jdk8->ArtifactId:demo_eureka
1.修改pom.xmlxml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>demo_parentartifactId> <groupId>com.demogroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>demo_eurekaartifactId> <dependencies>
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> dependency> dependencies> project>
2.在resources目录下新增application.yml文件
server: port: 7000 #eureka的启动端口,可自定义 spring: application: name: demo-eureka #eureka项目名称,可自定义 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://127.0.0.1:${server.port}/eureka #在IDEA中运行时使用127.0.0.1,部署发布时,请修改为虚拟机宿主机的ip地址
注:由于这里讲的是单机部署,所以eureka的register-with-eureka和fetch-registry都设置为了false
3.新建com.demo.eureka包,并在该包下新建启动类EurekaApplication
package com.demo.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 注册中心微服务
*/
// 标注为一个spring boot项目 @SpringBootApplication
// 标注为Eureka服务端 @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
4.右键EurekaApplication,点Run 'EurekaApplication'启动该demo-eureka项目
在浏览器输入localhost:7000,看到如上界面,表示项目启动成功。No instances available表示暂时还没有服务注册到eureka上来。
docker微服务部署之:二、搭建文章微服务项目