第一步创建一个根项目:

1)就是基本创建springboot的流程
2)依赖jar包
(子项目的依赖自己修改)

  1. <modules>
  2. <module>cloud_provider</module>
  3. <module>cloud_consumer</module>
  4. <module>cloud_common</module>
  5. </modules>

根项目要打程pom包,不需要插件!!!!!

  1. <packaging>pom</packaging>
  2. <!-- 2.SpringBoot项目 -->
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>2.5.9</version>
  7. <relativePath/> <!-- lookup parent from repository -->
  8. </parent>
  9. <!-- 3.自定义属性-->
  10. <properties>
  11. <maven.compiler.source>8</maven.compiler.source>
  12. <maven.compiler.target>8</maven.compiler.target>
  13. <java.version>1.8</java.version>
  14. <spring-cloud.version>2020.0.5</spring-cloud.version>
  15. </properties>
  16. <!-- 4.jar版本限定-->
  17. <dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-dependencies</artifactId>
  22. <version>${spring-cloud.version}</version>
  23. <type>pom</type>
  24. <scope>import</scope>
  25. </dependency>
  26. </dependencies>
  27. </dependencyManagement>
  28. <!-- 5.依赖,公共-->
  29. <dependencies>
  30. <dependency>
  31. <groupId>org.projectlombok</groupId>
  32. <artifactId>lombok</artifactId>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. </dependencies>

第二步创建公共类

1)在跟项目上面创建模块,也就是创建springboot项目
2)依赖jar包
(只需要导入根项目依赖,以及自定义属性,不需要插件)

  1. <parent>
  2. <groupId>com.mhy</groupId>
  3. <artifactId>hellocloud</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>
  6. <properties>
  7. <maven.compiler.source>8</maven.compiler.source>
  8. <maven.compiler.target>8</maven.compiler.target>
  9. </properties>

3)创建公共类目录结构
image.png

第三步:创建提供者

1)在跟项目上面创建模块,也就是创建springboot项目
2)依赖jar包
这是依赖跟项目,需要改为自己根目录的依赖

  1. <!-- 依赖根项目-->
  2. <parent>
  3. <groupId>com.mhy</groupId>
  4. <artifactId>hellocloud</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </parent>
  1. <properties>
  2. <maven.compiler.source>8</maven.compiler.source>
  3. <maven.compiler.target>8</maven.compiler.target>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <!-- 依赖nacos注册中心的jar-->
  11. <dependency>
  12. <groupId>com.alibaba.cloud</groupId>
  13. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  14. <version>2021.1</version>
  15. </dependency>
  16. <!-- 熔断降级sentinel-->
  17. <dependency>
  18. <groupId>com.alibaba.cloud</groupId>
  19. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  20. <version>2021.1</version>
  21. </dependency>
  22. <!-- 实时监控,链路追踪-->
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-sleuth</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-zipkin</artifactId>
  30. <version>2.2.6.RELEASE</version>
  31. </dependency>
  32. <!-- Mysql数据库驱动-->
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. <scope>runtime</scope>
  37. </dependency>
  38. <!-- 数据库连接池:Druid-->
  39. <dependency>
  40. <groupId>com.alibaba</groupId>
  41. <artifactId>druid-spring-boot-starter</artifactId>
  42. <version>1.2.6</version>
  43. </dependency>
  44. <!-- Mybatis-Plus简化JDBC,实现数据库操作-->
  45. <dependency>
  46. <groupId>com.baomidou</groupId>
  47. <artifactId>mybatis-plus-boot-starter</artifactId>
  48. <version>3.4.3.4</version>
  49. </dependency>
  50. </dependencies>

这个要依赖自己的公共模块

  1. <!-- 依赖自定义jar-->
  2. <dependency>
  3. <groupId>com.mhy</groupId>
  4. <artifactId>cloud_common</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </dependency>

3)配置yml配置文件
image.png

  1. server:
  2. port: 8888
  3. spring:
  4. application:
  5. name: LuckProviter #项目名,服务名
  6. cloud:
  7. nacos:
  8. discovery: #注册中心
  9. server-addr: 127.0.0.1:8848
  10. sentinel:
  11. transport:
  12. port: 8719 #信息采集的通信端口
  13. dashboard: 127.0.0.1:8080
  14. sleuth:
  15. sampler:
  16. probability: 1 #设置采样率
  17. zipkin:
  18. sender:
  19. type: web #设置日志收集的方式
  20. base-url: http://localhost:9411/
  21. service:
  22. name: LuckServer
  23. datasource:
  24. driver-class-name: com.mysql.cj.jdbc.Driver
  25. url: jdbc:mysql://110.40.192.129:3308/db_data212?serverTimezone=Asia/Shanghai
  26. username: root
  27. password: zzjava
  28. type: com.alibaba.druid.pool.DruidDataSource
  29. druid:
  30. initialSize: 5
  31. minIdle: 5
  32. maxActive: 20
  33. filters: stat,wall
  34. stat-view-servlet:
  35. enabled: true
  36. url-pattern: "/druid/*"
  37. jackson:
  38. date-format: yyyy-MM-dd HH:mm:ss
  39. time-zone: GMT+8
  40. mvc:
  41. format:
  42. date: yyyy-MM-dd HH:mm:ss

4)配置开关类
image.png

  1. package com.mmhy.cloud_provider;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. @SpringBootApplication//SpringBoot标识
  7. @EnableDiscoveryClient//服务注册
  8. @MapperScan(basePackages = "com.mmhy.cloud_provider.dao")
  9. public class CloudProviderApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(CloudProviderApplication.class, args);
  12. }
  13. }

5)创建提供者目录结构
image.png

  1. package com.mmhy.cloud_provider.config;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  4. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.transaction.annotation.EnableTransactionManagement;
  9. /**
  10. * @author Feri
  11. * @date 2021/12/31 11:59
  12. * @description:TODO
  13. */
  14. @Configuration
  15. @MapperScan(basePackages = "com.mmhy.cloud_provider.dao")//这个要改为自己的dao层
  16. @EnableTransactionManagement//表示开启事务支持
  17. public class MybatisConfig {
  18. @Bean
  19. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  20. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  21. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  22. return interceptor;
  23. }
  24. }

第四步创建消费者项目

1)在跟项目上面创建模块,也就是创建springboot项目
2)依赖java包
依赖根项目,改为自己的

  1. <!-- 依赖根项目,自己改成自己的-->
  2. <parent>
  3. <groupId>com.mhy</groupId>
  4. <artifactId>hellocloud</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </parent>
  1. <!-- 自定义属性-->
  2. <properties>
  3. <maven.compiler.source>8</maven.compiler.source>
  4. <maven.compiler.target>8</maven.compiler.target>
  5. </properties>
  6. <dependencies>
  7. <!-- 1.mvc-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <!-- 2.负载均衡-->
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  16. </dependency>
  17. <!-- 3.服务调用 -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-openfeign</artifactId>
  21. </dependency>
  22. <!-- 4.依赖nacos注册中心的jar-->
  23. <dependency>
  24. <groupId>com.alibaba.cloud</groupId>
  25. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  26. <version>2021.1</version>
  27. </dependency>
  28. <!--链路追踪-->
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-sleuth</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-zipkin</artifactId>
  36. <version>2.2.6.RELEASE</version>
  37. </dependency>
  38. <!-- 5.依赖kinf4J 接口文档-->
  39. <dependency>
  40. <groupId>com.github.xiaoymin</groupId>
  41. <artifactId>knife4j-spring-boot-starter</artifactId>
  42. <version>3.0.3</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>com.mhy</groupId>
  46. <artifactId>cloud_common</artifactId>
  47. <version>0.0.1-SNAPSHOT</version>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. <configuration>
  56. <excludes>
  57. <exclude>
  58. <groupId>org.projectlombok</groupId>
  59. <artifactId>lombok</artifactId>
  60. </exclude>
  61. </excludes>
  62. </configuration>
  63. </plugin>
  64. </plugins>
  65. </build>

3)配置yml配置文件

  1. server:
  2. port: 8083
  3. spring:
  4. application:
  5. name: LuckConsumer #项目名,服务名
  6. cloud:
  7. nacos:
  8. discovery: #注册中心
  9. server-addr: 127.0.0.1:8848
  10. sleuth:
  11. sampler:
  12. probability: 1 #设置采样率
  13. zipkin:
  14. sender:
  15. type: web #设置日志收集的方式
  16. base-url: http://localhost:9411/
  17. service:
  18. name: LuckApi

4)配置开关类

  1. package com.mhy.cloud_consumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient //服务发布
  8. @EnableFeignClients //开启OpenFeign,实现服务消费
  9. public class CloudConsumerApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(CloudConsumerApplication.class, args);
  12. }
  13. }

5)配置swagger在线接口文档类,java日常总结中有详细的,等等可以去看看

  1. package com.mhy.cloud_consumer.config;
  2. import io.swagger.annotations.Api;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. /**
  13. * @author Feri
  14. * @date 2021/12/27 11:44
  15. * @description:TODO
  16. */
  17. @Configuration //标记这是配置类,beans
  18. @EnableSwagger2 //启用Swagger
  19. public class SwaggerConfig {
  20. /**
  21. * 构建文档的基本信息
  22. */
  23. public ApiInfo createApi(){
  24. return new ApiInfoBuilder().description("微服务项目的接口在线文档,可以对接口进行测试等操作")
  25. .title("微服务项目的接口文档").contact(new Contact("mhy","http://www.mhy.com","xingfei_work@163.com"))
  26. .version("1.0.0").build();
  27. }
  28. /**
  29. * 就是ioc创建实例 修饰方法 方法必须返回对象
  30. */
  31. @Bean
  32. public Docket createDocket(){
  33. return new Docket(DocumentationType.SWAGGER_2).apiInfo(createApi())
  34. .select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
  35. .build();
  36. }
  37. }

消费者项目目录结构
image.png
第五步:创建网关项目
1)在跟项目上面创建模块,也就是创建springboot项目
2)依赖jar包
依赖跟项目,,不需要插件

  1. <!-- 依赖根项目-->
  2. <parent>
  3. <groupId>com.mhy</groupId>
  4. <artifactId>hellocloud</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </parent>
  1. <properties>
  2. <maven.compiler.source>8</maven.compiler.source>
  3. <maven.compiler.target>8</maven.compiler.target>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-gateway</artifactId>
  9. </dependency>
  10. <!-- 依赖nacos注册中心的jar-->
  11. <dependency>
  12. <groupId>com.alibaba.cloud</groupId>
  13. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  14. <version>2021.1</version>
  15. </dependency>
  16. <!-- 2.负载均衡-->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  20. </dependency>
  21. </dependencies>

3)配置yml配置类

  1. server:
  2. port: 8082
  3. spring:
  4. application:
  5. name: LxGateway #项目名,服务名
  6. cloud:
  7. nacos:
  8. discovery: #注册中心
  9. server-addr: 127.0.0.1:8848
  10. gateway:
  11. routes:
  12. - id: luck #唯一
  13. uri: lb://LuckConsumer #lb 从注册中心获取服务。要跳转的服务名称
  14. predicates:
  15. - Path=/luck/** #外界的访问路径
  16. filters:
  17. - StripPrefix=1 #去除路由跳转时的路径的级别

4)配置开关类

  1. package com.feri.gateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient
  7. public class GatewayApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(GatewayApplication.class,args);
  10. }
  11. }