Spring Cloud:第五章:Zuul服务网关

快速入门

定义user,order,pay服务,定义zull服务网关服务都注册到eureka服务上,

通过一下接口访问user,order,pay的服务,

http://localhost:7070/pay/index
http://localhost:8080/user/index
http://localhost:9090/order/index

定义服务网关服务zuul,我们看看其相关配置,zuul-service加入依赖:

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


配置文件:

spring:
application:
name: zuul-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: s p r i n g . a p p l i c a t i o n . n a m e : {spring.application.name}: spring.application.name:{spring.cloud.client.ipAddress}:KaTeX parse error: Expected '}', got 'EOF' at end of input: …on.instance_id:{server.port}}
prefer-ip-address: true
server:
port: 6069

定义启动类:

package com.zhihao.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

//使用@EnableZuulProxy注解开启zuul的api网关服务功能
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,args);
}
}

然后可以通过服务网关进行访问上面的三个服务接口,

http://localhost:6069/pay-service/pay/index
http://localhost:6069/user-service/user/index
http://localhost:6069/order-service/order/index

注意:
默认的zuul结合eureka会将注册到eureka的服务名作为访问的ContextPath。

 

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120382551

 

posted @ 2022-10-06 16:17  忘川信使  阅读(22)  评论(0编辑  收藏  举报