Spring Cloud学习笔记(服务网关Zuul)
Zuul概述
Zuul是一个服务网关组件,能够通过与Eureka进行整合,将自身注册到Eureka Server中,与Eureka、RibbonHystrix等进行整合,同时从Eureka中获得其他微服务架构的实例信息。
Zuul快速入门
(1)搭建Spring Boot项目eureka-server。
(2)创建服务提供者eureka-provider,在eureka-provider中创建一个包,创建一个HystrixController类
package com.itheima.eurekaprovider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@RequestMapping("hi")
public String hi(String id){
return "hi,访问成功"+id;
}
}
配置application.yml变量
server:
port: 7006 #指定该provider的端口号
spring:
application: #指定应用名称
name: eureka-provider
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
instance:
hostname: localhost
(3)创建服务消费者eureka-consumer,配置全局变量application.yml
spring:
application:
name: eureka-consumer
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka
创建一个config包,新建配置类RestConfig
package com.itheima.eurekaconsumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration