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/

 

 截图如下:

 

 

 

 

 

 

.

posted @ 2018-11-10 20:39  诚信天下  阅读(921)  评论(0编辑  收藏  举报