Spring cloud: from incomprehension to abandonment, Chapter 5
I. zuul routing gateway
1. zuul overview
Brief introduction: unified access gateway of external interface. Zuul includes two main functions of request routing and filtering: The routing function is responsible for forwarding the external request to the specific microservice instance, which is the basis of realizing the unified access to the external access, while the filter function is responsible for intervening the request processing process, which is the basis of realizing the request verification, service aggregation and other functions. Zuul and Eureka integrate, register zuul itself as an application under Eureka service governance, and obtain from Eureka at the same time The messages of other microservices, that is, the access to microservices in the future, are obtained through zuul jump. Note: Zuul service will eventually register with Eureka Provide three functions of = agent + routing + filtering
2. zuul basic configuration
1. Create a new cloud-zuul-9527 module
2>,POM
zuul services also need to be integrated into eureka
<dependencies> <!-- zuul Routing Gateway --> <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> <!-- actuator Monitor --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- hystrix fault-tolerant--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- Daily standard --> <dependency> <groupId>com.lee</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- Hot deployment plug in --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
3. New YML
server: port: 9527 spring: application: name: cloud-zuul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true info: app.name: cloud author.name: lee app.function: gateway build.artifactId: $project.artifactId$ build.version: $project.version$
4. HOST configuration modification
127.0.0.1 zuul9527.com
5. Main startup class
package com.lee.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class Zuul_9527_App { public static void main(String[] args) { SpringApplication.run(Zuul_9527_App.class,args); } }
Test:
① start three eureka clusters ②. Start cloud-provider-dept-8001 service provider ③ one route cloud-zuul-9527 ④ visit: No routing: http://localhost:8001/dept/get/1 Enable Routing: http://zuul9527.com: 9527 / cloud dept / dept / get / 1 Note: zuul has little to do with feign or ribbon. Visits like feign, ribbon http://localhost7001/consumer/xxx have nothing to do with zuul.com: 9521 / cloud dept / XXXX. In the actual project, the consumers of these services are generally based on the service delivery.
3. zuul route access mapping rules
3.1 >, reason:
before: http://Zuul9527.com: the cloud Dept microservice name in the 9527 / cloud dept / dept / get / 1 access path is easy to expose, so we need to hide the replacement.
Modification: YML
zuul: routes: mydept.serviceId: microservicecloud-dept #Microservice instance name mydept.path: /mydept/** #Mapping name
Test:
http://zuul9527.com:9527/mydept/dept/list
3.2 >, reason:
before: http://Zul9527.com: 9527 / cloud-dept / dept / get / 1 can also be seen. In the above step, only replacement is done, but no hiding is done
Modify YML
Ignore single: zuul: ignored-services: cloud-dept Ignore multiple: zuul: ignored-services: "*"
Test:
http://zuul9527.com:9527/cloud-dept/dept/get/1
3.3 >, reason:
Set a common prefix name
Modify YML
zuul: prefix: /lee
Test:
http://zuul9527.com:9527/lee/mydept/dept/list If it can't be accessed in this way, it may be the problem of yml, which is introduced in POM file <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.25</version> </dependency> that will do
II. Distributed configuration center
To be continued....