Spring cloud builds the microservice architecture GateWay (API GateWay), springcloud

Source: Internet
Author: User

Spring cloud builds the microservice architecture GateWay (API GateWay), springcloud

Preface

As mentioned in our previous blog, when service A needs to call service B, it only needs to obtain the registered instance of Service B from Eureka and then use Feign to call service B, ribbon is used to achieve load balancing. However, when we expose multiple services to the client at the same time, how does the client call our exposed services? If we want to add security authentication, permission control, filters, dynamic routing, and other features, you need to use Zuul to implement the API GateWay. Next, let's take a look at how to use Zuul.

I. Add Zuul Dependencies

<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> 

Because we need to register the Zuul service to the Eureka Server and discover the registered service from the Eureka Server, we add Eureka dependencies here.

2. Enable Zuul support on the main Application class

@ SpringBootApplication @ EnableZuulProxy // use @ EnableZuulProxy to enable Zuul support. If you do not want to use the Filter and reverse proxy functions provided by Zuul, here you can use the @ EnableZuulServer annotation public class ZuulApplication {public static void main (String [] args) {SpringApplication. run (ZuulApplication. class, args );}}

3. Add basic Zuul configuration information in application. yml.

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

4. Add service routing configuration in application. yml

Premise: two services have been registered on Eureka Server: springboot-h2-service and springboot-rest-template-feign, where the springboot-rest-template-feign service calls the springboot-h2-service service, springboot-rest-template-feign is our external service. That is to say, the springboot-rest-template-feign service is exposed to the client.

# Routing configuration method 1 # zuul: # routes: # springboot-rest-template-feign:/templateservice/** # all requests for springboot-rest-template-feign, will be intercepted and forwarded to templateservice # routing configuration method 2 zuul: routes: api-contract: # Where api-contract is the route name, which can be defined at will, however, path and service-id must correspond to path:/templateservice/** service-id: springboot-rest-template-feign # springboot-rest-template-feign is the service name registered to Eureka ribbon: NFLoadBalancerRuleClassName: com. netflix. loadbalancer. roundRobinRule # configure Server Load balancer policies

V. Verification

Now we can verify it. Enter http: // localhost: 8768/templateservice/template/1 in the browser to see the test result.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.