[SpringCloud] Gateway routing configuration (seventeen)

  The previous chapter [SpringCloud] Gateway Gateway Introduction (16) introduced the basic use of Gateway, this chapter introduces Gateway routing configuration

  This chapter uses the project, or the project of the previous chapter

Gateway routing configuration

  There are 2 ways to configure the gateway routing, one is to configure through YML configuration file, the other is to configure through configuration class

YML configuration file configuration routing

  1. Specify the path forwarding route

  That is, it is forwarded according to the specified path, and the case refers to the previous chapter

  The configuration is as follows:

. 1  Spring:
 2    file application:
 . 3      name: Cloud-Gateway- Gateway
 . 4    Cloud:
 . 5      Gateway:
 . 6        routes:
 . 7            # route ID, there is no fixed rule, but the only requirement is recommended with the service name 
. 8          - ID: payment_routh
 . 9            # after matching The routing address to provide the service 
10            uri: http: // localhost: 8001
 11            # Assert that the path matches the routing 
12            predicates:
 13              -Path = / payment / get / **

  2. Dynamic routing through service name

  By default, Gateway will create a dynamic route for forwarding based on the service list registered in the registration center, using the microservice name on the registration center as the path, so as to realize the function of dynamic routing

  1) On the basis of the previous item, a new payment service module is added, which is the same as the existing payment module. Refer to: [SpringCloud] Service Provider Cluster and Service Discovery Discovery (3)

  2) Modify the configuration file application.yml of the Gateway Gateway project (springcloud-gateway-gateway9527). The modifications are as follows:

. 1  Spring:
 2    file application:
 . 3      name: Cloud-Gateway- Gateway
 . 4    Cloud:
 . 5      Gateway:
 . 6        Discovery:
 . 7          Locator:
 . 8            # Open dynamically created routing functions from the registry, the use of micro routing service name 
. 9            Enabled: to true 
10        routes :
 11            # routing the above mentioned id 
12          - the above mentioned id: payment_routh
 13            # after matching service routing address 
14            uri: LB: // Cloud-payment- service
 15            # assertion 
16            predicates:
17             - Path=/payment/get/**

  3) Test

    a. Start the project

    b. Check the Eureka registration center, address: http: // localhost: 8761 /

    

    c. Visit address: http: // localhost: 8002 / payment / get / 1, verify that the payment service is normal

    d. Access address: http: // localhost: 9527 / payment / get / 1, to verify that the dynamic gateway has taken effect, and it is the method of load polling to access the service of the payment module

    

Configure routing through configuration classes

  This chapter demonstrates accessing Baidu News website ( http://news.baidu.com ) through configuration class configuration

  1. Visit the domestic news module of Baidu News, address: http://news.baidu.com/guonei, confirm the news address

  

  2. In the above Gateway Gateway project (springcloud-gateway-gateway9527), add a configuration class, the content is as follows:

 1 @Configuration
 2 public class GatewayConfig {
 3 
 4     @Bean
 5     public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
 6         // 路由构造器
 7         RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
 8         // 设置路径
 9         routes.route("baidu_news_guonei_routh", r -> {
10             return r.path("/guonei").uri("http://news.baidu.com/guonei");
11         });
12         routes.route("baidu_news_guoji_routh", r -> {
13             return r.path("/guoji").uri("http://news.baidu.com/guoji");
14         });
15 
16         return routes.build();
17     }
18 }

  3. Test

    1) Start the project

    2) Access address: http: // localhost: 9527 / guonei, verify whether the domestic news can be accessed through the gateway

    

    3) Access address: http: // localhost: 9527 / guoji, verify whether the international news can be accessed through the gateway

  

Guess you like

Origin www.cnblogs.com/h--d/p/12735381.html