SpringCloud基础框架搭建–1

这是系列搭建springcloud基础框架的文章,内容包括集成shiro、Mysql主从、seata、activiti、drools、hadoop大数据常用组件、keepalive+nginx https配置等;

1)安装 nacos (略)
2)根据 start.aliyun.com, start.spring.io 生成 框架 pom 依赖 ;

a. Unable to import Maven project
https://blog.csdn.net/qq_41704415/article/details/125392743 (版本不符) 使用 3.5.4
下载地址 : https://dlcdn.apache.org/maven/maven-3/3.5.4/binaries/
b. Cannot resolve plugin org.apache.maven.plugins:maven-clean-plugin:2.5
无法解决,直接使用 start.aliyun.com;

2023-4-18 补充解决:
i1. https://blog.nowcoder.net/n/23fc6b0b1bcc4a39bfd85d25f494e48f
file -> Settings, Build,Execution,Deployment—>Build Tools—>Maven—>Runner—>VM Options 输入:
-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true

i2. maven settings.xml 配置 mirrors
c. idea plugin marketplace 一直在 loading;
解决 :https://blog.csdn.net/qq_44868502/article/details/103937533

<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>*</mirrorOf>
  <name>Nexus aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

<mirror>
  <id>maven-center</id>
  <name>maven center repository</name>
  <url>https://repo.maven.apache.org/maven2/</url>
  <mirrorOf>central</mirrorOf>
</mirror> 

重启 idea;
i4. 清空全部或部分 maven repository 目录下内容;
i5. 如果 idea 工具上的mvn不支持,就在idea的Terminal 执行 source ~/bash_profile
(mac 上,/etc/profile 为自读文件)
i6. 重启 idea 后,再执行 reimport;

3. Error:(3, 32) java: 程序包org.springframework.boot不存在;

https://blog.csdn.net/qq_57404736/article/details/125118220
file -> Settings, Build,Execution,Deployment—>Build Tools—>Maven—>Runner 面版:
勾选: Delegate IDE build/run actions to maven;

4. Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency;
网上说zuul exclude web 依赖,或与gateway有关,也需要去掉web依赖;
1). 如果是 gateway ,把 spring-boot-starter-web 去掉,不是exclude;
2). 如果不是 gateway ,就把 gateway 依赖去掉;
3). 两者都要把保留,则可以在yml添加配置 ;reactive

spring:
  application:
    name: test-boot
  main:
    web-application-type: reactive

基于alibaba nacos搭建框架
1) 在new project -> idea initialr, 使用 start.aliyun.com 报错,不能使用;
直接在 https://start.aliyun.com 直接生成,生成组件 :
Spring Cloud Alibaba:nacos service discovery、spring cloud alibaba sentinel、spring cloud alibaba seata

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chen</groupId>
    <artifactId>frame</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>frame</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.chen.frame.FrameApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2) 将 pom.xml 的 package 改为 pom
3) 在 父目录 [chen] 添加 两个 module,chen-api、chen-modules 也分别为 packaging pom类型;
4) 再在 chen-modules 下添加 module: chen-test-biz
5) 再通过 start.aliyun.com 生成 web相关的依赖;
只选择 web, openfeign 两个依赖,后面会自动添加 test单元测试依赖 ;

生成后,多个 spring-cloud-dependencies 的依赖,在父pom.xml 补充上 properties 的版本,以及 denpendencyManager 下添加 spring-cloud-dependencies;

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.6.13</spring-boot.version>
    <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
    <spring-cloud.version>2021.0.5</spring-cloud.version>
</properties>

以及:dependencyManager 下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

处理 chen-test-biz pom.xml 文件

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

1) error: 提示 timeout, 重新 reimport
2) error: Cannot resolve org.springframework.cloud:spring-cloud-starter-openfeign:unknown
因为 spring-cloud-dependencies 依赖没有下载完成,多偿试几次 reimport;

3) 配置运行 chen-test-biz

@SpringBootApplication
public class TestBizApplication {

    public static void main(String... args) {
        SpringApplication.run(TestBizApplication.class, args);
    }
}

application.yml

server:
  port: 8022

spring:
  application:
    name: chen-test-biz
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yml
        group: DEFAULT_GROUP
        shared-configs[0]:
          data-id: application-dev.yml
          group: DEFAULT_GROUP
          refresh: true
  profiles:
    active: dev

welController.java

@RestController
public class WelcomecController {

    @GetMapping(value = "/welcome")
    public String welcome() {
        return "welcome...";
    }
}

nacos 配置
chen-test-biz-dev.yml以及 applicaion-dev.yml 配置
运行 :

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=nacos: property to your configuration.
    If configuration is not required add spring.config.import=optional:nacos: instead.
    To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.

解决,在父 pom.xml dependencies 下添加依赖 : (spring cloud 2020 后修改了架构)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

7 集成 gateway 路由模块
1) 在 父目录 [chen] 下,添加 pom module: chen-framework
2) 在 chen-framework 添加 chen-gateway 模块,配置:

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String... args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

bootstrap.yml

server:
  port: 8080

spring:
  application:
    name: chen-gateway
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yml
        group: DEFAULT_GROUP
        shared-configs[0]:
          data-id: application-dev.yml
          group: DEFAULT_GROUP
          refresh: true

nacos 上 chen-gateway-dev.yml 配置

spring:
  cloud:
    gateway:
      discovery:
        locator:
          #这里配置声明是否使用springcloud gateway的服务注册,false 代表不使用
          enabled: false

      routes:
        - id: chen-test-biz
          #这里指定命中path中的路径的请求会跳转到test1-service的微服务中
          # uri: lb://chen-test-biz #会走 8022, 8023
          # idea 上配置 chen-test-biz 新实例 vm params : -Dserver.port=8023
          uri: http://localhost:8023 #只会走这个端口,相当于个人
          predicates:
            - Path=/test/**
          filters:
            - StripPrefix=1

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>chen-framework</artifactId>
        <groupId>com.chen</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>chen-gateway</artifactId>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!-- spring cloud 2020 需求添加如下依赖 -->
        <!-- https://blog.csdn.net/RookiexiaoMu_a/article/details/118824153 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

补:
com.alibaba.nacos.api.exception.NacosException: http error, code=403,msg=user not found

spring:
  application:
    name: chen-manager
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.232.128:8848
        username: nacos
        password: nacos
      config:
        username: nacos
        password: nacos
        server-addr: 192.168.232.128:8848
        file-extension: yml
        group: DEFAULT_GROUP
        shared-configs[0]:
          data-id: application-dev.yml
          group: DEFAULT_GROUP
          refresh: true
        shared-configs[1]:
          data-id: chen-druid-dev.yml
          group: DEFAULT_GROUP
          refresh: true

欢迎您的到来,感谢您的支持!

为您推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注