目录

1.dataSource:

2. sericeImpl 使用 @Resource 导入的是 Dao接口,而controller导入的是  service接口

3.注意mapper文件中select中使用的只有parameterType=“Long”,而parameterMap已经被弃用 

否则会报错:没有 Long 这个类型

4.导入RestTemplate时,先在配置类中定义一个 Bean

5.如果使用Post 进行远程的插入操作时,需在远程的controller的方法的变量前面加上

         @requestParam与@RequestBody的区别

6.远程调用这个create方法 的第二个参数需为@RequestBody(一个方法只有一个) 后面的变量一致

7. zookeeper自带的jar与服务端的版本有冲突

8.指定参数 连接zookeeper服务端

9. 使用config分布式配置中心连接git时, 注意访问的路径

10. springcloud bus 进行广播通知时,一开始

11.出现NoUniqueBeanDefinitionException: No qualifying bean of type'org.springframework.messaging.MessageChannel' available: expected single matching bean but found 2: nullChannel,errorChannel

12.springcloud stream 无法连结rabbitmq org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

13.  1.1.4版本nacos将默认的数据库derby更换为8.0及其以上的数据库时,报错Caused by: java.lang.RuntimeException: Nacos Server did not start because dumpservice bean construction failure : No DataSource set

14. sentinel 中的 @SentinelResource(value = "byResource",blockHandler = "handleException")细节

15. SpringCloud整合Seata启动报no available server to connect

16. SpringCloud整合Seata+Nacos出现can not register RM,err:can not connect to services-server

17. 使用feign远程调用时,出现nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException

18.  feign远程调用 事务方法  时,status 405 reading AccountService # decrease (Long,BigDecimal) 

消费者远程调用服务者:消费者order模块   controller(@GetMapping())->service(服务者payment @PostMapping())-> 服务者payment模块 controller                 (  推荐使用 @RequestMapping() 或 @PostMapping() )

19. Spring Cloud组件总结


1.dataSource:


driver-class-name: com.mysql.cj.jdbc.Driver # 8.0及其以上的mysql驱动包 url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC #解决时区以及编码乱码问题


datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

2. sericeImpl 使用 @Resource 导入的是 Dao接口,而controller导入的是  service接口

public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao service;
public class PaymentController {
    @Resource
    private PaymentService paymentService;

3.注意mapper文件中select中使用的只有parameterType=“Long”,而parameterMap已经被弃用 

否则会报错:没有 Long 这个类型

<select id="getPaymentById" resultMap="BasePaymentMap" parameterType="Long">
        select * from payment where id=#{id};
    </select>

4.导入RestTemplate时,先在配置类中定义一个 Bean

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

5.如果使用Post 进行远程的插入操作时,需在远程的controller的方法的变量前面加上

@RequestBody   注解的作用:

主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),所以只能发送POST请求。

GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交

@RequestBody

@PostMapping(value = "/payment/create")

    public CommonResult create(@RequestBody Payment payment){

    //注意如果没有这个@RequestBody注解, 则将会使远程调用这个create方法能返回添加成功

    但是数据为 null

@requestParam与@RequestBody的区别

@requestParam 
1.用来获取URL后面追加的参数(所有的get请求,后端postman的 post请求)
2.POST请求,content-type:application/x-www-form-urlencoded 的body 中的参数
@requestBoby
1.接收POST ,content-type:application/json的body参数 (后端一般封装成 javaBean 对象处理,Map,JSONObject,不能是 Integer

6.远程调用这个create方法 的第二个参数需为@RequestBody(一个方法只有一个) 后面的变量一致

@GetMapping(value = "/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        //第二个参数请求 request: payment //添加的参数
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class,payment);
    }

7. zookeeper自带的jar与服务端的版本有冲突

<!-- SpringBoot整合zookeeper客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--先排除自带的zookeeper3.5.3 防止与3.8.0起冲突-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加zookeeper3.8.0版本-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.8.0</version>
        </dependency>

8.指定参数 连接zookeeper服务端

   连接ZooKeeper服务端

       ./zkCli.sh -server ip:port

  退出zookeeper客户端

        quit   ,CTRL+c

9. 使用config分布式配置中心连接git时, 注意访问的路径

application.yaml(用户配置文件)

cloud:
    config:
      server:
        git:
           # ssh 无用,具体原因未知
#         uri: git@github.com:jkl2073260375/springcloud-config.git #GitHub上面的git仓库名字
          #最好使用 http 或 https
         
          uri: https://github.com/jkl2073260375/springcloud-config.git
          
          ####搜索目录
          search-paths:
            - springcloud-config
#          skip-ssl-validation: true
          username: xxx
          password: xxx
      
      ####读取分支
      label: main

bootstrap.yaml(系统配置文件高于用户配置文件)

cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

2021年10月份及其之后已经将默认分支master 改为了main

所以访问的路径为:http://localhost:3344/main/config-test.yml

如果访问路径为:http://localhost:3344/master/config-test.yml 或

                             http://localhost:3344/config-test.yml

No such label: master
org.springframework.cloud.config.server.environment.NoSuchLabelException: No such label: master

因此分支省略还是默认为master,所以分支不可以省略 

10. springcloud bus 进行广播通知时,一开始

##rabbitmq相关配置,暴露bus刷新配置的端点<--------------------------
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'

广播全局通知 

curl -X POST "http://localhost:8001/actuator/bus-refresh"

广播定点通知 

curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:8002

总报错  "error":"Method Not Allowed","message":"Request method 'POST' not supported"

在网上找了好多常是都没有解决 

1、版本更新的缘故,新版本应该使用 /actuator/busrefresh

2、rabbitmq的依赖没有引入进去

3、yaml文件中格式没有对齐(不要忘记空格)

4、 ......

上面都没有解决我的问题,我将yaml配置文件中的 bus-refresh 的单引号变成双引号或者去掉单引号重新启动都是一样的报405,但是我又重新为bus-refresh添加单引号启动,竟然成功了,浪费了我一两个小时在寻找这种错误,它自然就好了。具体原因我也不知道,望有知道的大佬告知一二!

结论:遇到这种先重新启动idea,然后在自己去官网看看是如何操作的,实在找不到就自己慢慢动手摸索,不要一味的在百度找答案

11.出现NoUniqueBeanDefinitionException: No qualifying bean of type'org.springframework.messaging.MessageChannel' available: expected single matching bean but found 2: nullChannel,errorChannel

1、包可能导错误

导入此包:import javax.xml.transform.Source;

实际为:import org.springframework.cloud.stream.messaging.Source;

2、名称不一致

@Slf4j
@EnableBinding(Source.class)//定义消息的推送管道
public class StreamServiceImpl implements StreamService {

    @Resource
    private MessageChannel messageChannel;
实际名称为: output
      bindings: # 服务的整合处理
        output: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit # 设置要绑定的消息服务的具体设置

12.springcloud stream 无法连结rabbitmq org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

生产者和消费者原因 同理:能够启动,在注册中心也能够注册成功,程序实现代码没问题,就是控制台会报错Connection refused: connect 

解决:多加一份rabbitmq的服务信息

spring:
  application:
    name: cloud-stream-provider
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: xxx
                port: xxx
                username: xxx
                password: xxx
      bindings: # 服务的整合处理
        output: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit # 设置要绑定的消息服务的具体设置

  #如果启动该程序显示connect refused: connect 则还需要添加以下rabbitmq的服务信息
  rabbitmq:
    host: xxx
    port: xxx
    username: xxx
    password: xxx

13.  1.1.4版本nacos将默认的数据库derby更换为8.0及其以上的数据库时,报错Caused by: java.lang.RuntimeException: Nacos Server did not start because dumpservice bean construction failure : No DataSource set

nacos 1.1.14 连接mysql数据库包默认使用的是mysql-connector-java-5.1.34.jar,而我使用的是mysql-connector-java-8.0.17.jar

解决方案:请借鉴此文章 低版本nacos更换高版本的mysql数据库出现的问题

14. sentinel 中的 @SentinelResource(value = "byResource",blockHandler = "handleException")细节

blockHandler只有对 资源名  限流才有用,而对 url路径 限流无用。

当没有使用 blockHandler:

1、当使用 资源名(无 / )

2、当使用 url路径


Blocked by Sentinel (flow limiting)


15. SpringCloud整合Seata启动报no available server to connect

service {
  ##ld_tx_group是自定义的   my_test_tx_group
  #vgroup->rgroup
  vgroup_mapping.ld_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
cloud:
    alibaba:
      seata:
        #自定义事务组名称需要与seata-server中的对应
        tx-service-group: ld_tx_group

16. SpringCloud整合Seata+Nacos出现can not register RM,err:can not connect to services-server

seata−server运行时指定地址和端口

./seata-server.sh -h 127.0.0.1 -p 8091

17. 使用feign远程调用时,出现nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException

参数不匹配,请检查传入的参数是否与方法中需要的参数一致

18.  feign远程调用 事务方法

@FeignClient(value = "seata-account-service")
public interface AccountService {

    @PostMapping(value = "/account/decrease")
    CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money);
}
public class AccountController {
    @Resource
    AccountService accountService;

    //修改前 报错status 405 reading AccountService#decrease(Long,BigDecimal)
    //@GetMapping(value= "/account/decrease")
    //修改后   
    @RequestMapping(value = "/account/decrease")
    public CommonResult decrease(@RequestParam("userId") Long userId,@RequestParam("money") BigDecimal money){
        accountService.decrease(userId, money);

POST请求要 一 一 对应

@RequestMapping的请求方式
(1)如果方法上的@RequestMapping注解没有设置method属性,则get和post.put.push.delete.请求默认都可以访问。
(2)如果方法上的@RequestMapping注解设置了method属性,则只能是相应的请求方式可以访问。

DAO层尽量使用  @Mapper  注解代替  java 中的 四大注解

@RequestMapping ,@PostMapping,@GetMapping 注解的使用过程:

消费者远程调用服务者:消费者order模块   controller(@GetMapping())->service(服务者payment @PostMapping())-> 服务者payment模块 controller                 (  推荐使用 @RequestMapping() 或 @PostMapping() )

19. Spring Cloud组件总结

组件

简介

分类

说明

Eureka

Eureka is the Netflix Service Discovery Server and Client.

服务注册中心

eureka中文解释:int.(因找到某物,尤指问题的答案而高兴)我发现了,我找到了

Zookeeper

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.

服务注册中心

zookeeper中文解释:n.动物园管理员

Consul

Consul is a service mesh solution providing a full featured control plane with service discovery, configuration, and segmentation functionality.

服务注册中心

consul中文解释:n.领事

Ribbon

Ribbon is a client-side load balancer that gives you a lot of control over the behavior of HTTP and TCP clients.

服务调用

ribbon中文解释:n.(用于捆绑或装饰的)带子;丝带;带状物

OpenFeign

Feign is a declarative web service client. It makes writing web service clients easier.

服务调用

feign中文意思:v.假装,装作,佯装(有某种感觉或生病、疲倦等)

Hystrix

Netflix has created a library called Hystrix that implements the circuit breaker pattern.

服务降级

hystrix中文意思:n.豪猪属;猬草属;豪猪;豪猪亚属

GateWay

Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

服务网关

gateway中文意思:n.网关;途径;门道;手段

Config

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system.

github上读取配置文件

Bus

Spring Cloud Bus links nodes of a distributed system with a lightweight message broker.

服务总线

github配置文件被修改,配合rabbitmq进行广播通知

Stream

Spring Cloud Stream is a framework for building message-driven microservice applications.

消息队列

将rabbitmq与kabka进行封装,方便实现不同消息中间件转换问题

Sleuth

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud.

服务跟踪

sleuth中文意思:n.侦探

Nacos

Nacos致力于帮助您发现、配置和管理微服务。

服务注册中心、服务配置、


NAme + COnfiguration + Service

Sentinel

Sentinel是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来帮助您保障微服务的稳定性。

服务降级

sentinel中文意思:n.哨兵

Seata

Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

分布式事务

@GlobalTransation 全局事务

@Transation 本地事务

服务配置