[技术陈旧] SpringCloud-Zuul网关项目 环境搭建和整合Eureka访问

1 新建一个springboot的项目或模块,并添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--SpringBoot整合Eureka客户端组件-->
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!--SpringCloud整合 zuul网关-->
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

2 启动类添加注解

@SpringBootApplication
@EnableEurekaClient //开启Eureka客户端
@EnableZuulProxy   //开启zuul 网关
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(AppGateWay.class,args);
    }
}

3 配置文件application.yml

server:
  port: 9000


#服务的名称
spring:
  application:
    name: api-gateway


#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.0.12:8761/eureka/


#自定义路由映射,设置 服务名称--url映射
zuul:
  routes:
    order-service: /apigateway/orderapi/**
    product-service: /apigateway/productapi/**
  #统一入口为上面的配置,其他入口忽略
  ignored-patterns: /*-service/**
  #处理http请求头为空的问题
  sensitive-headers:


通过网关访问某接口的方法
http://网关服务的ip:网关服务的端口  /路由映射中服务名对应的url规则  /control层的UrlMapping

注意,这是本机测试,所有的服务在一台电脑上,所以都是localhost,但是是不同的服务或项目,不要混淆

比如之前访问 产品服务product-service项目中 的 产品列表接口
http://localhost:8771/productmapping/list

现在通过网关来访问 
http://localhost:9000/apigateway/productapi/productmapping/list

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值