Preface
In our previous blog, when service A needs to call service B, you only need to obtain the registered instance of service B from Eureka, and then use Feign to call B's service and use Ribbon to achieve load balancing. However, when we expose multiple services to the client at the same time, how the client calls the service we exposed. If we also want to add security authentication, permission control, filters, and dynamic routing and other features, then we need to use Zuul to implement API GateWay. Let's take a look at how to use Zuul.
1. Join Zuul's 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 to Eureka Server and discover the registered service from Eureka Server, we add Eureka's dependencies here.
2. Enable Zuul support on the main application application class
@SpringBootApplication @EnableZuulProxy // Use @EnableZuulProxy to enable Zuul support. If you don't want to use the Filter and reverse proxy functions provided by Zuul, you can use @EnableZuulServer to annotate public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } } 3. Add Zuul's basic 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
Prerequisite: Two services have been registered in Eureka Server, namely: springboot-h2-service and springboot-rest-template-feign. The springboot-rest-template-feign service will call the springboot-h2-service service, and the springboot-rest-template-feign service is a service we provide to the outside world. That is to say, the springboot-rest-template-feign service is something we 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 2zuul: routes: api-contract: # where api-contract is the route name, which can be defined at will, but path and service-id need to correspond to path one by one: /templateservice/** service-id: springboot-rest-template-feign # springboot-rest-template-feign is the service name ribbon registered on Eureka: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule # Configure server load balancing policy
V. Verification
Now we can verify it. Enter: http://localhost:8768/templateservice/template/1 in the browser to see the test results.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.