Welcome to visit mine GitHub

https://github.com/zq2599/blog_demos

Content : All original articles classified summary and supporting source code , involve Java、Docker、Kubernetes、DevOPS etc. ;

An overview of this article

  • This article is about 《Spring Cloud Gateway actual combat 》 The second part of the series , We learned from the foregoing that Spring Cloud Gateway The core of is routing configuration , And then locally application.yml A route is configured in , But this way of modifying the local configuration file lacks flexibility , May not be able to meet the flexible business needs , therefore , The purpose of this article is to find other configuration methods other than local configuration , Meet various practical needs ;
  • In general, the following three methods are commonly used :
  1. The target address supports using the service name ( Replace the former IP+ port );
  2. Support in nacos On the configuration ;
  3. Support code writing configuration ;
  • There is also a more flexible configuration : A dynamic proxy , Because it involves a lot of code, a separate article will be introduced in detail

Source download

  • The complete source code of this actual combat can be found in GitHub Download to , The address and link information is shown in the following table (https://github.com/zq2599/blog_demos):
name link remarks
Project home page https://github.com/zq2599/blog_demos The project is in progress. GitHub Home page on
git Warehouse address (https) https://github.com/zq2599/blog_demos.git The warehouse address of the source code of the project ,https agreement
git Warehouse address (ssh) [email protected]:zq2599/blog_demos.git The warehouse address of the source code of the project ,ssh agreement
  • This git Multiple folders in project , The source code of this article is in spring-cloud-tutorials Under the folder , As shown in the red box below :

preparation

  • A little more preparation needs to be done before the official start , Whole 《Spring Cloud Gateway actual combat 》 In the series , All requests will eventually be routed to provider-hello This web Up , The service currently has only one web Interface /hello/str, Now let's add another , The actual combat will use

  • Newly increased web Interface from LBTest.java, So it's very simple :

package com.bolingcavalry.provider.controller;
import com.bolingcavalry.common.Constants;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date; @RestController
@RequestMapping("/lbtest")
public class LBTest { private String dateStr(){
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
} /**
* Return string type
* @return
*/
@GetMapping("/str")
public String helloStr() {
return Constants.LB_PREFIX + ", " + dateStr();
}
}
  • In the above code Constants.LB_PREFIX From subproject common:
package com.bolingcavalry.common;
public interface Constants {
String HELLO_PREFIX = "Hello World";
String LB_PREFIX = "Load balance";
}
  • After writing the code , Make sure nacos Has been launched

  • Start up provider-hello engineering , After successful startup, go to see nacos, Confirm that you have registered :

  • Ready , It's time to start fighting

The target address supports using the service name ( Replace the former IP+ port )

  • Let's start with the simplest , Let's look at the previous routing configuration , The red box is shown below , The destination address is IP+ port :

  • Yes Spring Cloud Of course you see the problem : No registration found , exactly , It is not appropriate to write the address and port in the configuration file , Let's solve this problem first ;

  • The new name is gateway-by-loadbalance The sub project of , Its pom.xml The dependencies in are as follows , It can be seen that the focus is spring-cloud-starter-loadbalancer

<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- The routing strategy uses lb The way is , This dependence must have -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--nacos: Registry Center -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
  • The code to start the class eliminates ( As before )

  • The configuration information is as follows , The key is uri Value lb://provider-hello, Prefix used lb:, hinder provider-hello Is in the nacos Registered service name :

server:
# Service port
port: 8085
spring:
application:
name: gateway-by-loadbalance
cloud:
nacos:
# Configuration of registry
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: path_route_lb
uri: lb://provider-hello
predicates:
- Path=/lbtest/**
  • Unit test class :
package com.bolingcavalry.gateway;
import com.bolingcavalry.common.Constants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.junit.jupiter.api.Assertions.assertTrue; @SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureWebTestClient
public class HelloTest { @Autowired
private WebTestClient webClient; @Test
void testLoadBalance() {
webClient.get()
.uri("/lbtest/str")
.accept(MediaType.APPLICATION_JSON)
.exchange()
// Verify the status
.expectStatus().isOk()
// The verification results , Notice that the result is in string format
.expectBody(String.class).consumeWith(result -> assertTrue(result.getResponseBody().contains(Constants.LB_PREFIX)));
}
}
  • Run unit tests , adopt , It can be seen that the above configuration can be through the prefix lb: Accurately find the service :

Support in nacos On the configuration

  • Write all configuration information in application.yml There is a problem in : Cannot configure remotely , This is inconvenient in scenarios with a large number of applications , Fortunately nacos Provides remote configuration capabilities , After the application starts, you can start from nacos Get your own configuration information , Let's try

  • The new name is gateway-nacos-config The sub project of , Its pom.xml The dependencies in are as follows , Please pay attention to the Chinese notes inside , Each indicates the role of each dependency :

<dependencies>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Use bootstrap.yml When , This dependence must have -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- The routing strategy uses lb The way is , This dependence must have -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--nacos: Configuration center -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos: Registry Center -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
  • Local configuration file bootstrap.yml, It's simple , Namely nacos Address and remote configuration information :
spring:
application:
name: gateway-nacos-config
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yml
group: DEFAULT_GROUP
  • And then nacos Add a profile , The operation is shown in the red box below :

  • Add a configuration , The points to note are as follows ( The text of the configuration information will be given later , Easy to copy ):

  • The complete configuration information in the figure above is as follows :
server:
port: 8083
spring:
cloud:
gateway:
routes:
- id: path_route_addr
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/**
- id: path_route_lb
uri: lb://provider-hello
predicates:
- Path=/lbtest/**
  • The two test methods in the test class are as follows , There is no difference from the previous :
@Test
void testHelloPredicates() {
webClient.get()
.uri("/hello/str")
.accept(MediaType.APPLICATION_JSON)
.exchange()
// Verify the status
.expectStatus().isOk()
// The verification results , Notice that the result is in string format
.expectBody(String.class).consumeWith(result -> assertTrue(result.getResponseBody().contains(Constants.HELLO_PREFIX)));
} @Test
void testLoadBalance() {
webClient.get()
.uri("/lbtest/str")
.accept(MediaType.APPLICATION_JSON)
.exchange()
// Verify the status
.expectStatus().isOk()
// The verification results , Notice that the result is in string format
.expectBody(String.class).consumeWith(result -> assertTrue(result.getResponseBody().contains(Constants.LB_PREFIX)));
}
  • Run the unit test class , The test passed , Proof from nacos Get configuration file succeeded :

Write code to configure

  • The previous examples , The routing information is written in the configuration file , There is actually another way : Write code to configure routing , Can write their own code to configure , This makes it more flexible

  • The new name is gateway-by-code The sub project of , Its pom.xml The file can refer to the previous project

  • The focus of this next example , Add a... In the configuration class RouteLocator Type of bean, You can add a route through the following code :

package com.bolingcavalry.gateway.cofig;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RouteConfig { @Bean
public RouteLocator customizeRoute(RouteLocatorBuilder builder) {
return builder
.routes()
.route(
// The first parameter is the unique identity of the route
"path_route_lb",
// The second parameter is lambda Realization ,
// The matching condition is set to match according to the requested path , And forwarding address ,
// Be careful lb:// Indicates that this is a service name , From you to
r -> r.path("/lbtest/**").uri("lb://provider-hello")
)
.build();
}
}
  • The above code only configures one route , There is also one in the configuration file , This will verify that the code and configuration file can work at the same time :
server:
# Service port
port: 8084
spring:
application:
name: gateway-by-code
cloud:
nacos:
discovery:
# nacos Service address
server-addr: 127.0.0.1:8848
gateway:
routes:
- id: path_route_addr
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/**
  • The test class is as like as two peas , It doesn't take up space , There are still two test methods testHelloPredicates and testLoadBalance

  • Executing unit tests can pass , Prove that the code configuration routing has no problem :

  • thus , Load balancing 、nacos To configure 、 We've all tried examples of code configuration , Together, they will bring great convenience to the configuration of the actual living environment , I hope I can give you some reference

Defects and solutions

  • Although there are many configurations above , But there is a common problem : Every time the configuration changes ,Gateway The application needs to be restarted to take effect , This is not acceptable in a production environment that requires continuous production
  • In order to make the latest routing configuration available in Gateway The application takes effect without restarting , In the next article, let's explore how dynamic routing is implemented

You are not alone , Xinchen's original works are accompanied all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. kubernetes series
  5. database + Middleware family
  6. DevOps series

Welcome to the official account : Xinchen, programmer

WeChat search 「 Xinchen, programmer 」, I'm Xinchen , Looking forward to traveling with you Java The world ...

https://github.com/zq2599/blog_demos

Spring Cloud Gateway The second part of the actual battle : More related articles on routing configuration methods

  1. Spring Cloud gateway Gateway service II Assertion 、 filter

    The current popularity of microservices , If you can't learn a microservice framework technology . How can I get a promotion and a raise , Add chips to your resume ?spring cloud and Dubbo Need to study alone . Say there is no time ? No energy ? We need to learn two frames ? and Spring C ...

  2. Spring Cloud Gateway( 7、 ... and ): Route predicate factory WeightRoutePredicateFactory

    This article is based on spring cloud gateway 2.0.1 Continued above 5. Based on routing weight (weigth) Predicate factory for Spring Cloud Gateway An assertion factory based on routing weight is provided , When configuring, specify the minutes ...

  3. Spring Cloud Gateway( 6、 ... and ): Route predicate factory RoutePredicateFactory

    This article is based on spring cloud gateway 2.0.1 1. brief introduction Spring Cloud Gateway establish Route Object time , Use RoutePredicateFactory establish ...

  4. Spring Cloud Gateway( 5、 ... and ): Route locator RouteLocator

    This article is based on spring cloud gateway 2.0.1 1. brief introduction direct obtain road from Of Method yes adopt RouteLocator Interface obtain . Again , The The top level Interface How much individual Realization class , ...

  5. Spring Cloud Gateway( Four ): Route definition locator RouteDefinitionLocator

    This article is based on spring cloud gateway 2.0.1 1. brief introduction RouteDefinitionLocator It is the top-level interface of the route definition locator , Its main function is to read the routing configuration information (org.spri ...

  6. Spring Cloud Gateway + Nacos(1) Simple configuration

    When I was studying, I just referred to the blog of this big guy : Nacos Integrate Spring Cloud Gateway Based on using Now learn spring cloud alibaba Use nacos Be a service center ,dubbo Communication ...

  7. Build from scratch Spring Cloud Gateway gateway ( Two )—— Print request response log

    As gateway , Logging is an essential function , You can add... In the gateway requestId To query the call execution of the whole request chain, and so on . Print request log The most important thing about the print request log is the print request parameters , however RequestBody Common feelings ...

  8. Spring Cloud Alibaba actual combat ( Twelve ) - Nacos Configuration Management

    The main content of this chapter is : Use Nacos Manage configuration and the reasons for configuration management , Configure how to manage as well as dynamic refresh and best implementation summary , And finally Nacos Configuration refresh principle interpretation The technology is similar to Spring Cloud Config 1 Configuration Management ...

  9. Follow me Spring Cloud(Finchley edition )-17-Zuul Detailed explanation of routing configuration

    But in the real world , You often need to define your own routing rules ,Zuul The routing configuration of is very flexible . Simple , This section explains in detail Zuul Routing configuration for . One . Customize the access path of the specified microservice To configure zuul.routes. Specify the name of the microservice serviceId ...

  10. 5.2 spring5 Source code --spring AOP Source code analysis II -- Configuration mode of section

    The goal is : 1. What is? AOP, What is? AspectJ 2. What is? Spring AOP 3. Spring AOP Implementation of the principle version of the note 4. Spring AOP Analysis of section principle One . know AOP And its use See the blog for details ...

Random recommendation

  1. jquery submit() Submit failed

    Write a form to submit today I went to $('#wechat_form').submit() this , But I never submitted this form google For a moment Additional Notes:Forms and their ...

  2. DGV Two lines in the header

    try { if (e.RowIndex < 0) { RectangleF _rect = e.CellBounds; Pen _pen = new Pen(Color.Black); Pen ...

  3. EXTJS The dynamic change Gird The column value

      var me = this.getView('EditProProductQrcodePanel'); var grid = me.down("[name=mallQrcodeGrid] ...

  4. JSP Programming - Advance gradually and entrench oneself at every step

    [ first JSP give an example ] header.jsp <%@ page language="java" contentType="text/html; charset=utf ...

  5. Microsoft SQL Server Examples of stored procedures

    -- if SP is existed, drop it. if (object_id('InvHoldToDPS', 'P') is not null) drop proc InvHoldToDPS ...

  6. Win7 and MAC The system goes through VMware Shared folders ( Simple and easy to use , Almost nothing to set )

    Win7 yes Server,Mac yes Client,VMware Up operation Mac System 1. stay VMware Of Options From the menu Shared Folders Options 2. choice Always enabled Options 3. ...

  7. 20175209 Experiment two 《Java Object oriented programming 》 Experimental report

    20175209 Experiment two <Java Object oriented programming > Experimental report One . Preparation for the experiment Learn about three types of code Pseudo code Product code Test code Let's write pseudo code first , Pseudo code Solve the problem from the perspective of intention : With pseudo code We use it ...

  8. Vue Learning notes -- inline-template

    Simple books More convenient use of private sub components When defining a private subcomponent , If the template Too long makes the code very hard to read In this case, you can use the inline template But if it's written like this What about sub components for wool ?

  9. After class exercises of introduction to software engineering Github Homework ( Reverse the order of words in an English sentence , The letters in a word are arranged properly )

    Java Source code     package yly; import java.util.Scanner; public class ruanjian { public static void main(St ...

  10. Firebird Get user table and fields

    select rdb$relation_fields.rdb$relation_name table_name, rdb$relations.rdb$description table_des, rd ...