- 创建openFegin 服务
- 创建OpenFegin 服务接口
- 日志配置
- 新建product-nacos
- 配置全局日志
- 超时时间配置
- 复制order-ribbon,重命名为order-openfegin
- 删除order-ribbon.iml
- **更改artifactId order-openfegin
- 父pom文件中添加模块
order stock order-nacos stock-nacos order-ribbon order-openfegin
- 注释 RibbonRandomRuleConfig 类
- 注释RestTemplateConfig 类
- 注释 主启动类的负载均衡策略
@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
- 更改OrderNacosMain8011 类名为 OrderNacosMain8040
- 启动stock-nacos8022 8023
- 启动OrderNacosMain8040
- 访问 http://localhost:8040/order-nacos/orderAdd
更改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]



