28、网关zuul入门二

上一篇讲解了网关zuulpath-url这种转发方式;这一篇讲解一下path-serviceId这种转发方式。path-serviceId这种方式需要使用到注册中心eureka

1、 新建项目sc-zuul-consumer,该项目主要提供一个Controller,两个接口,对应的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>spring-cloud</groupId>

<artifactId>sc-zuul-consumer</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

 

<name>sc-zuul-consumer</name>

<url>http://maven.apache.org</url>

 

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.4.RELEASE</version>

</parent>

 

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

 

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

 

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- 说明是一个 eureka client -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

</dependencies>

</project>

 

 

2、 新建配置文件application.yml

 

server:

  port: 7090

  

spring:

  application:

    name: sc-zuul-consumer

    

eureka:

  client:

    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

    serviceUrl:

      defaultZone: http://localhost:5001/eureka/

 

3、 新建Controller

 

package sc.zuul.consumer.controller;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

import sc.zuul.consumer.model.User;

 

@RestController

public class UserController {

 

@RequestMapping("/user/getUser/{id}")

public Map<String, Object> getUser(@PathVariable Integer id) {

System.out.println("id = " + id);

Map<String, Object> resp = new HashMap<String, Object>();

resp.put("code", "000000");

resp.put("msg", "success");

User u = new User();

u.setId(1);

u.setPosition("cto");

u.setUsername("黄金");

resp.put("body", u);

return resp;

}

 

@RequestMapping("/user/listUser")

public Map<String, Object> listUser() {

Map<String, Object> resp = new HashMap<String, Object>();

resp.put("code", "000000");

resp.put("msg", "success");

User u1 = new User();

u1.setId(1);

u1.setPosition("cto");

u1.setUsername("黄金");

 

User u2 = new User();

u2.setId(2);

u2.setPosition("cto");

u2.setUsername("白银");

 

List<User> list = new ArrayList<User>();

list.add(u1);

list.add(u2);

resp.put("body", list);

 

return resp;

}

 

}

 

 

4、 新建springboot启动类ZuulConsumerApplication.java

 

package sc.zuul.consumer;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

@SpringBootApplication

@EnableEurekaClient

public class ZuulConsumerApplication {

 

public static void main(String[] args) {

SpringApplication.run(ZuulConsumerApplication.class, args);

}

 

}

 

5、 先启动注册中心项目sc-eureka-server,再启动sc-zuul-consumer

 

访问接口:http://127.0.0.1:7090/user/listUser

 

访问接口:http://127.0.0.1:7090/user/getUser/1

 

6、 新建网关项目sc-zuul-towway,对应的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>spring-cloud</groupId>

<artifactId>sc-zuul-towway</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

 

<name>sc-zuul-towway</name>

<url>http://maven.apache.org</url>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.4.RELEASE</version>

</parent>

 

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

 

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

 

<dependencies>

<!-- 说明是一个 eureka client -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

 

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-zuul</artifactId>

</dependency>

<!--

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-zuul</artifactId>

    <version>1.4.6.RELEASE</version>

</dependency>

-->

 

</dependencies>

</project>

 

备注:springboot2.xspring-cloud-starter-zuul被标志为过期

 

 

7、 新建配置文件application.yml

 

server:

  port: 8090

  

spring:

  application:

    name: sc-zuul-towway

    

# 路由规则配置

zuul:

  routes:

    user:

      path: /api/**

      serviceId: sc-zuul-consumer

 

# API网关也将作为一个服务注册到eureka-server上

eureka:

  client:

    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

    serviceUrl:

      defaultZone: http://localhost:5001/eureka/

      

 

8、 新建启动类ZuulApplication.java

 

package sc.zuul.towway;

 

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

@EnableZuulProxy

@EnableEurekaClient

public class ZuulApplication {

 

public static void main(String[] args) {

SpringApplication.run(ZuulApplication.class, args);

}

 

}

 

 

9、 启动项目ZuulApplication.java

 

 

 

10、 验证

访问注册中心

 

访问:http://127.0.0.1:8090/api/user/listUser

 

访问:http://127.0.0.1:8090/api/user/getUser/1

 

 

原文地址:https://www.cnblogs.com/happyhuangjinjin/p/12863078.html