nacos配置:
在父项目的pom文件中引入cloud,要注意版本的冲突问题
<dependencyManagement> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.7.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.12.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement>
在服务提供者的pom文件中引入:
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!--<version>2.2.1.RELEASE</version> --> <exclusions> <!-- 不使用Ribbon 进行客户端负载均衡--> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
服务提供者application.yml配置:
spring: application: name: ben-service cloud: nacos: discovery: # 指定Nacos服务的地址,默认值是localhost:8848 server-addr: 127.0.0.1:8848 # 指定Nacos 管理界面登录用户名和密码,默认值是nacos username: nacos password: nacos # 指定命名空间 # 命名空间的作用是可以隔离不同的服务实例,默认名称是public namespace: public service: ${spring.application.name} # 注册到 Nacos 的服务名。默认值为 ${spring.application.name}。
Feign接口端也需要跟服务提供者一样配置nacos,并且引入feign:
<!-- 開啟springmvc功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <!-- 不使用Ribbon 进行客户端负载均衡--> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
Feign接口端的yml文件配置:
server: port: 8082 spring: application: name: ben-common cloud: nacos: discovery: # 指定Nacos服务的地址,默认值是localhost:8848 server-addr: 127.0.0.1:8848 # 指定Nacos 管理界面登录用户名和密码,默认值是nacos username: nacos password: nacos # 指定命名空间 # 命名空间的作用是可以隔离不同的服务实例,默认名称是public namespace: public # 暴露监控端点 management: endpoints: web: exposure: include: '*'
Feign接口端的启动类:
package com.ben.common; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients//(basePackages = "com.ben.common.feign.*") public class CommonApplication { public static void main(String[] args) { SpringApplication.run(CommonApplication.class, args); } }
Feign接口端的feign接口:
package com.ben.common.feign; import java.util.List; import java.util.Map; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.ben.common.entity.GridResponseInfo; import com.ben.common.entity.Supplier; //url = "http://localhost:8081" 当没有引入注册中心时,用url进行调用 @FeignClient(name ="ben-service") public interface SupplierFeign { @RequestMapping(value = "/supplier/getList/{suppId}/{rows}/{page}",method= {RequestMethod.GET}) public GridResponseInfo<Supplier> getSuppList(@PathVariable("suppId") String suppId, @PathVariable("rows") int rows,@PathVariable("page") int page); }
问题1.nacos无法发现服务
一、介绍
以下是问题分析
- nacos服务器启动正常。
- bootstrap.yml配置正常。
- 项目启动正常。
- nacos
无法找到服务
。二、解决方案
这种情况输入缺少依赖,只需要在项目中导入依赖即可:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
nacos配置相关及遇到的问题
最新推荐文章于 2023-04-19 10:35:38 发布