1 Star 0 Fork 0

Mr.Zhou / springcloud-alibaba-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

配置版本

  1. jdk 8
  2. docker v20.xx
  3. springcloud alibaba 2.2.1.RELEASE
  4. springboot 2.3.0.RELEASE
  5. mysql 5.7
  6. redis 6.x
  7. elk(es logpush kibana)7.6.2
  8. kafka 3.2
  9. mongo latest
  10. nacos-server lastest
  11. bladex/sentinel-dashboard lasttest

日志

2022-10-31

1.分布式uuid生成

​ 模块:generator-uuid-9999

1.通过redis生成

2.通过mysql生成

3.通过snowflake雪花算法生成

4.通过百度uid-generator生成

5.通过美团 Leaf-snowflake生成

2022-10-08

1.springboot-starter 自定义启动

参考项目:config-sprintboot-starter

  1. maven 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 自定义配置包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--添加了该模块以来的模块,再配置yml时候会有本模块配置属性的提示信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
          
  2. 自定义Properties类 加载 application.yml配置

  3. 之定义 Configuration类,负责加载properties,和bean创建

  4. 自定义服务

  5. 在resources文件夹下创建META-INF文件夹,并创建spring.factories文件

    #-------starter自动装配---------
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhou.cloud.config.ZhouConfig
  6. mvn install 打包

  7. 其他项目引用上面的包,调用服务

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = ConfigServer3344.class)
    @Component
    public class BaseTest implements ApplicationContextAware{
        @Autowired
        ZhouConfig zhouConfig;
        @Test
        public void test() {
            System.out.println(zhouConfig.getZhouService().test());
    
        }
    }

2022-09-26

1.高并发-限流 漏桶/令牌

详细部署查看 在 consumer-80工程 test com.zhou.test.concurrency.limiter目录下

限流方式:

  • 方案一:令牌桶方式(Token Bucket) Guava RateLimiter

  • 方案二:漏桶方式

  • 方案三:计数器方式

    • 采用AtomicInteger

      使用AomicInteger来进行统计当前正在并发执行的次数,如果超过域值就简单粗暴的直接响应给用户,说明系统繁忙,请稍后再试或其它跟业务相关的信息。 弊端:使用 AomicInteger 简单粗暴超过域值就拒绝请求,可能只是瞬时的请求量高,也会拒绝请求。

    • 采用ThreadPoolExecutor java线程池 固定线程池大小,超出固定先线程池和最大的线程数,拒绝线程请求;

2022-09-24

1.docker-compose部署 elk

详细部署查看 在 es-9001工程下 docker和readme.md

2022-09-07

1.支持nacos-config配置资源

项目:consumer-nacos-config-10081

  1. maven

    <!-- 使用Nacos Config -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
  2. bootstrap.yml

    bootstrap.yml文件会先于application.yml。在这里配置的是获取nacos配置的相关信息。

    spring:
      application:
        name: consumer
      profiles:
        # 配置中心存放的对应文件命名规则为: spring.application.name-spring.profiles.active
        active: dev
      cloud:
        nacos:
          config:
            server-addr: 127.0.0.1:8848
              #配置中心对应的配置文件所属的组,稍后在Nacos中创建文件时对应。
              #注意group里不要配置生成文件夹无法创建的 特殊字段,比如 : 会报错
            group: consumer_10081
            file-extension: yaml
  3. nacos config 配置

  4. 启动

    @SpringBootApplication
    @EnableDiscoveryClient
    public class Consumer_10081 {
        public static void main(String[] args) {
            SpringApplication.run(Consumer_10081.class,args);
        }
    }
  5. 注意

    • 远程加载类 com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder

    • 加载文件检查

      dataId[consumer] & group[consumer_10081] dataId[consumer.yaml] & group[consumer_10081]

      dataId[consumer-dev.yaml] & group[consumer_10081]

2022-09-02

1.支持 shardingspphere 分库分表(有问题)

​ 项目:provider-8003-shardingsphere

https://pdai.tech/md/spring/springboot/springboot-x-mysql-shardingjdbc.html#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%88%86%E8%A1%A8%E5%88%86%E5%BA%93

  1. maven

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.1.1</version>
    </dependency>
  2. application.yml

spring:
    sharding:
      tables:
        dept:
          actual-data-nodes: ds.dept_$->{0..1}
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: dept_$->{id % 2}
          key-generator:
            column: id
            type: SNOWFLAKE
            props:
              worker:
                id: 123
      binding-tables: dept
      broadcast-tables: address
  1. 问题:

数据库没有连上: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

2022-08-31

1.支持mybatis+pagehelper分页

​ Provider8001项目

  1. maven

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <!-- mybatis 支持-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <!--pagehelper 支持  -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.12</version>
    </dependency>
  2. application.yml配置

    spring:
      datasource:
        driver-class-name: org.gjt.mm.mysql.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3307/springcloud-alibaba-demo2?userUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
        
    mybatis:
      mapper-locations: classpath:mybatis/mapper/*.xml
      type-aliases-package: com.zhou.cloud.dao
    
    pagehelper:
      helper-dialect: mysql
      reasonable: true
      support-methods-arguments: true
      params: count=countSql
    
  3. service层调用

        @Override
        public List<Dept> getListByPage(int page, int pageNum) {
    //        PageHelper.offsetPage(page,pageNum);
    		//分页配置,使用绑定ThreadLocle,遇到的第一个select会触发分页
            PageHelper.startPage(page,pageNum);
            return deptMapper.selectAll();
        }
  4. 请求连接

    http://localhost:8001/dept/list/page?page=2&pageNum=3

  5. 参考连接

    https://blog.csdn.net/weixin_44307065/article/details/109473284

以下内容 日志流程 实现了 nginx(单例)+gateway(集群)+consomer(集群)+prodiver(集群)+mysql(单例)+kafka(单例) 配置流程

2022-08-26

1.昨天nginx问题
  1. 原因: proxy_pass http://localhost:10000; 配置地址不行,因为是docker安装所以,访问localhost就他自己本身,所以走不到
  2. 解决办法:暂时不用docker安装的nginx,以后再说,使用 本地安装的nginx
2. 使用nginx 实现gateway负载均衡
  1. nginx.conf配置如下

    #负载均衡配置
    upstream gateway {
            server localhost:10000;
            server localhost:10002;
        }
          server {
        # server全局块
                listen       20001;
                server_name  localhost;
    
                #charset koi8-r;
    
                #access_log  logs/host.access.log  main;
    
        # location块
                location /gateway {
                    ###############添加这3行
                    proxy_buffer_size 64k;
                    proxy_buffers  32 32k;
                    proxy_busy_buffers_size 128k;
                    ###############添加这3行
                    #proxy_set_header Host $host;
                    proxy_set_header X-Real-IP    $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                # 使用 proxy_pass(固定写法)后面跟要代理服务器地址            
                    proxy_pass http://gateway;  
                    proxy_redirect http:// $scheme://;
    
                }
        }
  2. gateway服务器 路由 application.yml配置:

        gateway:
          discovery:
            locator:
              enabled: true
          routes:
            - id: order #路由的ID 保证唯一
              uri: http://localhost:10080 # 目标服务地址:uri以lb://开头(lb代表从注册中心获取服务),后面就是需要转发到的服务名称
              predicates:
                # 断言,路径相匹配的进行路由 (谓词)
                - Path=/gateway/** # 路径匹配
              filters:
                - StripPrefix=1 # 转发去掉第一层路径

2022-08-25

1.nginx配置 实现 gateway支持群组访问

​ 待解决问题: 访问gateway服务器失败

  1. docker nginx

    1. docker pull nginx

    2. 启动

      docker run -d -p 80:80 --name nginx -v F:/docker/nginx/www:/usr/share/nginx/html -v F:/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v F:/docker/nginx/logs:/var/log/nginx nginx
    3. nginx.conf

    # 全局快
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    
    # events块
    events {
        worker_connections  1024;
    }
    
    # http块 
    http {
    # http全局块
        include       mime.types;
        default_type  application/octet-stream;
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        #keepalive_timeout  0;
        keepalive_timeout  65;
        #gzip  on;        
        # server块
        server {
        # server全局块
                listen       80;
                server_name  localhost;
                #charset koi8-r;
                #access_log  logs/host.access.log  main;
        # location块
                location /gateway/** {
                    ###############添加这3行
                    proxy_buffer_size 64k;
                    proxy_buffers  32 32k;
                    proxy_busy_buffers_size 128k;
                    ###############添加这3行
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP    $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                # 使用 proxy_pass(固定写法)后面跟要代理服务器地址            
                    proxy_pass http://localhost:10000;  
                    proxy_redirect http:// $scheme://;
                }
        }
    }
    
2.springcloud gateway
  1. 解决启动时候application.yml无效问题

    原因:是build后在丢失了application.yml

    代码断点处:

    org.springframework.boot.context.config.ConfigFileApplicationListener#loadForFileExtension
      load(loader, prefix + fileExtension, profile, profileFilter, consumer); 断点查看 prefix=classpath:/application fileExtension=yml
     

    解决办法:将application.yml拷贝到target\classes目录下

  2. gateway支持 nacos 群组模式

    • maven

      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-gateway</artifactId>
              <version>2.2.1.RELEASE</version>
              <exclusions>
                  <exclusion>
                      <artifactId>spring-cloud-starter</artifactId>
                      <groupId>org.springframework.cloud</groupId>
                  </exclusion>
              </exclusions>
          </dependency>
          <dependency>
              <groupId>com.alibaba.cloud</groupId>
              <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
              <exclusions>
                  <exclusion>
                      <artifactId>guava</artifactId>
                      <groupId>com.google.guava</groupId>
                  </exclusion>
                  <exclusion>
                      <artifactId>servo-core</artifactId>
                      <groupId>com.netflix.servo</groupId>
                  </exclusion>
                  <exclusion>
                      <artifactId>archaius-core</artifactId>
                      <groupId>com.netflix.archaius</groupId>
                  </exclusion>
              </exclusions>
          </dependency>
      </dependencies>
    • application.yml配置

    server:
      port: 10000
    
    spring:
      application:
        name: gateway-service
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848 #nacos服务地址
    
        gateway:
          discovery:
            locator:
              enabled: true
          routes:
            - id: order #路由的ID 保证唯一
              uri: http://localhost:10080 # 目标服务地址:uri以lb://开头(lb代表从注册中心获取服务),后面就是需要转发到的服务名称
              predicates:
                # 断言,路径相匹配的进行路由 (谓词)
                - Path=/gateway/** # 路径匹配
              filters:
                - StripPrefix=1 # 转发去掉第一层路径
    • 代码 启动注解:@EnableDiscoveryClient

2022-08-19

1.springcloud gateway
  1. 关联module:gateway-10001和10000

  2. maven

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
                <version>2.2.1.RELEASE</version>
            </dependency>
  3. application.yml

    server:
      port: 10001
    
    spring:
      application:
        name: gateway-service
      cloud:
        gateway:
          routes:
            - id: order #路由的ID 保证唯一
              uri: http://localhost:10080 # 目标服务地址:uri以lb://开头(lb代表从注册中心获取服务),后面就是需要转发到的服务名称
              predicates:
                # 断言,路径相匹配的进行路由 (谓词)
                - Path=/consumer/** # 路径匹配
    #          filters:
    #            - StripPrefix=1 # 转发去掉第一层路径
  4. 启动

  5. 测试

    http://localhost:10001/consumer/dept/list

    ​ 测试成功

2022-08-15

1.springcloud gateway
  1. 关联module:gateway-10000

  2. maven

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
                <version>2.2.1.RELEASE</version>
            </dependency>
  3. application

    spring:
      profiles:
        active: dev
    ---
    server:
      port: 10000
    spring:
      profiles: dev
      application:
        name: gateway
      #nacos配置
      cloud:
        gateway:
          routes:
            - id: order #路由的ID 保证唯一
              uri: http://localhost:10080 # 目标服务地址:uri以lb://开头(lb代表从注册中心获取服务),后面就是需要转发到的服务名称
              predicates:
                # 断言,路径相匹配的进行路由 (谓词)
                - Path=/consumer/** # 路径匹配
    #          filters:
    #            - StripPrefix=1 # 转发去掉第一层路径
  4. controller

  5. 问题

    1. 启动时问题提示:Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time

    2. 原因:

      springcloudgateway的内部是通过netty+webflux实现的,webflux实现和springmvc配置依赖冲突

    3. 解决: 屏蔽下面的依赖,保证没有web依赖

      <!-- <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>-->

2022-07-29

1.kafka
  1. maven
<!-- kafka -->
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>2.5.0.RELEASE</version>
</dependency>
  1. 生产者 provider

    1. application.yml 配置

      server:
        port: 9001
      spring:
        application:
          name: provider-kafka
        kafka:
          ###########【Kafka集群】###########
          bootstrap-servers: 127.0.0.1:9092
          listener:
            # 消费端监听的topic不存在时,项目启动会报错(关掉)
            missing-topics-fatal: false
            # 设置批量消费
      #      type: batch
          ###########【初始化生产者配置】###########
          producer:
            # 重试次数
            # 如果开启事务或者触发 用户显式地指定了 retries 参数,那么这个参数的值必须大于0
            retries: 1
            # 应答级别:多少个分区副本备份完成时向生产者发送ack确认(可选0、1、all/-1)
            # 如果开启事务或者触发 幂等 asks必须= -1
            acks: 1
            # 批量大小
            batch-size: 16384
            properties:
              # 提交延时
              # 当生产端积累的消息达到batch-size或接收到消息linger.ms后,生产者就会将消息提交给kafka
              # linger.ms为0表示每接收到一条消息就提交给kafka,这时候batch-size其实就没用了
              linger:
                ms: 0
                # 自定义分区器
              partitioner:
                # 自定义分区器
                class: CustomizePartitioner
            # 生产端缓冲区大小
            buffer-memory: 33554432
            # Kafka提供的序列化和反序列化类
            key-serializer: org.apache.kafka.common.serialization.StringSerializer
            value-serializer: org.apache.kafka.common.serialization.StringSerializer
            # 加事务前缀,自动给producer开启事务
      #      transaction-id-prefix: tx_
         
    2. 代码生产消息

      @Service
      @Slf4j
      public class KafkaProducerService {
          @Autowired
          private KafkaTemplate<String, Object> kafkaTemplate;
      
          public void sendMessage( String normalMessage) {
              String sendTopic = "topic1";
              kafkaTemplate.send(sendTopic, normalMessage);
              log.warn("生产消息:topic={},message={}",sendTopic,normalMessage);
          }
          }
  2. 消费者 consumer

    1. application.yml 配置
    server:
      port: 9100
    spring:
      application:
        name: consume-kafka
      kafka:
        ###########【Kafka集群】###########
        bootstrap-servers: 127.0.0.1:9092
        listener:
          # 消费端监听的topic不存在时,项目启动会报错(关掉)
          missing-topics-fatal: false
          # 设置批量消费
    #      type: batch
    ###########【初始化消费者配置】###########
        consumer:
          properties:
            # 默认的消费组ID
            group:
              id: defaultConsumerGroup1
              # 消费会话超时时间(超过这个时间consumer没有发送心跳,就会触发rebalance操作)
            session:
              timeout:
                ms:120000
              # 消费请求超时时间
            request:
              timeout:
                ms:180000
          enable-auto-commit: true
          auto:
            commit:
              interval:
                ms: 1000
          # 当kafka中没有初始offset或offset超出范围时将自动重置offset
          # earliest:重置为分区中最小的offset;
          # latest:重置为分区中最新的offset(消费分区中新产生的数据);
          # none:只要有一个分区不存在已提交的offset,就抛出异常;
          auto-offset-reset: latest
          # Kafka提供的序列化和反序列化类
          key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
          value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
          # 批量消费每次最多消费多少条消息
        #  max-poll-records: 50
    1. 参考文档

      ​ 参考文档: https://mp.weixin.qq.com/s/JvKPbvJ7q0ypqfF3ARBW7Q

    2. 问题

      1. 使用事务时候:Producer factory does not support transactions

        1. 异常原因

          使用kafka事务发送消息的时候没有开启事务

          1. 解决方案

          加事务前缀,自动给producer开启事务,所有加

          1. https://blog.csdn.net/qq_39654841/article/details/122967154
      2. 使用事务时候:Must set acks to all in order to use the idempotent

        1. 异常原因

          触发了幂等性功能正常执行时候,如果用户显式地指定了 acks 参数,那么还需要保证这个参数的值为 -1(all),如果不为 -1(这个参数的值默认为1)

          1. 解决办法

          acks 设置为-1 或者或者不配置

2022-07-22

1. sentinel
  1. 参考稳定

    ​ 官方文档:https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8

    ​ 注解说明:https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81

  2. docker部署

    docker search sentinel
    docker pull bladex/sentinel-dashboard
    docker run --name sentinel-dashboard -p 8858:8858 -d bladex/sentinel-dashboard:latest               
    localhost:8858
    用户名/密码:sentinel sentinel
  3. maven

    <!-- sentinel -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
  4. @SentinelResource 使用

    @SentinelResource(value = "provider-list",fallback = "listFallback")
    public List<Dept> list() {
        List<Dept> list = deptService.getAll();
        System.out.println(list);
        return list;
    }
    public List<Dept> listFallback(){
            String msg = "listfallback ...";
            log.warn("<<<<fallback");
            return null;
        }
        
    
    @SentinelResource(value = "test",blockHandler = "testBlockHandler")
    public String test() {
    	return "test";
    }
        

2.kafka+stream
  1. docker 部署

    1. 自定义 网络

      docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
    2. 安装zookeeper(使用自定义网络)

      docker pull zookeeper:3.6.1
      
      docker run -d -p 2181:2181 --net mynet   --restart=always  --name zookeeper -v F:\docker\zookeeper\conf:/apache-zookeeper-3.6.1-bin/conf -v F:\docker\zookeeper\logs:/apache-zookeeper-3.6.1-bin/logs zookeeper:3.6.1
    3. kafka (使用自定义网络)

      搜索版本 https://hub.docker.com/

      docker pull bitnami/kafka:3.2
      
      docker run -d --restart=always --name kafka -p 9092:9092 --net mynet -e KAFKA_BROKER_ID=1 -e KAFKA_auto_create_topics_enable=true  -e KAFKA_HEAP_OPTS="-Xmx256M -Xms128M" -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -e ALLOW_PLAINTEXT_LISTENER=yes  -t bitnami/kafka:3.2
      
      
      //配置分组id
      KAFKA_BROKER_ID=1
      
      //开启自动创建主题(不然代码整合服务后启动报错,必须自己手动到服务上创建)
      KAFKA_auto_create_topics_enable=true
      
      //连接zookeeper
      KAFKA_ZOOKEEPER_CONNECT=公网ip:2181
      
      //默认内存1G自己服务器太小,调小一点不然启动报错内存溢出(此处也比较坑)
      KAFKA_HEAP_OPTS="-Xmx256M -Xms128M"
      
      //配置外网ip访问kafka
      KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://公网ip:9092
  2. 同springboot版本兼容

    兼容版本

    1. maven

      <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-stream</artifactId>
                  <version>3.0.2.RELEASE</version>
              </dependency>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-stream-binder-kafka</artifactId>
                  <version>3.0.2.RELEASE</version>
         </dependency>
    2. 生产者配置

      1. application.yml配置

        spring:
          cloud:
            stream:
              kafka:
                binder:
                  brokers: localhost:9092  #Kafka的消息中间件服务器
                  zk-nodes: localhost:2181 #Zookeeper的节点,如果集群,后面加,号分隔
                  autoCreateTopics: true #如果设置为false,就不会自动创建Topic 有可能你Topic还没创建就直接调用了
                  requiredAcks: 1
                  autoAddPartitions: true
              	bindings:
                  output:      #这里用stream给我们提供的默认output,后面会讲到自定义output
                    destination: stream-demo    #消息发往的目的地
                    content-type: text/plain    #消息发送的格式,接收端不用指定格式,但是发送端要
      2. 生产消息

        package com.zhou.cloud.config.kafka;
        
        /**
         * @descrition: 消息发送端
         * @author: zhouyongjun
         * @date: 2022/7/22
         **/
        import lombok.extern.slf4j.Slf4j;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.cloud.stream.annotation.EnableBinding;
        import org.springframework.cloud.stream.messaging.Source;
        import org.springframework.messaging.support.MessageBuilder;
        import org.springframework.stereotype.Component;
        
        @Slf4j
        @Component
        @EnableBinding(Source.class)
        public class KafkaMessageSender {
            @Autowired
            private Source channel;
        
            public void sendToDefaultChannel(String message) {
                channel.output().send(MessageBuilder.withPayload(message).build());
            }
        
        }

  1. 消费者配置

    1. application.yml配置
    spring:
      cloud:
        stream:
          kafka:
            binder:
              brokers: localhost:9092  #Kafka的消息中间件服务器
              zk-nodes: localhost:2181 #Zookeeper的节点,如果集群,后面加,号分隔
              autoCreateTopics: true #如果设置为false,就不会自动创建Topic 有可能你Topic还没创建就直接调用了
              requiredAcks: 1
              autoAddPartitions: true
          	bindings:
               input:  #input是接收,注意这里不能再像前面一样写output了
                   destination: stream-demo #消息接收的目的地
    1. 监听消费消息

      @Slf4j
      //消息接收端,stream给我们提供了Sink,Sink源码里面是绑定input的,要跟我们配置文件的input关联的。
      @EnableBinding(value = Sink.class)
      public class KafkaMessageReceiveListener {
      
          /**
           * 从缺省通道接收消息
           * @param message
           */
          @StreamListener(Sink.INPUT)
          public void receive(Message<String> message){
      
              log.warn("订阅消息:message={},data = {}",  message.getPayload(),message);
          }
      }
      

2022-07-21

mongdb + springboot 配置及其测试

测试工程 prodiver-8001

  1. maven 依赖:

    <!-- mongodb支持 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
  2. application.yml

    sping: 
        data:
          mongodb:
            #无密码连接:mongodb://服务器IP:端口/数据库名
            #有密码连接 mongodb://用户名:密码@服务器IP:端口/数据库名
            uri: mongodb://kapai:123456@127.0.0.1:27017/test?authSource=admin&authMechanism=SCRAM-SHA-1
              # 上方为明确指定某个数据的用户进行连接
            # 也可以使用admin 数据库中的用户进行连接  统一到admin 数据库进行认证
            # admin 用户认证 url 写法: mongodb://账户:密码%40@ip:端口/数据库名?authSource=admin&authMechanism=SCRAM-SHA-1
  3. config 代码

    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
    import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
    import org.springframework.data.mongodb.core.convert.MongoConverter;
    
    /**
     * @descrition: 监听 mongo,保存数据
     * 此类若不加,那么插入的一行会默认添加一个_class字段来存储实体类类型
     * @author: zhouyongjun
     * @date: 2022/7/21
     **/
    @Configuration
    public class ApplicationReadyListener implements ApplicationListener<ContextRefreshedEvent> {
    
        @Autowired
        MongoTemplate oneMongoTemplate;
    
        private static final String TYPEKEY = "_class";
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            MongoConverter converter = oneMongoTemplate.getConverter();
            if (converter.getTypeMapper().isTypeKey(TYPEKEY)) {
                ((MappingMongoConverter) converter).setTypeMapper(new DefaultMongoTypeMapper(null));
            }
        }
    }
  4. entity 实体

    /**
     * @descrition:
     * @author: zhouyongjun
     * @date: 2022/7/21
     **/
    @Data
    @Document(collection = "person")  //指定要对应的文档名(表名)
    public class Person {
        /*** 自定义mongo主键 加此注解可自定义主键类型以及自定义自增规则
         *  若不加 插入数据数会默认生成 ObjectId 类型的_id 字段
         *  org.springframework.data.annotation.Id 包下
         *  mongo库主键字段还是为_id 。不必细究(本文实体类中为id)
         */
        @Id
        private Long id;
        private String username;
        private LocalDateTime timer;
    }
  5. service 服务

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Override
    public int insert(Person person) {
        try {
            person.setTimer(LocalDateTime.now());
            mongoTemplate.insert(person);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    
    @Override
    public int update(Person person) {
        //通过query根据id查询出对应对象,通过update对象进行修改
        Query query = new Query(Criteria.where("_id").is(person.getId()));
        Update update = new Update().set("username", person.getUsername());
        try {
            mongoTemplate.updateFirst(query, update, Person.class);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    
    @Override
    public int remove(Long id) {
        Query query=new Query(Criteria.where("_id").is(id));
        try {
            mongoTemplate.remove(query,Person.class);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    
    @Override
    public Person find(Long id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Person one = mongoTemplate.findOne(query, Person.class);
        return one;
    }
    
    @Override
    public List<Person> findAll() {
        Query query = new Query();
        List<Person> studentList = mongoTemplate.find(query, Person.class);
        return studentList;
    }

2022-07-20

实现功能 prodiver提供群组访问
  1. 生产者提供2个 prodiver-8001和prodiver-8002

  2. 生产者配置修改 application.yml,保证name相同:

* prodiver-8001

  ```java
  server:
    port: 8001
  spring:
    profiles: dev
    application:
      name: provider #保证同名
  ```

* prodiver-8002

  ```java
  server:
    port: 8002
  spring:
    profiles: dev
    application:
      name: provider #保证同名
  ```

3.  消费者访问 URL:URL = "http://provider/dept";
  1. nacos 服务界面情况:

    图片

    集群

docker 启动nacos

1、搜索nacos镜像
docker search nacos
2、下载镜像
docker pull nacos/nacos-server
3、查看本地镜像,看看拉到本地没有
docker images
4、启动镜像
nacos官方文档
docker run -d -p 8848:8848 --env MODE=standalone  --name nacos  nacos/nacos-server
5、验证是否成功
http://localhost:8848/nacos
默认用户名密码都是: nacos

2022-06及其之前

  1. 消费者和生产者同时注册到nacos

  2. 消费者通过RestTemplate访问生产者 ,使用 RIbbon框架

    • URL = "http://provider-8001/dept" 使用 生产者名字组成请求URL,避免地址和端口号的暴露,及其支持集群
    • 消费端 给 RestTemplate 实例添加 @LoadBalanced 注解,开启 @LoadBalanced 与 Ribbon 的集成
消费者项目:consumer-80
  1. 配置 application.yml

    spring:
      profiles:
        active: dev
    ---
    server:
      port: 80
    spring:
      profiles: dev
      application:
        name: consumer-80
      #nacos配置
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848 #nacos服务地址
    1. 消费者访问 生产者,使用 @LoadBalanced restTemplate() 实现 URL 访问生产者

      @Configuration
      public class ConfigBean {
          @LoadBalanced
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
      }
      @RestController
      @RequestMapping("/consumer/dept")
      @Log
      public class ConsumerController {
          @Autowired
          @Qualifier("restTemplate")
          RestTemplate restTemplate;
          static String URL = "http://provider-8001/dept";
          @GetMapping("/get/{id}")
          public Dept getDept(@PathVariable("id") Long id) {
              log.info("get id="+id);
              return restTemplate.getForObject(URL+"/get/"+id,Dept.class);
          }
          @GetMapping("/list")
          public List<Dept> listDept() {
              log.info("listDept");
              return restTemplate.getForObject(URL+"/list",List.class);
          }
      }
生产者 provider-8001
  1. application.yml配置
spring:
  profiles:
    active: dev
---
server:
  port: 8001
spring:
  profiles: dev
  application:
    name: provider-8001
  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:13306/springcloud-demo1?userUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
  #nacos配置
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #nacos服务地址
       # namespace: feacbef6-2a0c-4707-939c-f3f88601f52a #配置 nacos命名空间ID
        #group: provider #nacos分组

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.zhou.cloud.dao.entity

#Actuator info 监控信息配置
info:
  app.name: zhoushen-provider
  company.name: joymeng

查找资料

MAVEN: https://mvnrepository.com/

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

springcloud-alibaba demo 学习 分布式唯一ID;分布式锁;arthas监控 展开 收起
Java 等 3 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zhou_sir/springcloud-alibaba-demo.git
git@gitee.com:zhou_sir/springcloud-alibaba-demo.git
zhou_sir
springcloud-alibaba-demo
springcloud-alibaba-demo
master

搜索帮助