Spring Cloud Fegin 和 Eurake 简单使用

Spring Cloud Fegin 和 Eurake 简单使用

Eurake注册中心

gradle中的依赖

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

启动类

@SpringBootApplication
@EnableEurekaServer//配置服务注册中心
//@EnableDiscoveryClient//开启客户端注册
public class SpringCloudEurekaStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaStudyApplication.class, args);
    }

}

相关的配置

#续约任务调用间隔时间
eureka.instance.lease-renewal-interval-in-seconds=30
#失效时间
eureka.instance.lease-expiration-duration-in-seconds=90
#单机开发情况下容易触发保护机制导致,维护的注册列表不是实时准备
#默认开着,生产环境开着(产生原因一般是网络原因),会将当前的实例保存下来
#让其尽可能不过期,但是容易使消费端拿到不正确的注册列表,所以客户端一般要
#有容错的机制:断路器或者请求重试之类的
eureka.server.enable-self-preservation=false
#注册的实例名 默认:IdUtils.getDefaultInstanceId
eureka.instance.instance-id=${spring.application.name}:${server.port}
#region 分组 设置分组优先在分组内进行服务调用,可以根据地域网络之类的进行分组
eureka.client.region=guangzhou
#zone 分组
eureka.client.availability-zones.guangzhou=study

#注册中心
server.port=9000
eureka.instance.hostname=localhost
#是否向注册中心注册,如果是没有集群的话,没有必要
eureka.client.register-with-eureka=false
#是否检索注册的服务列表,如果是没有集群的话,没有必要
eureka.client.fetch-registry=false
#注册中心的地址,如果是没有集群的话,没有必要,如果有集群写其他的注册中心的地址,相应的eureka.client.register-with-eureka,eureka.client.fetch-registry 需要配置成true
eureka.client.service-url.defaultZone=http://ip:port/eureka/

Fegin provide service

其实就相当于一个 Web 应用整合 Eurake
gradle中的依赖

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

配置

  port: 9001
spring:
  application:
    name: user-feign-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:9000/eureka/
/**
 * @Auther:
 * @Date: 2021/4/13
 * @Description: UserController 可以作为服务提供者给别的服务调用
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @PostMapping("/info")
    public String userInfo(){
        return "userinfo";
    }
    @GetMapping("/list")
    public List<String> userList(){
        List<String> list = new ArrayList<>();
        list.add("YY");
        list.add("ZZ");
        return list;
    }
}

@SpringBootApplication
@EnableDiscoveryClient//表名开启Eureka客户端
public class SpringCloudFeignServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudFeignServiceApplication.class, args);
    }

}

Fegin consumer

ribbon 通过 RestTemplate 负载均衡的请求 其他service提供的服务ribbon
通过Feign 负载均衡和 hystrix(Feign内置)进行默认的的断路处理

import com.study.springcloudfeignapi.user.feign.fallback.UserFeignServiceFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Auther: ASUS
 * @Date: 2021/4/13
 * @Description: UserFeignService
 * @Version 1.0.0
 */
//path 是请求路径
//contextId 是别名
//value 代表服务的引用,如果有相同的定义的value 可以用contextId 进行区分,否则启动会报错
//这里直接定义service 调用的接口,feign 会将其和远程的服务关联起来(通过http的方式)
@FeignClient(value = "user-feign-service",path = "/user",contextId = "user",fallback = UserFeignServiceFallBack.class)
@Component
public interface UserFeignService {
    @PostMapping("/info")
    String userInfo();
}


/**
 * @Auther: ASUS
 * @Date: 2021/4/13
 * @Description: UserExtraFeignService
 * @Version 1.0.0
 */
@FeignClient(value = "user-feign-service",path = "/user",contextId = "userextra",fallback = UserExtraFeginServiceFallBack.class )
@Component
public interface UserExtraFeignService {
    @GetMapping("/list")
    List<String> userList();
}

服务降级处理实现之前定义的接口,对每个方法做降级的处理操作

/**
 * @Auther: ASUS
 * @Date: 2021/4/13
 * @Description: UserExtraFeginServiceFallBack 服务降级处理,当服务提供方无法提供服务的时候可以进行默认的返回处理
 * @Version 1.0.0
 */
@Service
public class UserExtraFeginServiceFallBack implements UserExtraFeignService {
    @Override
    public List<String> userList() {
        List<String> list = new ArrayList<>();
        list.add("UserExtraFeginServiceFallBack userList error");
        return list;
    }
}

/**
 * @Auther: ASUS
 * @Date: 2021/4/13
 * @Description: UserFeignServiceFallBack 服务降级类,当服务提供方无法提供服务的时候可以进行默认的返回处理
 * @Version 1.0.0
 */
@Service
public class UserFeignServiceFallBack implements UserFeignService {
    @Override
    public String userInfo() {
        return "UserFeignServiceFallBack info";
    }
}

feign http 发送请求时进行拦截处理,对请求信息进行封装RequestInterceptor 时Feign 提供的接口

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
/**
 * @Auther: ASUS
 * @Date: 2021/4/13
 * @Description: FeignRequestInterceptor 在发生Fegin http请求的时候 进行拦截设置一些参数
 * @Version 1.0.0
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        System.out.println("请求url:"+template.url()+"befor.....");
        template.header("toker","111");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值