In micro services, a unified gateway is usually added to all services. The gateway mainly performs routing forwarding and transverse section functions (such as authentication, log collection, current limiting and counting).
Step 1: As a maven project, add the specified package in pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> //Gateway also needs to be registered with the registry eureka </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-netflix-zuul</artifactId>//This is the key jar package </dependency> </dependencies>
Step 2: The configuration file application.yml is configured as follows:
spring: application: name: gateway --- server: port: 8080 tomcat: uri-encoding: UTF-8 spring: http: encoding: charset: UTF-8 force: true enabled: true# Character set setting to solve the problem of Chinese to front ends becoming garbled servlet: multipart: max-file-size: 2048MB max-request-size: 200MB ribbon://Load Balancing Strategy ReadTimeout: 600000 # http build socket timeout time, milliseconds ConnectTimeout: 600000 # http read response socket timeout hystrix://Fuse command: default: execution: isolation: thread: timeoutInMilliseconds: 2400000 zuul://Here is the service forwarding configuration routes: uaa: sensitiveHeaders: path: /uaa/** url: uaa stripPrefix: false activiti: sensitiveHeaders: path: /activiti/** serviceId: activiti add-proxy-headers: true add-host-header: true host: socket-timeout-millis: 300000 connect-timeout-millis: 300000
Step 3: Configure the bootstrap.yml file
spring: profiles: active: test --- #testing environment spring: profiles: test eureka: instance: prefer-ip-address: true lease-expiration-duration-in-seconds: 10 lease-renewal-interval-in-seconds: 2 client: service-url: defaultZone: http://---: 7002/eureka/ # Here is the registry address, just write IP.
Step 4: Configure the startup class Gateway Application. Now, if the environment is okay, it should be okay to run the main class directly.
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}