springboot 集成 openfeign、nacos

cs-custom
pom
<!-- nacos 服务注册中心-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- nacos 服务配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
    <groupId>com.gmy</groupId>
    <artifactId>cs-product-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

yml
spring:
  application:
    name: cs-custom # 微服务的名称

  cloud:
    nacos:
      discovery: # 服务注册中心
        server-addr: 111.111.111.111:9001
        namespace: c4ad03f3-bdc0-4c63-9f95-198040bf01e8
        username: nacos
        password: nacos
      config: # 服务配置中心
        server-addr: 111.111.111.111:9001
        namespace: c4ad03f3-bdc0-4c63-9f95-198040bf01e8


    # 启用nacos的权重策略,默认轮询
    loadbalancer:
      nacos:
        enabled: true
feign:
  client:
    config:
      # 设置全局超时时间
      default:
        connect-timeout: 10000 #请求连接的超时时间
        read-timeout: 60000 #请求处理的超时时间
      # 指定服务名称为服务单独配置超时时间
      cs-product:
        connect-timeout: 5000
        read-timeout: 5000
  okhttp:
    enabled: true
  httpclient:
    # 最大的连接数
    max-connections: 200
    # 每个url的最大连接数
    max-connections-per-route: 50

cs-product-api

pom

<!-- 声明式服务调用 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<!-- 负载均衡依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!--声明式服务连接池-->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

1.在调用微服务cs-custom启动类加@EnableFeignClients

2.cs-product Controller层开放方法端口

@GetMapping("/list")
public Result list(){
    return Result.success(productService.list());
}

3.在cs-product-api FeignProductService接口中加@FeignClient(value = "cs-product",path = "/product"),写接口方法 注:如有参数必须写名称

@FeignClient(value = "cs-product",path = "/product")
public interface FeignProductService {

    @GetMapping("/list")
    Result list();
}

4.cs-custom Controller层中 

@Autowired
private FeignProductService feignProductService;
@GetMapping("/feignProduct")
public Result list(){
    return  Result.success(feignProductService.list());
}

             

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值