Zuul 网关搭建

 

本机IP为  192.168.1.102

 

1.    新建 Maven 项目   zuul

 

2.   pom.xml

<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.0</modelVersion>
    <groupId>com.java</groupId>
    <artifactId>zuul</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>

    <!-- 配置版本常量 -->
    <properties>
        <jdk.version>1.8</jdk.version>
        <spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>${spring.cloud.version}</version>
        </dependency>

        <!-- 在Eureka服务注册中心,完善显示关于微服务信息 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>$</delimit>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 

3.   application.yml

server: 
  port: 10000
 
spring: 
  application:
    name: zuul
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://192.168.1.102:8080/eureka   
  instance:
    instance-id: zuul.java.com
    prefer-ip-address: true 
 
 
zuul: 
  #prefix: /java
  ignored-services: "*"
  #ignored-services: microservice
  routes: 
    microservice.serviceId: microservice
    microservice.path: /service/**
 
info:
  app.name: zuul
  app.update: 2018-11-10
  build.groupId: $project.groupId$
  build.artifactId: $project.artifactId$
  build.version: $project.version$

 

 

4.   ZuulStarter.java

package com.java.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@SpringBootApplication
public class ZuulStarter extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(ZuulStarter.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ZuulStarter.class);
    }

}

 

 

5.    运行测试

启动  Eureka   服务注册中心,参考  https://www.cnblogs.com/jonban/p/eureka.html

启动  MicroService  微服务, 参考   https://www.cnblogs.com/jonban/p/microservice.html

启动  Zuul服务,运行 ZuulStarter.java

 

浏览器输入URL

http://192.168.1.102:10000/service/getHostMessage/hello

 

返回数据如下:

{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}

 

截图如下:

 

搭建成功!

 

 

查看Eureka  服务注册情况,输入Eureka服务URL

http://192.168.1.102:8080/

 

 截图如下:

 

 

 

 

 

 

.

转载于:https://www.cnblogs.com/jonban/p/zuul.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值