gateway通过uri的lb调服务id掉不到服务

282 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。 POM file dependency

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!--gateway fhadmin.org-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
å        <version>3.0.2</version>
    </dependency>
    <!--spring-boot fhadmin.org-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

The related dependencies of Nacos I added in the parent component are as follows:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${nacos.version}</version>
</dependency>
<!--alibaba fhadmin.org-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>${nacos.version}</version>
</dependency>

The version of Nacos is as follows:

<properties>
    <nacos.version>2021.1</nacos.version>
</properties>

The application.yml file of gateway is configured as follows:

#fhadmin.org
server:
  port: 9040

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer
          #          uri: http://localhost:9010
          predicates:
            - Path=/**
    nacos:
      discovery:
        server-addr: localhost:8848
        metadata:
          preserved.heart.beat.interval: 3 
          preserved.heart.beat.timeout: 6 
          preserved.ip.delete.timeout: 9 

When I pass URI: http://localhost:9010 When calling a service, it can be called, but when I use URI LB:// consumer, I cannot call the service, and an error 503 is reported

The solution is: Add feign dependency.

``xml org.springframework.boot spring-boot-starter

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>3.0.2</version>
</dependency>

<!--fegin fhadmin.cn-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.2</version>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
    <version>3.0.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
```

1.跳过密码启动mysql

service mysqld start --skip-grant-tables 启动mysql 开启跳过密码 也可以配置文件:加入skip-grant-tables

2.修改密码

update mysql.user set authentication_string=password('123456') where user='root';

安装完成之后没有启动,用ps -ef|grep mysql 发现没有进程,重新启动电脑,然后就好了。

3.初次使用的时候提示:You must reset your password using ALTER USER statement before executing this statement. 解决办法如下:

SET PASSWORD = PASSWORD('你的新密码'); ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; FLUSH PRIVILEGES;

    public Map<String, List<Object>> FunctionName(){
        long l = System.currentTimeMillis();
        Map<String, List<Object>> list= service.serviceFunction();
        System.out.println("消耗时长:" + (System.currentTimeMillis() -  l ));
        return list;
    }

解释:

业务开始前获得系统的时间,单位为毫秒 long l = System.currentTimeMillis();

业务结束后,在此调用这个方法,减去开始前的 l,差值即为业务消耗的总毫秒数。

优化小总结:

数据库层优化:

数据库查询优化DB(降低查询次数 降低查询复杂度 优化sql语句等) 增加事宜的索引提升查询效率

借助Nosql:

复杂查询,实时性要求不高数据,借助非关系型数据库(Nosql)存储

优化逻辑:

优化算法逻辑(降低算法复杂度) 避免循环操作数据库

静态资源:

能静态化的做静态化 动静分离 静态文件压缩 如图片 js css等压缩 对于开源静态可以引用CDN加速

中间件优化:

降低中间件损耗 如:静态化后让nginx直接处理

提升中间件的性能配置 如:nginx gateway等配置优化 合理配置并发数 等待时长 合理配置日志级别

测试环境: 笔记本1(centOS)作为Nginx服务 笔记本2全部其他服务 额外开两个浏览器 页面若干 360 钉钉 微信 Jvisualvm IDEA编译器 xshell Nvicat Notepad++

压测内容压测线程数吞吐量KB/S(越大越好)90%响应时间MS(越小越好)99%(越小越好)
nginx50103466133
gateway503096.71275
简单任务50296040142
gateway+简单任务50810171391
nginx+gateway+简单任务5093.91833063

nginx:测试nginx默认页面 gateway:测试网关88 404页面 简单任务:一个服务返回字符串 hello gateway+简单任务:通过网关访问服务返回字符串 hello nginx+gateway+简单任务:nginx代理网关 网关访问服务返回字符串 hello