目录
1.boot方式nacos与openFeign集成
1.引入依赖
说明:参考下面依赖
<properties>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
<spring-boot.version>2.3.6.RELEASE</spring-boot.version>
<alibaba-fastjson.version>2.0.20</alibaba-fastjson.version>
</properties>
<dependencies>
<!-- 引入nacos配置中心依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${com.alibaba.boot.version}</version>
</dependency>
<!-- 引入注册中心依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${com.alibaba.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${alibaba-fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.添加配置
原文参考:SpringCloud-Alibaba-Nacos配置项详解_nacos配置文件详解_JavaMN的博客-CSDN博客
3.测试接口调用
4.常见问题:
1.版本依赖
版本如果不匹配也是会出现一些乱七八糟的问题,参照我上面的版本来问题就可以正常使用
2.nacos客户端
说明:java.lang.NoClassDefFoundError: com/alibaba/nacos/client/utils/StringUtils
出现这个异常时候请把下面这个依赖加进去,这里需要注意版本,否则会有连接不上的情况
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.4</version>
</dependency>
参考: springboot+openFeign+nacos开发实战_openfeign整合nacos_wh柒八九的博客-CSDN博客
2.cloud方式nacos与openFeign集成
1.引入依赖
重要的事情说三遍。。。。
版本信息非常重要,一定要对应上!!!
版本信息非常重要,一定要对应上!!!
版本信息非常重要,一定要对应上!!!
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<dependencyManagement>
<dependencies>
<!-- SpringCloud 微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- https://segmentfault.com/q/1010000021044375?bd_source_light=4746641 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<exclusions>
<exclusion>
<artifactId>jackson-dataformat-xml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2.添加配置
使用cloud方式需要换成bootstrap文件,我选择bootstrap.yaml
server:
port: 9427
nacos:
server-addr: 127.0.0.1:8848
#local环境
namespace: 4e335d42-4caa-49b0-b209-0363e1a2071c
config-group: ocpx
# 服务只能同组调用 https://developer.aliyun.com/ask/317473
discovery-group: delivery
spring:
application:
name: delivery-ocpx
profiles:
active: ${BOOT_ENV}
cloud:
nacos:
discovery:
server-addr: ${nacos.server-addr}
namespace: ${nacos.namespace}
group: ${nacos.discovery-group}
service: ${spring.application.name}
config:
server-addr: ${nacos.server-addr}
namespace: ${nacos.namespace}
group: ${nacos.config-group}
file-extension: yml
extension-configs:
- data-id: ocpx-config.yml
group: ${nacos.config-group}
refresh: true
3.接口定义
请保持和目标服务的请求接口一致
package com.hhmt.delivery.chain.service;
import com.hhmt.delivery.continer.ServiceMode;
import com.hhmt.delivery.core.domain.ResultVo;
import com.hhmt.delivery.core.page.TableDataInfo;
import com.hhmt.delivery.pojo.model.query.HhChainApiInfoQuery;
import com.hhmt.delivery.pojo.model.vo.HhChainApiInfoVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 链路api信息Service接口
*
* @author hauchun
* @date 2023-02-09
*/
@FeignClient(name = ServiceMode.CHAIN_SERVICE_NAME, path = "/chain/HhChainApiInfo")
public interface IHhChainApiInfoService {
/**
* 查询链路api信息
*
* @param id 链路api信息主键
* @return 链路api信息
*/
@GetMapping(value = "/{id}")
public ResultVo<HhChainApiInfoVo> selectHhChainApiInfoById(@PathVariable("id") Long id);
/**
* 查询链路api信息列表
*
* @param hhChainApiInfo 链路api信息
* @return 链路api信息集合
*/
@GetMapping("/list")
public TableDataInfo<HhChainApiInfoVo> selectHhChainApiInfoList(@RequestParam("hhChainApiInfo") HhChainApiInfoQuery hhChainApiInfo);
}
需要注意的是这里的接口入参,虽然在目标服务中可以不使用@RequestParam去定义,但是在这里是需要带上的,否则参数绑定不上
4.开启FeignClients客户端
package com.hhmt.delivery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DeliveryOcpxApplication {
public static void main(String[] args) {
SpringApplication.run(DeliveryOcpxApplication.class, args);
}
}
注意需要添加的注解:
在启动类添加这两个注解: 1.@EnableFeignClients 启用Feign客户端 2.@EnableDiscoveryClient 开启注册功能,可以将当前服务注册进注册中心
5.远程接口测试
在当前服务定义两个接口,通过调用feign定义接口,测试目标服务列表和详情接口是否可以正常调用
1.编写控制器
package com.hhmt.delivery.chain.controller;
import com.hhmt.delivery.chain.service.IHhChainApiInfoService;
import com.hhmt.delivery.core.domain.ResultVo;
import com.hhmt.delivery.core.page.TableDataInfo;
import com.hhmt.delivery.pojo.model.query.HhChainApiInfoQuery;
import com.hhmt.delivery.pojo.model.vo.HhChainApiInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author huachun
* @version 1.0
* @description: TODO
* @email huachun_w@163.com
* @date 2023-03-01 11:40
*/
@RestController
@RequestMapping("/chain/HhChainApiInfo")
public class HhChainApiInfoController {
@Autowired
private IHhChainApiInfoService hhChainApiInfoService;
@GetMapping("/list")
public TableDataInfo<HhChainApiInfoVo> chainApiList() {
TableDataInfo<HhChainApiInfoVo> chainApiInfoVoTableDataInfo = hhChainApiInfoService.selectHhChainApiInfoList(new HhChainApiInfoQuery());
return chainApiInfoVoTableDataInfo;
}
@GetMapping("/{id}")
public ResultVo<HhChainApiInfoVo> byId(@PathVariable("id") Long id) {
ResultVo<HhChainApiInfoVo> chainApiInfoVoResultVo = hhChainApiInfoService.selectHhChainApiInfoById(id);
return chainApiInfoVoResultVo;
}
}
2.接口调用测试
列表接口测试
详情接口测试
至此,openFeign远程调用功能已经完成
6.Nacos配置中心
1.需要指定当前类使用的是哪个配置(如果配置文件与规则不相匹配)
@NacosConfigurationProperties(dataId = "delivery-ocpx.yml", groupId = "ocpx")
2.使用value注解替换NacosValue注解 @Value
示例如下图所示:
3.常见问题
1.Get请求接口带参数无法传递
我在客户端定义了feign接口,想要调用服务端接口
这是客户端feign接口,此时和服务端接口保持了一致
服务端接口
此时虽然接口可以正常调用,但是用来过滤的条件参数却无法传递
此时调用客户端接口测试
服务端此时接受不到参数
解决办法:在客户端使用 @SpringQueryMap注解
更换注解后问题完美解决