SpringBoot2.0应用Zuul路由网关

简介

zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用。Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。zuul的例子可以参考 netflix 在github上的 simple webapp,可以按照netflix 在github wiki 上文档说明来进行使用

实例1

简单配置路由

pom配置中加入以下依赖:


     org.springframework.cloud
     spring-cloud-starter-netflix-zuul

application.yml中加入以下内容:

server:
  port: 5000
spring:
  application:
    name: api-geteway
zuul: 
  ignoredServices: '*'
  routes:
    microservice-consumer-movie: /user/**
  host:
    connect-timeout-millis: 15000 #HTTP连接超时要比Hystrix的大
    socket-timeout-millis: 60000   #socket超时
eureka:
  client:
    serviceUrl: 
      defaultZone: http://user:password123@localhost:8761/eureka


ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

启动类中加入@EnableZuulProxy注解。

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
	}

}

microservice-consumer-movie微服务的Controller中的内容:

@RestController
public class MovieController {

	@Autowired
	private UserFeignClient userFeignClient;
	
	@GetMapping("/getUserById/{id}")
	public User getUserById(@PathVariable Long id) {
		User user = userFeignClient.getUserById(id);
		return user;
	}
	
}

microservice-consumer-movie微服务的UserFeignClient接口中的内容:

@FeignClient("microservice-provider-user")
public interface UserFeignClient {

	@RequestMapping(value="/simple/{id}",method=RequestMethod.GET)
	public User getUserById(@PathVariable("id") Long id);

}

 microservice-provider-user微服务中的Controller内容:

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;
	
	@GetMapping("/simple/{id}")
	public User findById(@PathVariable Long id) {
		return userRepository.findById(id).orElse(null);
	}

}

最后启动Eureka、 microservice-provider-user微服务、microservice-consumer-movie微服务、microservice-zuul网关。

SpringBoot2.0应用Zuul路由网关_第1张图片

在浏览器中输入路由网关的地址:http://192.168.203.103:5000/user/getUserById/1  输出查询结果。

SpringBoot2.0应用Zuul路由网关_第2张图片

实例2

配置请求前缀

application.yml中加入以下内容:

server:
  port: 5000
spring:
  application:
    name: api-geteway
zuul: 
  routes:
    abc: 
      path: /user/**
      service-id: microservice-provider-user
  host:
    connect-timeout-millis: 15000 #HTTP连接超时要比Hystrix的大
    socket-timeout-millis: 60000   #socket超时
  prefix: /simple #请求前缀
  strip-prefix: false
eureka:
  client:
    serviceUrl: 
      defaultZone: http://user:password123@localhost:8761/eureka
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

  
 

其他部分同实例1

发起 http://192.168.203.103:5000/simple/user/1 请求

SpringBoot2.0应用Zuul路由网关_第3张图片

实例3

路由中使用负载均衡

application.yml中加入以下内容:

server:
  port: 5000
spring:
  application:
    name: api-geteway
zuul: 
  routes:
    abc: 
      path: /user/**
      service-id: microservice-provider-user
  host:
    connect-timeout-millis: 15000 #HTTP连接超时要比Hystrix的大
    socket-timeout-millis: 60000   #socket超时
eureka:
  client:
    serviceUrl: 
      defaultZone: http://user:password123@localhost:8761/eureka
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000
  eureka: 
    enabled: false
microservice-provider-user: 
  ribbon: 
    listOfServers: http://localhost:7900,http://localhost:7901

  
 

其他部分同实例1

发起 http://192.168.203.103:5000/user/simple/1 请求后,ribbon会一定算法分配请求到7900和7901两个服务器上。

你可能感兴趣的:(springCloud,zuul,微服务)