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

创建openFegin 服务

  1. 复制order-ribbon,重命名为order-openfegin
  2. 删除order-ribbon.iml
  3. **更改artifactId <artifactId>order-openfegin</artifactId>
  4. 父pom文件中添加模块
<modules>
        <module>order</module>
        <module>stock</module>
        <module>order-nacos</module>
        <module>stock-nacos</module>
        <module>order-ribbon</module>
        <module>order-openfegin</module>
    </modules>
  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依赖

<!-- openfegin 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <!-- 父工程改为自己的父类 -->
    <parent>
        <groupId>com.yc</groupId>
        <artifactId>sunflower</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <artifactId>product-nacos</artifactId>
    
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

向父工程添加子模块

<modules>
        <module>order</module>
        <module>stock</module>
        <module>order-nacos</module>
        <module>stock-nacos</module>
        <module>order-ribbon</module>
        <module>order-openfegin</module>
        <module>product-nacos</module>
    </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.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </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]

spring-cloud-alibaba(五)Nacos-Config篇

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值