Tag Archives: JAVA related

Solution to the problem of using Alibaba gateway but unable to route successfully

One of the important functions of gateway is routing and forwarding. We often encounter the problem of forwarding failure
for example, the front-end configuration is as follows:

 // api interface request address
 window.SITE_CONFIG['baseUrl'] = 'http://localhost:90/api';

The application.yml configuration of gateway is as follows:

server:
  port: 90
spring:
  application:
    name: threat-gateway
  cloud:
    gateway:
      routes:
#        threat-ip microservice routing and forwarding
        - id: ip_route
# The forwarding address of the matching route
          uri: lb://threat-ip
          predicates:
            - Path=/api/cyber_threat_ip/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

The reason for the error may be that the registry (such as Nacos) is not enabled, resulting in the unrecognized name of the microservice
it is revised as follows:

#          Match the forwarding address of the route (8082 is the threat-ip microservice port)
          uri: localhost:8082

As a result, an error was reported because http://, should be added in front of the URI, as follows:

#          Match the forwarding address of the route (8082 is the threat-ip microservice port)
          uri: http://localhost:8082

As a result, you can visit it

of course, the rewritepath path rewriting error in filters may also cause access error. Other reasons will be added later.

Zuul gateway routing URL and service ID configuration

there are two routing configurations in zuul:

1. Map

by accessing IP and port Numbers

2. Map

by service name

based on the code in the previous section:

first I cluster both service providers and service consumers:

first configure the first:

change gateway application.yml

server:
  port: 8090
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
spring:
  application:
    name: zuul-service
zuul:
  routes:
    feign:
      path: /feign/**
      url: http://localhost:8085/

accesses the path path through path and url mapping and accesses the url directly to the address corresponding to the url, which is obviously not good, because the address of the service must be known, and the number of clusters may be dynamically extended, which only realizes proxy forwarding and cannot realize load balancing. Where /feign/* represents only one path, /feign/* represents the following multiple paths

accessed through the gateway:

 

let’s look at the second one:

The

profile is as follows:

server:
  port: 8090
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
spring:
  application:
    name: zuul-service
zuul:
  routes:
    api-a:
      path: /feign/**
      service-id: eureka-feign
#zuul:
#  routes:
#    feign:
#      path: /feign/**
#      url: http://localhost:8085/

now that you are registered on eureka, you should get the registered service information, access the micro service through the service id, write the service name here with the service-id, and also implement the routing and melting functions.

is then accessed through the gateway:

and the third: zuul.routes. Service name: access path

zuul:
  routes:
    eureka-feign: /feign/**

if you ignore a micro service and do not use the gateway, set this:

zuul:
    #忽略某个微服务
  ignored-services: eureka-feign

forward jumps to local url:

zuul.routes.api-a.path=/user/**
zuul.routes.api-a.url=forward:/user

routing prefix:

zuul.prefix

does not take effect by default. To take effect, zuul.stripprefix =false sets the omitted prefix to false

zuul.routes.api-a.path=/user/**
zuul.routes.api-a.stripPrefix=false

if you want to configure routing rules in order, you must use the yml file, not the properties file

zuul:
  routes:
    users:
      path: /user/**
    others:
      path: /**

my github address