How to use Spring Cloud zuul achieve Gateway

Use zuul unified gateway interfaces exposed outside

1. Create a new project spring-zuul

2. Introduction of pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

3. Configuration

spring:
  application:
    name: spring-zuul
  http:
    encoding:
      charset: UTF-8 #设置请求返回UTF-8编码
      force: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
server:
  port: 8082

4. Start class notes

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class SpringZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringZuulApplication.class, args);
    }

}

 

Zuul transit service by accessing the respective service url micro format:  {the basePath} / {spring.application.name} / url

{basePath}: zuul path after the gateway program starts
{spring.application.name} subsystems each specific micro service name, i.e., the configuration file is spring.application.name
URL: each of the http custom application interface.

 

test:

Complete code access: https://github.com/halouprogramer/spring-cloud-demo

Guess you like

Origin www.cnblogs.com/haloujava/p/12122040.html