先说明一下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文件,完整如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.demo</groupId>
8 <artifactId>demo_parent</artifactId>
9 <packaging>pom</packaging>
10 <version>1.0-SNAPSHOT</version>
11
12 <!-- spring boot parent-->
13 <parent>
14 <groupId>org.springframework.boot</groupId>
15 <artifactId>spring-boot-starter-parent</artifactId>
16 <version>2.0.0.RELEASE</version>
17 <relativePath/>
18 </parent>
19
20 <properties>
21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23 <java.version>1.8</java.version>
24 <lombok.version>1.16.10</lombok.version>
25 </properties>
26
27 <dependencyManagement>
28 <dependencies>
29 <dependency>
30 <groupId>org.springframework.cloud</groupId>
31 <artifactId>spring-cloud-dependencies</artifactId>
32 <version>Finchley.M9</version>
33 <type>pom</type>
34 <scope>import</scope>
35 </dependency>
36 </dependencies>
37 </dependencyManagement>
38
39 <dependencies>
40 <dependency>
41 <groupId>org.springframework.boot</groupId>
42 <artifactId>spring-boot-starter-web</artifactId>
43 </dependency>
44 <dependency>
45 <groupId>org.springframework.boot</groupId>
46 <artifactId>spring-boot-starter-test</artifactId>
47 <scope>test</scope>
48 </dependency>
49 <!-- 引入lombok依赖,可以在使用了@Data注解的类中,不用写getter和setter方法 -->
50 <dependency>
51 <groupId>org.projectlombok</groupId>
52 <artifactId>lombok</artifactId>
53 <version>${lombok.version}</version>
54 </dependency>
55 </dependencies>
56
57 <repositories>
58 <repository>
59 <id>spring-snapshots</id>
60 <name>Spring Snapshots</name>
61 <url>https://repo.spring.io/snapshot</url>
62 <snapshots>
63 <enabled>true</enabled>
64 </snapshots>
65 </repository>
66 <repository>
67 <id>spring-milestones</id>
68 <name>Spring Milestones</name>
69 <url>https://repo.spring.io/milestone</url>
70 <snapshots>
71 <enabled>false</enabled>
72 </snapshots>
73 </repository>
74 </repositories>
75 <pluginRepositories>
76 <pluginRepository>
77 <id>spring-snapshots</id>
78 <name>Spring Snapshots</name>
79 <url>https://repo.spring.io/snapshot</url>
80 <snapshots>
81 <enabled>true</enabled>
82 </snapshots>
83 </pluginRepository>
84 <pluginRepository>
85 <id>spring-milestones</id>
86 <name>Spring Milestones</name>
87 <url>https://repo.spring.io/milestone</url>
88 <snapshots>
89 <enabled>false</enabled>
90 </snapshots>
91 </pluginRepository>
92 </pluginRepositories>
93</project>
94
二、新增demo_eureka模块,并编写代码
右键demo_parent->new->Module->Maven,选择Module SK为jdk8->ArtifactId:demo_eureka
1.修改pom.xml
<?
xml version="1.0" encoding="UTF-8"
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1<project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <parent>
5 <artifactId>demo_parent</artifactId>
6 <groupId>com.demo</groupId>
7 <version>1.0-SNAPSHOT</version>
8 </parent>
9 <modelVersion>4.0.0</modelVersion>
10
11 <artifactId>demo_eureka</artifactId>
12
13
14 <dependencies>
15 <!-- eureka 服务端依赖 -->
16 <dependency>
17 <groupId>org.springframework.cloud</groupId>
18 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
19 </dependency>
20 </dependencies>
21</project>
22
2.在resources目录下新增application.yml文件
1
2
3
4
5
6
7
8
9
10
11
12 1server:
2 port: 7000 #eureka的启动端口,可自定义
3spring:
4 application:
5 name: demo-eureka #eureka项目名称,可自定义
6eureka:
7 client:
8 register-with-eureka: false
9 fetch-registry: false
10 service-url:
11 defaultZone: http://127.0.0.1:${server.port}/eureka #在IDEA中运行时使用127.0.0.1,部署发布时,请修改为虚拟机宿主机的ip地址
12
注:由于这里讲的是单机部署,所以eureka的register-with-eureka和fetch-registry都设置为了false
3.新建com.demo.eureka包,并在该包下新建启动类EurekaApplication
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1package com.demo.eureka;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6
7/**
8 * 注册中心微服务
9 */
10// 标注为一个spring boot项目
11@SpringBootApplication
12// 标注为Eureka服务端
13@EnableEurekaServer
14public class EurekaApplication {
15 public static void main(String[] args) {
16 SpringApplication.run(EurekaApplication.class, args);
17 }
18}
19
4.右键EurekaApplication,点Run 'EurekaApplication'启动该demo-eureka项目
在浏览器输入localhost:7000,看到如上界面,表示项目启动成功。No instances available表示暂时还没有服务注册到eureka上来。
docker微服务部署之:二、搭建文章微服务项目