0    课程地址

https://coding.imooc.com/lesson/380.html#mid=28552

 

1    浓缩精华
1.1  4.1

 

2    个人关注
2.1  4.1

 

3    课程内容
3.1  搭建网关模块

 

4    代码演练
4.1  搭建网关模块

a  搭建网关子模块

file-->New-->module-->maven-->next-->artifactID为 coupon-gateway-->next-->coupon-gateway...->finish

 

b  子模块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>imooc-coupon</artifactId>
        <groupId>com.imooc.coupon</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <!-- 此处不写版本,版本会以父版本为准-->
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <artifactId>coupon-gateway</artifactId>
    <!-- 模块名及描述信息 -->
    <name>coupon-gateway</name>
    <description>Spring Cloud Gateway For Coupon</description>


    <!-- 需要有的依赖:网关插件,(io,guava 常用依赖,后续用到自加的)-->
    <dependencies>
        <!--
            Eureka 客户端, 客户端向 Eureka Server 注册的时候会提供一系列的元数据信息, 例如: 主机, 端口, 健康检查url等
            Eureka Server 接受每个客户端发送的心跳信息, 如果在某个配置的超时时间内未接收到心跳信息, 实例会被从注册列表中移除
        -->
        <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>
        <!-- apache 工具类 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0</version>
        </dependency>
    </dependencies>

    <!-- 需要有的插件:打包插件-->
    <!--
           SpringBoot的Maven插件, 能够以Maven的方式为应用提供SpringBoot的支持,可以将
           SpringBoot应用打包为可执行的jar或war文件, 然后以通常的方式运行SpringBoot应用
        -->
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    </build>

</project>

 

父模块pom.xml也会自动添加

 <modules>
        <module>coupon-eureka</module>
        <module>coupon-gateway</module>
    </modules>

 

c  网关启动类编写

package com.imooc.coupon;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * GateWayApplicatoin
 * 网关模块启动入口
 * @author 魏豆豆
 * @date 2021/6/1
 *
 * SpringCloudApplication注解点进去
 *  * 1    包含SpirngBootApplication应用注解
 *  * 2    包含EnableDiscoveryClient注解,能够注册和发现服务
 *  * 3    包含EnableCircuitBreaker注解,能够在服务调用超时、异常时发生熔断相关博客参考
 *  * https://zhuanlan.zhihu.com/p/353753774
 */

@EnableZuulProxy
@SpringCloudApplication
public class GateWayApplicatoin {
    public static void main(String [] args){
        SpringApplication.run(GateWayApplicatoin.class,args);
    }
}

 

d  网关配置文件application.yml

#端口9000
server:
  port: 9000

spring:
  application:
    name: coupon-gateway

#注册发现服务地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

 

 

e  测试(分别启动服务端和网关服务)

访问http://127.0.0.1/8000

 

 

 

 

5    问题解决
5.1  服务端启动报错:Cannot execute request on any known server

问题解决:发现服务端模块application.yml用的是:

 

eureka:
instance:
hostname: localhost

 

网关模块yml配置的是

 

eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8000/eureka/

 

将网关模块修改后即可

 

posted on 2021-01-19 07:20  菜鸟乙  阅读(128)  评论(0编辑  收藏  举报