SpringCloudNetflix集成其组件

文章介绍了如何在SpringCloud项目中集成OpenFeign进行服务调用,并结合Hystrix实现熔断机制。同时,文章还展示了Zuul的使用,作为微服务的边缘服务和路由网关,进行请求分发和统一登录检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一丶集成OpenFeign

1.OpenFeign概念

基于Ribbon封装,简化url,参数的拼接过程,编写feign接口就key了

2.OpenFeign+Hystrix熔断器实战

2.1导入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 导入公共模块 -->
        <dependency>
            <groupId>com.jd</groupId>
            <artifactId>springcloud-netflix-pojo-user</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--2.导入Feign的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

2.2配置启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
//表示开启Feign服务支持,也可以用@EnableEurekaClient
@EnableFeignClients
public class PayApplication {
    public static void main(String[] args) {
        SpringApplication.run(PayApplication.class, args);
    }
}

2.3配置yml

#注册到EurekaServer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:10010/eureka/
  instance:
    instance-id: pay-server
spring:
  application:
    name: pay-server
server:
  port: 10040
feign:
  hystrix:
    enabled: true  #开启熔断支持

二丶集成zuul

1.zuul是什么

所有的请求都需要通过zuul将请求分发到其他微服务,根据这一特性我们就可以在zuul做统一的登录检查,下游的微服务不再处理登录检查逻辑

2.zuul实战

2.1.导入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.2.配置启动类

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

@SpringBootApplication
//开启zuul功能
@EnableZuulProxy
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
}

2.3.配置yml

#注册到EurekaServer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:10010/eureka/
  instance:
    instance-id: zuul-server
spring:
  application:
    name: zuul-server
server:
  port: 10050
zuul:
  ignoredServices: "*" #禁用服务名
  prefix: "/services" #前缀
  routes:
    user-server: "/user/**" #如果请求路径包含/user/,就将请求分发给user-server
    pay-server: "/pay/**" #如果请求路径包含/pay/,就将请求分发给user-server
    order-server: "/order/**" #如果请求路径包含/order/,就将请求分发给user-server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值