栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-cloud-alibaba(四)Nacos-OpenFegin篇

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-cloud-alibaba(四)Nacos-OpenFegin篇

Open-Fegin
  • 创建openFegin 服务
    • 创建OpenFegin 服务接口
    • 日志配置
      • 新建product-nacos
      • 配置全局日志
    • 超时时间配置

创建openFegin 服务
  1. 复制order-ribbon,重命名为order-openfegin
  2. 删除order-ribbon.iml
  3. **更改artifactId order-openfegin
  4. 父pom文件中添加模块

        order
        stock
        order-nacos
        stock-nacos
        order-ribbon
        order-openfegin
    
  1. 注释 RibbonRandomRuleConfig 类
  2. 注释RestTemplateConfig 类
  3. 注释 主启动类的负载均衡策略
@SpringBootApplication
//@RibbonClients(value = {
//        // 访问stock-service 服务 使用RibbonRandomRuleConfig 负载均衡类
//        @RibbonClient(name = "stock-service",configuration = RibbonRandomRuleConfig.class)
//})
//也可以使用此注释 配置单个服务 负载均衡
//@RibbonClient(name = "stock-service",configuration = RibbonRandomRuleConfig.class)
public class OrderNacosMain8011 {

   public static void main(String[] args) {
       SpringApplication.run(OrderNacosMain8011.class,args);
   }
}
创建OpenFegin 服务接口

添加pom依赖


        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

创建StockFeginService 服务接口

package com.yc.ordernacos.service;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "stock-service",path = "stock-nacos")
public interface StockFeginService {

    @RequestMapping("/stockReduct")
       String stockReduct();
}

更改OrderNacosController

@RestController
@RequestMapping("/order-nacos")
public class OrderNacosController {

    @Autowired
    StockFeginService feginService;
    
    @RequestMapping("/orderAdd")
    public String orderAdd(){

        String stockMsg = feginService.stockReduct();
        return "OpenFegin " +stockMsg;
    }
}

更改主启动类

@SpringBootApplication
//@RibbonClients(value = {
//        // 访问stock-service 服务 使用RibbonRandomRuleConfig 负载均衡类
//        @RibbonClient(name = "stock-service",configuration = RibbonRandomRuleConfig.class)
//})
//也可以使用此注释 配置单个服务 负载均衡
//@RibbonClient(name = "stock-service",configuration = RibbonRandomRuleConfig.class)
@EnableFeignClients
public class OrderNacosMain8011 {

    public static void main(String[] args) {
        SpringApplication.run(OrderNacosMain8011.class,args);
    }
}
### 更改端口8040 

```xml
server:
  port: 8040
  # 服务名称
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  1. 更改OrderNacosMain8011 类名为 OrderNacosMain8040
  2. 启动stock-nacos8022 8023
  3. 启动OrderNacosMain8040
  4. 访问 http://localhost:8040/order-nacos/orderAdd
日志配置 新建product-nacos



更改pom



    4.0.0
    
    
        com.yc
        sunflower
        0.0.1-SNAPSHOT
    
    
    product-nacos
    
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



向父工程添加子模块


        order
        stock
        order-nacos
        stock-nacos
        order-ribbon
        order-openfegin
        product-nacos
    

更改yml

server:
  port: 8013
spring:
  application:
    name: product-service
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
        

创建 ProductController

package com.yc.productnacos.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${server.port}")
    public String port;
    @RequestMapping("/{}id")
    public String get(@PathVariable String id){

        return "查询" + id +" 好商品成功。" + port;
    }
}

在order-openfegin中 创建ProductFeginService服务接口

@FeignClient(name = "product-service",path = "/product")
public interface ProductFeginService {
    @RequestMapping("/{id}")
      String get(@PathVariable("id") String id);//参数必须添加@PathVariable("id") 不能只写@PathVariable( ) “id” 否则启动会报错
    
}

在order-openfegin中 更改OrderNacosController

@RestController
@RequestMapping("/order-nacos")
public class OrderNacosController {

    @Autowired
    StockFeginService feginService;

    @Autowired
    ProductFeginService productFeginService; 

    @RequestMapping("/orderAdd")
    public String orderAdd(){

        String stockMsg = feginService.stockReduct();
        String product = productFeginService.get("100");
        return "StockOpenFegin " +stockMsg +  "  ProductOpenFegin"+product;
    }
}

访问http://localhost:8040/order-nacos/orderAdd
发现 500异常

在product-nacos pom 添加

 
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

访问http://localhost:8040/order-nacos/orderAdd
日志没有输出openfegin相关的日志

配置全局日志

在order-openfegin中新建FeginConfig

@Configuration
public class FeginConfig {

    @Bean
    public Logger.Level feginLoggerLevel(){
        return Logger.Level.FULL;
    }
}

访问http://localhost:8040/order-nacos/orderAdd
发现还是没有日志输出
日志配置
在order-openfegin中yml添加

#springboot 默认日志输出等级是info openfegin debug等级就不会输出
logging:
  level:
    com.yc.ordernacos.service: debug

日志打印成功


配置指定输出日志
更改注释FeginConfig

//@Configuration 注释掉
public class FeginConfig {

    @Bean
    public Logger.Level feginLoggerLevel(){
        return Logger.Level.FULL;
    }
}

更改ProductFeginService

@FeignClient(name = "product-service",path = "product",
        configuration = FeginConfig.class)//添加FeginConfig日志输出级别 使用自己配置的
public interface ProductFeginService {
        @RequestMapping("/{id}")
      String get(@PathVariable("id") String id);//参数必须添加@PathVariable("id") 不能只写@PathVariable( ) “id” 否则启动会报错
    
}

值输出8013

使用配置文件配置局部日志

#Fegin 日志局部配置
feign:
  client:
    config:
      product-service: #对应的微服务名
        logger-Level: BASIC
        
超时时间配置

yml中配置超时

#Fegin 日志局部配置
feign:
  client:
    config:
      stock-service: #对应的微服务名
        logger-Level: FULL
        # 链接超时时间 默认2秒
        connectTimeout: 5000
        # 请求处理超时时间 默认5秒
        readTimeout: 3000

更新ProductController 睡5秒

  @RequestMapping("/{id}")
    public String get(@PathVariable String id) throws InterruptedException {

        Thread.sleep(5000);
        return "查询" + id +" 好商品成功。" + port;
    }

前端异常

控制台 异常

java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_161]
	at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_161]
	at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_161]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826933.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号