• Spring Cloud 微服务入门


    源代码 链接: https://pan.baidu.com/s/1ZXqxDyLbGWhcrWCKqa7WZg 提取码: kcei 复制这段内容后打开百度网盘手机App,操作更方便哦

    概述:本文适用于新入门的童鞋参考,一步步创建一个新的Spring Cloud项目。

    一、配置nacos注册中心

    1、下载地址 home

    2、解压后在bin目录windows下双击startup.cmd启动,停止使用shutdown.cmd

    3、验证
    http://localhost:8848/nacos/index.html
    默认帐号密码均为nocos

    二、新建测试项目

    1、目录说明

    demo

    - demo-gateway #网关模块8000

    - demo-test #测试模块8001

    2、步骤
    1)创建demo
    File->New->Project

    选择Maven,对应的SDK版本,然后下一步

    输入项目名称,选择目录路径,填写对应的信息,然后完成

    修改pom.xml文件(参考)

    
    
        4.0.0
        org.example
        demo
        pom
        1.0-SNAPSHOT
        
            demo-gateway
            demo-test
        
        
            
                
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    2020.0.3
                    pom
                    import
                
    
                
                
                    com.alibaba.cloud
                    spring-cloud-alibaba-dependencies
                    2021.1
                    pom
                    import
                
                
                
                    com.alibaba.nacos
                    nacos-client
                    2.0.3
                
    
                
                
                    org.springframework.boot
                    spring-boot-dependencies
                    2.5.3
                    pom
                    import
                
            
        
    
        
            
            
                org.springframework.cloud
                spring-cloud-starter-bootstrap
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    2)创建demo-gateway模块
    在当前项目目录下,右键->New->Module

    选择对应SDK,然后下一步

    填写对应信息

    修改pom.xml文件(参考)

    
    
        
            demo
            org.example
            1.0-SNAPSHOT
        
        4.0.0
    
        demo-gateway
        
    
            
            
                org.springframework.cloud
                spring-cloud-starter-gateway
            
    
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
            
    
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-config
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-openfeign
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-loadbalancer
            
    
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在java目录新建包com.demo.gateway及启动类GatewayApplication.java

    package com.demo.gateway;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class GatewayApplication {
    
        public static void main(String[] args)
        {
            SpringApplication.run(GatewayApplication.class, args);
            System.out.println("网关启动成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在resources新建bootstrap.yml文件

    # Tomcat
    server:
      port: 8000
    
    # Spring
    spring:
      application:
        # 应用名称
        name: demo-gateway
      profiles:
        # 环境配置
        active: dev
      main:
        allow-bean-definition-overriding: true
      cloud:
        nacos:
          discovery:
            # 服务注册地址
            server-addr: 127.0.0.1:8848
          config:
            # 配置中心地址
            server-addr: 127.0.0.1:8848
            # 配置文件格式
            file-extension: yml
            # 共享配置
            shared-configs:
              - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在nacos配置中心http://127.0.0.1:8848/nacos/配置demo-gateway-dev.yml

    spring:
      cloud:
        gateway:
          discovery:
            locator:
              lowerCaseServiceId: true
              enabled: true
          routes:
            # 系统模块
            - id: demo-test
              uri: lb://demo-test
              predicates:
                - Path=/test/**
              filters:
                - StripPrefix=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    启动GatewayApplication.java,成功后在nacos配置中心的服务管理-服务列表可看到demo-gateway服务

    3)创建demo-test模块
    创建步骤参考demo-gateway模块,下面主要列代码及配置

    pom.xml文件参考

    
    
        
            demo
            org.example
            1.0-SNAPSHOT
        
        4.0.0
    
        demo-test
        
    
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
            
    
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-config
            
            
                org.springframework
                spring-web
            
    
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.projectlombok
                lombok
            
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    在java目录新建包com.demo.test及启动类TestApplication.java

    package com.example.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class TestApplication {
        public static void main(String[] args)
        {
            SpringApplication.run(TestApplication.class, args);
            System.out.println("测试启动成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在resources目录下创建bootstrap.yml

    # Tomcat
    server:
      port: 8001
    
    # Spring
    spring:
      application:
        # 应用名称
        name: demo-test
      profiles:
        # 环境配置
        active: dev
      cloud:
        nacos:
          discovery:
            # 服务注册地址
            server-addr: 127.0.0.1:8848
          config:
            # 配置中心地址
            server-addr: 127.0.0.1:8848
            # 配置文件格式
            file-extension: yml
            # 共享配置
            shared-configs:
              - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在nacos配置中心http://127.0.0.1:8848/nacos/配置demo-test-dev.yml
    我这边是没有内容的,根据实际情况修改

    创建controller包及TestController.java测试类

    package com.example.test.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    
        @GetMapping("/test2")
        public String Test2(){
            System.out.println("Test2 ok");
            return "Alive2";
        }
    
        @GetMapping("/index")
        public String Test(){
            System.out.println("Test ok");
            return "Alive";
        }
    
        @GetMapping("/card/{cardId}")
        public String cardTest(@PathVariable("cardId") String cardId){
            System.out.println("Test ok" + cardId);
            return "CardId>>>" + cardId;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    启动TestApplication.java ,成功后在nacos 配置中心可看到对应的服务

    4)验证
    不经过网关 http://127.0.0.1:8001/test2
    经过网关 http://127.0.0.1:8000/test/test2
    经过网关的链接test/**,对应是nacos配置中心demo-gateway-dev.yml配置的路由。实际http://127.0.0.1:8000/test对应demo-test服务http://127.0.0.1:8001/ ,所以http://127.0.0.1:8000//test/test2 对应http://127.0.0.1:8001/test2

    另外2个验证链接

    http://127.0.0.1:8000/test/card/1234

    http://127.0.0.1:8000/test/index

    至此,完成

  • 相关阅读:
    安装node, 配置npm全局安装位置,安装Vue.
    【重拾C语言】十、递归程序设计
    Hive架构原理
    idea如何快速找到项目中对应的类(包括源码)
    路由器配置单区域(多区域)OSPF
    Spring Boot (Vue3+ElementPlus+Axios+MyBatisPlus +Spring Boot 前后端分离)
    MATLAB中d2d函数用法
    【从0开始编写webserver·基础篇#03】TinyWeb源码阅读,还是得看看靠谱的项目
    JSP基础
    Go 语言是如何实现切片扩容的?【slice】
  • 原文地址:https://blog.csdn.net/m0_67393295/article/details/126496938