raven-springcloud-nacos
|--spring-cloud-nacos目录结构
| |--raven-common
| | |--utils
| | |--constant
| |--raven-nacos
| |--raven-service
| | |--raven-driver-service
| | |--raven-order-service
| |--raven-service-api
| | |--raven-driver-service-api
| | |--raven-order-service-api
2.0.1.pom.xml
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.ravengroupId>
<artifactId>spring-cloud-nacosartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion
>
<module>raven-commonmodule>
<module>raven-nacosmodule>
<module>raven-servicemodule>
<module>raven-service-apimodule>
modules>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<skipTests>trueskipTests>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.4.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
2.1.1.存放整个项目公共文件,如utils工具类、contant常量等等
2.1.2.管理整个项目公共依赖,如mysql数据库、redis、mq等等
2.1.3.pom.xml
<dependencies>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>com.zhouyutonggroupId>
<artifactId>z-orm-jdbcartifactId>
<version>4.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.21version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
<scope>truescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-log4j2artifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-undertowartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
2.2.1.nacos的配置中心服务,可以用来获取在nacos中的配置信息
2.2.2.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>0.2.1.RELEASEversion>
dependency>
dependencies>
2.2.3.bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# Nacos 控制台添加配置:
# Data ID:example.properties
# Group:DEFAULT_GROUP
# 配置内容:useLocalCache=true
spring.application.name=config-server
# 指定配置的后缀,支持 properties、yaml、yml,默认为 properties
spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.file-extension=yaml
2.2.4.ConfigController
@RestController
@RequestMapping("/config")
// 动态的刷新config数据
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
/**
* http://localhost:8080/config/get
*/
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
2.3.1.管理整个项目业务相关微服务服务文件,如order服务,driver服务
2.3.2.各业务服务可以基于nacos进行注册发现,通过feign远程调用
2.3.3.各业务服务pom.xml
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.ravengroupId>
<artifactId>raven-driver-service-apiartifactId>
<version>${project.version}version>
dependency>
dependencies>
2.3.4.各业务服务application.yml
server:
port: 38943
spring:
application:
name: consumer
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/nacos?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false
username: dipao
password: dipao123
# nacos注册中心地址信息
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
feign:
hystrix:
enabled: true
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.edaijia.edipao.driver.entity
2.3.4.各业务服务启动类Application
@SpringBootApplication
//扫描Mapper
@MapperScan("com.raven.consumer.mapper")
/**
* 项目是多模块,包名不同,所以扫描不到 需要加basePackages
*/
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.raven.driver.feign"})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
2.3.5.各业务服务feign远程注入
@RestController()
@RequestMapping("/consumer")
public class ConsumerController
@Autowired
private DriverFeign driverFeign;
}
2.4.1.管理整个项目业务相关各微服务远程调用接口、实体类,如orderFeign、orderEntity、orderAddReq等等
2.4.2.各远程调用接口、实体类微服务pom.xml
<dependencies>
<dependency>
<groupId>com.ravengroupId>
<artifactId>raven-commonartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
dependencies>
2.4.3.各远程调用接口feign
/**
* @PackageName: com.raven.driver.feign
* @ClassName: DriverFeign
* @Blame: raven
* @Date: 2021-07-07 17:19
* @Description:
* FeignClient注解: 该接口对外提供远程调用,远程调用服务名称为driver
* 调用地址为类上的RequestMapping + 方法上的GetMapping
*/
@FeignClient("driver")
@RequestMapping("/driver")
public interface DriverFeign {
@GetMapping("/get/{id}")
public DriverEntity getOne(@PathVariable(value = "id") int id);
}
指定配置的后缀,支持 properties、yaml、yml,默认为 properties
spring.cloud.nacos.config.file-extension=properties
错误如下:
IllegalStateException: Ambiguous mapping. Cannot map 'xxx.XxxClient' method
Spring Feign 注入失败问题排查思路
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>0.2.1.RELEASEversion>
dependency>
------------------------------------2021-07-12更新-----------------------------
也可使用com.alibaba.cloud下的nacos config 依赖
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.1.RELEASEversion>
dependency>
项目中使用springcloud maven依赖无法导入
排查后发现,服务提供方和服务消费方采用了不同的分组配置,导致服务注册成功,但找不到
# 消费者
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 08c3e74b-5b7c-4924-a593-e4cf2712317f # 使用的 Nacos 的命名空间,默认为 null
group: raven-consumer-service
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 08c3e74b-5b7c-4924-a593-e4cf2712317f # 使用的 Nacos 的命名空间,默认为 null
group: raven-producer-service
3.6 Method has too many Body parameters的处理办法
feign书写时不通过@PathVariable拼接参数时,需要通过@RequestParam + @RequestBody拼接参数,RequestBody有且只能有一个 (可以省略不写)
// 正确书写方式如下:
// 1.
@ApiOperation(value = "根据条件构造器修改司机信息")
@RequestMapping(path = "/updateByCriteria")
public void updateByCriteria(BaseCriteriaDTO criteriaDTO);
// 2.
@ApiOperation(value = "根据司机手机号获取司机信息(指定查询字段)")
@RequestMapping(path = "/getByPhone/includeFields")
public DriverInfoEntity getByPhone(@RequestParam(value = "phone") String phone, List<String> includeFields);
// 错误书写方式1:
@ApiOperation(value = "根据条件构造器修改司机信息")
@RequestMapping(path = "/updateByCriteria")
public void updateByCriteria(Criteria criteria, Update update);
nacos会根据 namespace->group->dataId 找到唯一的配置文件,并进行加载。
当没有配置spring-cloud–nacos-config-name时,dataId = spring.application.name + file-extension(默认为properties)
当配置name后 dataId = name + file-extension = nacosConfig.yaml
如果在指定的namespace - group - dataId 中没有找到对应的配置 springboot就会报错
错误信息:
NACOS SocketTimeoutException httpGet] currentServerAddr:http://localhost:8848, err : connect timed out
原因:
bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等;
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 先于 application.yml 加载
处理方案:
application.yml 更改为bootstrap.yml即可