docker微服务部署之:二、搭建文章微服务项目
一、新增demo_eureka模块,并编写代码
右键demo_parent->new->Module->Maven,选择Module SK为jdk8->ArtifactId:demo_zuul
1.修改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 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 <parent>
6 <artifactId>demo_parent</artifactId>
7 <groupId>com.demo</groupId>
8 <version>1.0-SNAPSHOT</version>
9 </parent>
10 <modelVersion>4.0.0</modelVersion>
11
12 <artifactId>demo_zuul</artifactId>
13
14 <dependencies>
15 <!-- 服务网关依赖 -->
16 <dependency>
17 <groupId>org.springframework.cloud</groupId>
18 <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
19 </dependency>
20 <!-- eureka客服端依赖 -->
21 <dependency>
22 <groupId>org.springframework.cloud</groupId>
23 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
24 </dependency>
25 </dependencies>
26</project>
27
2.在resources目录下新增application.yml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1server:
2 port: 8888
3spring:
4 application:
5 name: demo-zuul
6zuul:
7 routes: # 配置路由
8 app: # 路由名称,这是一个标识作用,只要是[a-z]可随意,没有什么影响
9 path: /myarticle/* #匹配以/myarticle路径开头的所有路径
10 serviceId: demo-article #只要符合path规则,则转发到demo-article微服务
11eureka:
12 client:
13 fetch-registry: true
14 register-with-eureka: true
15 service-url:
16 defaultZone: http://127.0.0.1.181:7000/eureka #在IDEA中运行时使用127.0.0.1,部署发布时,请修改为虚拟机宿主机的ip地址
17 instance:
18 prefer-ip-address: true
19
3.新建com.demo.zuul包,并在该包下新建启动类ZuulApplication
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1package com.demo.zuul;
2
3import org.springframework.boot.SpringApplication;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
7
8/**
9 * 微服务网关
10 */
11@SpringBootApplication
12// 标注启动zuul网关代理
13@EnableZuulProxy
14// 标注eureka客户端
15@EnableEurekaClient
16public class ZuulApplication {
17 public static void main(String[] args) {
18 SpringApplication.run(ZuulApplication.class, args);
19 }
20}
21
4.运行ZuulApplication启动类
刷新eureka界面,可以看到有一个名为DEMO-ZUUL的服务已经注册上来了
5.测试zuul网关代理是否成功
docker微服务部署之:四、安装docker、docker中安装mysql和jdk1.8、手动构建镜像、部署项目