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

spring-cloud-alibaba(四)Nacos-OpenFegin篇,第1张

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依赖


        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

创建StockFeginService 服务接口

package com.yc.ordernacos.service;


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

/**
 *  name 调用的服务名
 *  提供服务的Controller的 @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


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    
    <parent>
        <groupId>com.ycgroupId>
        <artifactId>sunflowerartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    
    <artifactId>product-nacosartifactId>
    
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

向父工程添加子模块

<modules>
        <module>ordermodule>
        <module>stockmodule>
        <module>order-nacosmodule>
        <module>stock-nacosmodule>
        <module>order-ribbonmodule>
        <module>order-openfeginmodule>
        <module>product-nacosmodule>
    modules>

更改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” 否则启动会报错
    /**
     * Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
     */
}

在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 添加

 <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>

访问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” 否则启动会报错
    /**
     * Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
     */
}

值输出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]

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/720791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存