spring boot配合nacos实现feign的服务发现
spring boot使用nacos作为服务发现报错
·
nacos启动
参考官网启动,账号和密码默认为nacos
创建提供提供者
创建openfegin提供者,fallback进行降级处理
``
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* @author jxc
* 2022/8/12
*/
@FeignClient(name = Constants.SERVER, path = Constants.BASE_PATH, fallbackFactory = DefaultTestBizClient.class)
public interface TestBizClient {
/**
* <pre>
* 测试表列表
* </pre>
*
* @param query 查询条件
* @return 测试列表
*/
@GetMapping("tests")
PageResult<List<TestVO>> queryPage(@SpringQueryMap TestBizQuery query);
/**
* <pre>
* 测试表新增
* </pre>
*
* @param req 入参
* @return id
*/
@PostMapping("tests")
Resp<Integer> saveTest(@RequestBody @Valid TestBizReq req);
/**
* <pre>
* 测试表更新
* </pre>
*
* @param req 入参
* @return 更新行数
*/
@PutMapping("tests")
Resp<Integer> updateTest(@RequestBody @Validated(Update.class) TestBizReq req);
/**
* <pre>
* 测试表删除
* </pre>
*
* @param testId id
* @return 更新行数
*/
@DeleteMapping("tests/{testId}")
Resp<Integer> deleteById(@PathVariable("testId") Integer testId);
}
pom添加nacos的配置和启动文件增加配置
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
调用者
启动类添加
@EnableDiscoveryClient
@EnableFeignClients
pom配置添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
配置文件添加
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
注意事项
如果提示负载均衡客户端选择错误,主要是包冲出,排查很久了参考了文章解决问题。https://blog.csdn.net/weixin_51626435/article/details/115012925
java.lang.AbstractMethodError: Receiver class org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient does not define or inherit an implementation of the resolved method 'abstract org.springframework.cloud.client.ServiceInstance choose(java.lang.String, org.springframework.cloud.client.loadbalancer.Request)' of interface org.springframework.cloud.client.loadbalancer.ServiceInstanceChoose
引入包的时候进行排除
更多推荐
所有评论(0)