Location>code7788 >text

nacos configuration & gateway configuration service discovery keeps reporting 500

Popularity:706 ℃/2024-07-31 15:25:06

Project Scene:

These two days have not been in the process of simplifying the configuration, the use of public configuration, my services can be accessed through the gateway to these tasks, but also constantly stepping on the pit to make up for the knowledge of these tasks are finally good, the following is a record of the process of encountering the problem.


Using Public Configuration

Because the project is found to use too many configuration files, there are application,, remote nacos configuration, I want to try to make it simpler to put all the configuration on the line, the local just to make a distinction, and then they make up the difference between application and bootstrap!

Application and bootstrap differences and priorities

Order: > > >
Priority:properties>yml
File location priority:
config folder in src>> root folder>> config folder in resource>> under resource
Summary:
For the same yml, bootstrap has a higher priority than application and is loaded by the parent context, while application is loaded by the child
bootstrap is generally configured bootstrap configuration, connected to the spring cloud configuration center, the default local can not override the remote configuration, remote configuration of some encrypted information


Using Public Configuration

Go straight to the configuration code, and note thatcap (a poem)Inside the parameter

chixxxxx:
  nacos:
    server-addr: 123.123.000.000:8848
spring:
  profiles:
    active: test
  application:
    name: chixxxxx-gateway
    group: chixxxxx_GROUP
  cloud:
    nacos:
      discovery:
        group: ${}
        namespace: ${}
        server-addr: ${-addr}
      config:
        group: ${}
        namespace: ${}
        server-addr: ${-addr}
        prefix: ${}
        shared-configs:
          - data-id: 
            group: ${}
        extension-configs:
          - data-id: 
            group: ${}


Configuring Gateway Services

My previous company was matched with a gateway, in fact, is to specify the service + assertion of the path on the line, but this side is still a little different, first of all, over there, the first to do a match path forwarding to the gateway of an operation, as follows

location /api/backend/ {
                proxy_pass http://localhost:8001;
            }

Advance forwarding to the gateway, then gateway configuration

- id: platformback-end interface
        uri: lb://chixxxx-platform
        order: 1000
        predicates:
          - Path=/api/backend/**
        filters:
          - StripPrefix=0
          - name: RequestRateLimiter
            args:
              key-resolver: '#{@hostAddrKeyResolver}'
              redis-rate-limiter:
                replenishRate: 20
                burstCapacity: 50

Of particular note here is theStripPrefixthis parameter, did not pay too much attention before, but also made a 404, read some explanations to realize that this is to remove the path I started requesting the characters, such as: StripPrefix = 1, then the request to the service is backend/user/detail?id = 1; StripPrefix = 2, user/detail?id = 1, the number represents the removal of several / split string. This number represents the string with several / splits removed.

Request service forwarding failure

What is this again, I requested the url as usual, but it returned 500, then I checked the logs

ERROR 1399157 [reactor-http-epoll-4]  [bfc53f9d-5959]  500 Server Error for HTTP GET "/api/backend/appVersion/detail?id=12"

is a server error, I then look at the logs on the target server, there is no new logs, and then because of the error message is limited, I spent a day before and after the last by "synonymous with a thousand questions" to find the inspiration - a good look at the state of the service is normal, open: the service list - service details, to see the state of health is ture ah! I saw that the health status is ture ah, then why report 500, and then look at the ip address, the ip is written: 172.17.0.5 this seems to docker or intranet address ah, at first suspected that the intranet, but think about it will not be ah, my server in the outside, and then go to the target server to execute:docker inspect xxxI see that this is it, oh ~ the original docker assigned ip registered to nacos, I should be to register the server ip, through a thousand questions need to set up

spring:
  cloud:
    nacos:
      discovery:
        ip: 123.123.123.123

Then re-send it, then go to the details and see that the ip has changed, and finally request it again via postman and it's accessible via the gateway address


wrap-up

Recently this week to do these tasks are my own initiative to do, because I saw the configuration is more chaotic now the number of users is relatively small can be folded, to avoid the later more people and then folded loss is relatively large, at the same time on the gateway this piece of knowledge to check the gaps have also improved accordingly.