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());
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)