[SpringCloud] Zuul routing gateway

1. What is

Zuul filter includes two main function and the routing request to:

  1. Routing function: responsible for forwarding the request to an external service on a specific micro instance, it is the basis for external access unified portal
  2. Filtering: responsible for processing the request to intervene is the basis of requesting a check, and other functions of the service aggregator

  Zuul integration and Eureka, will register itself as Zuul applications in Eureka service governance, and access to other micro-messaging service from Eureka, that after services are obtained through access to micro-jumps after Zuul

Note: Zuul eventually registered as a separate service to Eureka


2. Role

  Proxy + routing + filter three functions, if these three functions do not understand, can be combined with nginx, nginx we all know that to achieve a reverse proxy functionality, zuul also achieve a reverse proxy functionality, this blog is mainly about a reverse proxy , it also has load balancing (built Ribbon), access control role, as long as you can configure serviceId


3. project combat

3.1 rely lead

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

3.2 configuration files

server: 
  port: 9527  # zuul服务的端口号
 
spring: 
  application:
    name: microservicecloud-zuul-gateway   # 注册到Eureka上的服务的名称
 
# eureka配置
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 
 
# zuul配置 
zuul: 
	#在服务前统一加前缀
  prefix: /dmsd
	# 将真实的服务名称忽略
  #ignored-services: microservicecloud-dept   # 要隐藏的服务名称
  ignored-services: "*"  ,* 代表忽略所有的,即不能通过真实服务名路径访问,只能用替换会后的名,遵守单一入口原则
  routes: 
    mydept.serviceId: microservicecloud-dept   # 被隐藏的服务名
    mydept.path: /mydept/**    # 将服务名替换为此名

3.3 Configuration startup items

@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
	}
}

Zuul this service is to build successful, and modify the local hosts file, 127.0.0.1 is mapped to myzuul.com ;
before we visit a service inputs are: HTTP: // localhost: 8080 / the dept / GET / 2 ;
but using enter the Zuul route is: http://myzuul.com:9527/microservicecloud-dept/dept/get/2 ;
Zuul routing prefix and hide the real service name can be entered: http://myzuul.com:9527/dbsd / mydept / Dept / GET / 2 ;


4. Concluding Thoughts

  Build Zuul service, and register to Eureka, this time Zuul will obtain information and services on the Eureka registered to, when we enter the address (zuul + micro service name + url), first by Zuul find the service you want to access, and then by Zuul route to access the service to the address of a jump

5. Project Framework

  1. Use nignx load balancing and reverse proxy technology can achieve high availability gateway
  2. zuul gateway automatically integrated ribbon clients achieve reverse proxy routing

6. zuul and feign a difference

Both the application level as well as the principles are not the same.

  1. zuul a flow inlet of the entire application , receiving all requests, such as App, web pages, and forwards the request to the different services of different micro-processing module, which can be regarded as acting nginx
  2. feign sucked micro-current portion of the service interface is exposed and services, and is mainly used for service calls between services each micro
  3. zuul also contains hystrix and ribbon, http-based communications, direct proxy service on the line. In between it and increase feign service will only increase consumption of communication, no special significance. feign when the service call on the line with each other, can imitate rpc communication.
  4. Feign mainly for server-side flow control, Feign load balancing is based on the realization of Eureka
  5. Zuul mainly for client flow control and load balancing combined with Eureka Zuul achieve better ease of use, and Zuul I generally used to provide third party access interface.

The difference 7.zuul and nginx

Same point:

  1. Implement load balancing, reverse proxy, filter requests, to achieve a gateway effect

difference

  1. nginx using c language, zuul using java language
  2. nginx achieve server load balancing, and zuul using ribbon + eureka achieve client load balancing

So we only choose zuul, because (using their own familiar language development, easy to learn):

  1. zuul sessions for all services can intercept
  2. zuul can achieve security control, unified exception handling, xxs
  3. Access control, blacklist and white list, performance monitoring, logging print

Guess you like

Origin blog.csdn.net/wrs120/article/details/91782432