spring cloud Gateway for building microservices architecture of API GateWay

  • 2020-12-26 05:40:07
  • OfStack

preface

In front of our blog, said, when the service A B needs to call service, you just need to get from Eureka B services registered instance, Feign is then used to invoke B service, using Ribbon to realize load balance, however, when we are at the same time to the client the ferrous multiple services, the client how we teach service call, if we also want to join the security authentication, access control, filter and the characteristics of dynamic routing, etc, you will need to use Zuul to implement API GateWay, Next, let's look at how Zuul is used.

1. Add Zuul dependency


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

Since we need to register the Zuul service on Eureka Server and find the registered service on Eureka Server, we add the Eureka dependency here.

2. Enable Zuul support on the application Application main class


@SpringBootApplication 
@EnableZuulProxy //  use @EnableZuulProxy To turn on Zuul Support if you don't want to use it Zuul To provide the Filter And the reverse proxy function can be used here @EnableZuulServer annotations  
public class ZuulApplication { 
 public static void main(String[] args) { 
  SpringApplication.run(ZuulApplication.class, args); 
 } 
} 

3. Add the base configuration information of Zuul to ES38en.yml


spring: 
 application: 
  name: gateway-zuul #  The application of  
server: 
 port: 8768 #Zuul Server The port number  
eureka: 
 client: 
  service-url: 
   defaultZone: http://localhost:8761/eureka 
 instance: 
  prefer-ip-address: true 

4. Add service routing configuration to ES45en.yml

Premise: Two services have been registered with Eureka Server, respectively: springboot-h2-service and ES55en-ES56en-ES57en-ES58en, of which ES59en-ES60en-ES61en-ES62en service will call ES63en-ES64en2-ES65en service, springboot-ES67en-ES68en-ES69en service is the service we provide to the outside world, that is to say, springboot-ES71en-ES72en-ES73en service is the service we disclosed to the client.


#  Routing configuration 1 
#zuul: 
# routes: 
#  springboot-rest-template-feign: /templateservice/** # All requests springboot-rest-template-feign Will be intercepted and forwarded to templateservice on  
 
 
#  Routing configuration 2 
zuul: 
 routes: 
  api-contract: #  Among them api-contract It's a routing name, you can define it any way you want, but path and service-id Need to be 11 The corresponding  
   path: /templateservice/** 
   service-id: springboot-rest-template-feign # springboot-rest-template-feign To register to Eureka The name of the service  
ribbon: 
 NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #  Configure the server side load balancing policy   

5. Verify

We can to verify the below, in the browser input: http: / / localhost: 8768 / templateservice/template / 1 can see the test result.


Related articles: