1. Service gateway
The reason for the emergence of API gateway is the emergence of micro service architecture. Different micro services generally have different network addresses, and external clients may need to call the interfaces of multiple services to complete a business requirement. If the client is allowed to communicate directly with each micro service, there will be the following problems:
(1) The client will request different microservices many times, which increases the complexity of the client.
(2) There are cross domain requests, which are relatively complex to process in certain scenarios.
(3) Authentication is complex, and each service needs independent authentication.
(4) It is difficult to refactor. With the iteration of the project, it may be necessary to re divide the microservices. For example, you might merge multiple services into one or split a service into multiple. If the client communicates directly with the microservice, the refactoring will be difficult to implement.
(5) Some micro services may use firewall / browser unfriendly protocols, and direct access will be difficult.
These problems can be solved by API gateway. API gateway is an intermediate layer between client and server. All external requests will pass through the API gateway layer first. In other words, business logic is more considered in the implementation of API, and security, performance and monitoring can be done by API gateway, which not only improves business flexibility but also does not lack security.
In a sense, it can replace the proxy port of nginx to distribute requests. At the same time, configuration in the service gateway can also solve the cross domain problem of front and back-end requests.
2. Spring cloud gateway
Spring Cloud gateway is the official spring gateway based on Spring 5.0 and Spring Boot2.0 0 and Project Reactor. Spring Cloud gateway aims to provide a simple, effective and unified API routing management method for microservice architecture. As a gateway in Spring Cloud ecosystem, Spring Cloud gateway aims to replace Netflix Zuul. It not only provides a unified routing method, but also provides the basic functions of the gateway based on Filer chain, For example: safety, monitoring / embedding point, current limiting, etc
3. Service construction
3.1 build server gateway
Create a new module in the project
3.2 modify the configuration POM xml
Modify POM xml
<dependencies> <dependency> <groupId>com.atguigu.yygh</groupId> <artifactId>common-util</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Service registration --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
3.3 add configuration file under resources
1,application.properties
# Service port server.port=80 # service name spring.application.name=service-gateway # nacos service address spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 #Using service discovery routing spring.cloud.gateway.discovery.locator.enabled=true #Set routing id spring.cloud.gateway.routes[0].id=service-hosp #Set the uri of the route spring.cloud.gateway.routes[0].uri=lb://service-hosp #Set the route assertion. The agent servicerId is the / auth / path of auth service spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/** #Set routing id spring.cloud.gateway.routes[1].id=service-cmn #Set the uri of the route spring.cloud.gateway.routes[1].uri=lb://service-cmn #Set the route assertion. The agent servicerId is the / auth / path of auth service spring.cloud.gateway.routes[1].predicates= Path=/*/cmn/**
3.4 add startup class
package com.atguigu.yygh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServerGatewayApplication { public static void main(String[] args) { SpringApplication.run(ServerGatewayApplication.class, args); } }
3.5 cross domain processing
Cross domain: Browser restrictions on javascript's homology policy.
The following situations are cross domain:
Cross domain cause description example
Different domain names www.jd.com COM and www.taobao.com com
Same domain name, different ports www.jd.com COM: 8080 and www.jd.com com:8081
The secondary domain name is different from item jd. COM and Miaosha jd. com
If the domain name and port are the same, but the request path is different, it does not belong to cross domain, such as:
www.jd.com/item
www.jd.com/goods
http and https are also cross domain
Access localhost:8888 from localhost:1000, which belongs to different ports and cross domains.
3.5.1 why is there a cross domain problem?
Cross domain problems do not always exist.
Because the cross domain problem is a security limitation of the browser for ajax requests: the ajax request initiated by a page can only be the same path as the current page domain name, which can effectively prevent cross site attacks.
Therefore: cross domain issues are a limitation of ajax.
But this has brought inconvenience to our development, and in the actual production environment, there must be many servers interacting with each other, and the addresses and ports may be different. What should we do?
3.5.2 solving cross domain problems
Global configuration class implementation
CorsConfig class
@Configuration public class CorsConfig { @Bean public CorsWebFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedMethod("*"); config.addAllowedOrigin("*"); config.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/**", config); return new CorsWebFilter(source); } }
3.6 service adjustment
At present, we have done cross domain processing in the gateway, so the service does not need to do cross domain processing. Remove the @ CrossOrigin tag previously added to the controller class to prevent program exceptions