Zuul 相當於是設備和 Netflix 流應用的 Web 網站后端所有請求的前門,提供動態路由,監控,彈性,安全等的邊緣服務
所有請求都經過網關(API Gateway)zuul,然后轉發到各個子服務上去
1.注冊中心eureka
<!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
@EnableEurekaServer//注解啟動一個服務注冊中心 @SpringBootApplication public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
#application配置 server.port=8888 #在默認設置下,該服務注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false #服務注冊地址 eureka.instance.ip-address=localhost eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
啟動app,訪問http://localhost:8888/
2.service1
@EnableDiscoveryClient//激活Eureka中的DiscoveryClient實現 @SpringBootApplication public class Service1App { public static void main(String[] args) { SpringApplication.run(Service1App.class, args); } }
server.port=1111 server.address=localhost spring.application.name=service1 #注冊中心地址 eureka.instance.ip-address=localhost eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
public class UserController { private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class); @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/index") public String index(HttpServletRequest request){ return "service1 被調用, 請求地址:" + request.getRequestURI(); } }
3.service2
@EnableDiscoveryClient//激活Eureka中的DiscoveryClient實現 @SpringBootApplication public class Service2App { public static void main(String[] args) { SpringApplication.run(Service2App.class, args); } }
server.port=2222 server.address=localhost spring.application.name=service2 #注冊中心地址 eureka.instance.ip-address=localhost eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
public class User2Controller { private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class); @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/index") public String index(HttpServletRequest request){ return "service2 被調用, 請求地址:" + request.getRequestURI(); } }
4.zuul網關
<!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
@EnableZuulProxy//聲明zuul代理 @SpringBootApplication public class ZuulMainApp { public static void main(String[] args) { SpringApplication.run(ZuulMainApp.class, args); } }
#注冊中心地址 eureka.instance.ip-address=localhost eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ #路由配置 #url形式 zuul.routes.baidu.path=/baidu/** zuul.routes.baidu.url=http://www.baidu.com #注冊服務 zuul.routes.service1.path=/service1/** zuul.routes.service1.serviceId=service1 zuul.routes.service2.path=/service2/** zuul.routes.service2.serviceId=service2
測試:啟動sevice1,service2,zuul
查看注冊中心:
通過網關+路由名稱訪問:
http://localhost:8080/baidu
http://localhost:8080/service1/index
http://localhost:8080/service2/index
項目地址:https://github.com/yangzhenlong/mySpringBootDemo/tree/master/springboot12-zuul