Java微服务-新建demo

一、整体架构

1.功能模块 如下,简洁明了

                  Client (前端、App、小程序)
                           |
                       Gateway
                (统一入口,流量网关、鉴权)
                           |
        ------------------------------------
        |           |            |         
   auth-service  user-service  order-service
     (认证)         (用户)         (订单)
                           |
                        common
               (通用工具库被引用)

代码结构:

模块介绍:

模块归属层级是否同级主要作用备注
gateway-service网关层——统一入口、安全网关负责全局转发和认证
auth-service业务微服务层认证中心登录、发放token
user-service业务微服务层用户中心管理用户信息
order-service业务微服务层订单中心管理订单流程
common通用支撑层被所有业务微服务引用通用工具只提供工具支持

二、IDEA创建

1.创建项目(项目和模块填写好包名 最好统一包名)

 

2.创建模块(创建好项目后,接着在项目名上面右键-创建模块) 

三、具体代码(已经调试完bug,踩完坑,可以运行的版本)

父工程

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xbc</groupId>
    <artifactId>xbcms-api</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>xbcms-api 微服务父工程</name>

    <!-- 全局属性 -->
    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.7.15</spring.boot.version>
        <spring.cloud.version>2021.0.8</spring.cloud.version>
        <spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
        <mybatis.plus.version>3.5.3.1</mybatis.plus.version>
        <knife4j.version>3.0.3</knife4j.version>
        <jjwt.version>0.9.1</jjwt.version>
        <springdoc.version>1.6.14</springdoc.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mysql.version>8.0.33</mysql.version>
    </properties>

    <!-- 子模块声明 -->
    <modules>
        <module>common</module>
        <module>gateway</module>
        <module>user-service</module>
        <module>order-service</module>
        <module>auth</module>
    </modules>

    <!-- 统一依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot BOM -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

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

            <!-- Spring Cloud Alibaba BOM -->
            <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>

            <dependency>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
                <version>2.3.2</version> <!-- 当前非常稳定 -->
            </dependency>

        </dependencies>
    </dependencyManagement>

    <!-- 公共依赖(所有子模块自动继承) -->
    <dependencies>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <!-- 插件配置 -->
    <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>

        </plugins>
    </build>

</project>

 common模块

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xbc</groupId>
        <artifactId>xbcms-api</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>common</artifactId>
    <name>common</name>

    <dependencies>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <!-- Fastjson2 -->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.42</version>
        </dependency>

        <!-- Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- Spring Web (工具模块也可使用) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
</project>

gateway模块

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xbc</groupId>
        <artifactId>xbcms-api</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>gateway</artifactId>
    <name>gateway</name>

    <dependencies>

        <!-- Spring Cloud Gateway (核心依赖,无需引入spring-boot-starter-web) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!-- Nacos 服务发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

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

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <!-- 公共模块 -->
        <dependency>
            <groupId>com.xbc</groupId>
            <artifactId>common</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

</project>

配置文件:

server:
  port: 8080

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  # 改成真实IP
    gateway:
      routes:
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/auth/**
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/user/**
        - id: order-service
          uri: lb://order-service
          predicates:
            - Path=/order/**


  main:
    web-application-type: reactive # 防止启动失败 需要去掉默认的web

# 日志
logging:
  level:
    com.xbc: debug

auth模块

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xbc</groupId>
        <artifactId>xbcms-api</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>auth</artifactId>
    <name>auth</name>

    <dependencies>

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

        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- MyBatis Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>

        <!-- MySQL 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <!-- 引入 common 模块 -->
        <dependency>
            <groupId>com.xbc</groupId>
            <artifactId>common</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

</project>

配置:

server:
  port: 8081

spring:
  application:
    name: auth-service

  datasource:
    url: 
    username: 
    password: 
    driver-class-name: com.mysql.cj.jdbc.Driver

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

user模块

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xbc</groupId>
        <artifactId>xbcms-api</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>user-service</artifactId>
    <name>user-service</name>

    <dependencies>

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

        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- OpenFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- MyBatis Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>

        <!-- MySQL 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <!-- 引入 common 模块 -->
        <dependency>
            <groupId>com.xbc</groupId>
            <artifactId>common</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

</project>

配置文件:

server:
  port: 8082

spring:
  application:
    name: user-service

  datasource:
    url: 
    username: 
    password: 
    driver-class-name: com.mysql.cj.jdbc.Driver

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848   #

# feign日志级别 (后续用到)
logging:
  level:
    com.xbc: debug

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

order模块

pom.xml

<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">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xbc</groupId>
        <artifactId>xbcms-api</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>order-service</artifactId>
    <name>order-service</name>

    <dependencies>

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

        <!-- Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- OpenFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <!-- 引入 common 模块 -->
        <dependency>
            <groupId>com.xbc</groupId>
            <artifactId>common</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

</project>

配置文件:

server:
  port: 8083

spring:
  application:
    name: order-service

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 10000

logging:
  level:
    com.xbc: debug

四、安装运行

1.安装mysql(自行安装)

2.安装nacos

  • 官网下载地址:Nacos 快速开始 | Nacos 官网
  • 下载解压
  • a.解压后修改配置为你的数据库。另外把这3个配置解开:
  • spring.datasource.platform=mysql
    spring.sql.init.platform=mysql
    
    ### Count of DB:
    db.num=1
    
  • b.新建数据库,导入mysql的相关表到你的库,脚本位置:\nacos\conf\mysql-schema.sql

五、运行测试

 访问网关,然后转发得到结果

最后:需要源码私信 

### Java微服务与Nacos集成指南 #### 一、Nacos简介及其重要性 Nacos作为阿里巴巴开源的服务发现与配置管理基础设施,旨在帮助开发者更轻松地构建云原生应用。通过提供统一的命名空间和服务治理能力,Nacos能够有效简化分布式系统的开发流程并提升其稳定性[^1]。 #### 二、Nacos配置管理详解 对于采用Java技术栈构建的应用程序而言,利用Nacos实现集中式的配置管理是一项非常有价值的功能。这不仅有助于减少重复劳动,还能确保各个环境中的一致性和安全性。以下是基于Spring Boot框架的具体实施方法: - **创建Spring Boot项目** 为了使应用程序具备访问Nacos的能力,在新建工程时需引入相应的依赖库。通常情况下,只需添加`spring-cloud-starter-alibaba-nacos-config`即可完成基本设置[^4]。 ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` - **定义外部化属性文件** 接下来应该准备一份名为`bootstrap.yml`(或`.properties`)的资源文件来指定连接至目标Nacos服务器所需的信息,比如地址、用户名密码等参数[^2]。 ```yaml spring: application: name: demo-service cloud: nacos: config: server-addr: localhost:8848 file-extension: yaml ``` - **注入配置项** 借助于Spring Framework所提供的强大功能集,可以直接在代码内部声明变量并通过自动装配机制获取来自远程存储的数据源。这里列举两种常用方式供参考者选用:一是运用`@Value`标签;二是结合自定义类结构配合`@ConfigurationProperties`注解共同作用。 ```java @RestController public class HelloController { @Value("${example.message}") private String message; // 或者使用@ConfigurationProperties绑定整个对象 } ``` - **支持热加载特性** 值得一提的是,当涉及到频繁变动的关键字表单时,建议开启监听器以便即时响应任何潜在的变化事件。如此这般便能保证本地副本始终处于最新状态而无需重启进程就能生效新版本设定值。 ```yaml management: endpoints: web: exposure: include: refresh ``` #### 三、服务注册与发现原理说明 除了上述提到过的静态资源配置外,动态感知其他成员节点的存在与否同样是至关重要的环节之一。得益于内置的支持插件,只要遵循既定模式编写业务逻辑部分就可无缝对接官方API接口从而达成预期效果。 - **初始化客户端实例** 首先得确认已经成功安装好运行环境并且按照文档指示完成了必要的准备工作之后再继续后续步骤。一般来讲,只需要简单修改几处地方就可以让现有架构兼容新的组件了[^3]。 ```xml <!-- pom.xml --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` - **标注主类入口** 紧接着要在启动引导阶段加入额外指令以激活该模块的核心功能——即告诉容器当前角色身份以及期望与其他参与者建立联系的方式。 ```java @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` - **测试连通状况** 最后一步则是验证一切是否正常运作。可以通过浏览器或其他工具向暴露出来的HTTP API发送请求来进行初步诊断。如果返回的结果符合预期,则证明整个链路畅通无阻。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值