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