SpringCloud 2023.0.1

本文介绍如何使用 springboot3及cloud2023 进行微服务模块化开发

采用父-module 模块开发

父工程 demo-java

pom.xml

复制代码
 <!--配置 springboot的依赖的版本号, 方便 module 进行继承-->
     <dependencyManagement>
         <dependencies>
             <!--增加 springboot的依赖-->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-dependencies</artifactId>
                 <version>3.2.5</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
 ​
             <!--增加 springcloud的依赖-->
             <dependency>
                     <groupId>org.springframework.cloud</groupId>
                     <artifactId>spring-cloud-dependencies</artifactId>
                     <version>2023.0.1</version>
                     <type>pom</type>
                     <scope>import</scope>
             </dependency>
 ​
 ​
         </dependencies>
     </dependencyManagement>
 ​
 ​

子模块 cloud-eureka-server-7001

pom.xml

复制代码
 ​
     <dependencies>
         <!--增加 boot web的依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 ​
         <!--增加  eureka-server 的依赖-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
         </dependency>
     </dependencies>
 ​

启动类:

复制代码
 package com.ly;
 ​
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 ​
 @SpringBootApplication
 @EnableEurekaServer
 public class CloudEurekaServer7001 {
     public static void main(String[] args) {
         SpringApplication.run(CloudEurekaServer7001.class,args);
     }
 }
 ​

配置文件 application.yml

复制代码
 # 设置端口号为 7001
 server:
   port: 7001
 ​
 ​
 eureka:
   instance:
     hostname: localhost
   client:
     fetch-registry: false    #如果fetch-registry为false, 则表示自己为注册中心
     register-with-eureka: false  #表示是否向eureka注册中心注册自己
     service-url:
       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka   # 服务地址
 ​
 ​
 ​
 ​

启动测试:

子模块 cloud-eureka-provider-8001

pom.xml

复制代码
 <dependencies>
         <!--增加 boot web的依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <!--增加 eureka client 依赖-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
      <!--增加 监控 boot 依赖-->
       <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
     </dependencies>

启动类

复制代码
 package com.ly;
 ​
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 ​
 ​
 @SpringBootApplication
 @EnableDiscoveryClient
 public class EurekaProvider8001 {
     public static void main(String[] args) {
         SpringApplication.run(EurekaProvider8001.class,args);
     }
 }
 ​

application.yaml

复制代码
 ​
 #设置端口号
 server:
   port: 8001
 ​
 ​
 ​
 eureka:
   client:
     fetch-registry: true #是提供者,不是注册中心 ,可省略
     register-with-eureka: true #向注册中心 注册服务,可省略
     service-url: #服务地址
       defaultZone: http://localhost:7001/eureka
 ​
 ​

刷新 之前的 server

如何 解决 unknow ?修改 provider-8001 的 yaml文件,增加 spring.application.name

为了 模拟 用户管理 ---provider8001 , 订单管理--provider8002, 消费者来 消费服务

子模块 cloud-eureka-common-api

pom.xml

复制代码
  <dependencies>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
         </dependency>
     </dependencies>

创建 实体层 User.java 与 OrderInfo.java

复制代码
 package com.ly.entity;
 ​
 ​
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.ToString;
 ​
 /**
  * 用户 实体
  */
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 @ToString
 public class User {
     private int userId; //用户编号
     private String username;//用户名
     private String phone;//电话
 }
 ​
复制代码
 package com.ly.entity;
 ​
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.ToString;
 ​
 import java.time.LocalDateTime;
 ​
 /**
  * 订单 实体
  */
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 @ToString
 public class OrderInfo {
     private int orderNo;//订单编号
     private String title;// 标题
     private double price;//单价
     private double count;//个数
     private LocalDateTime time;//购买时间
     private int userId;// 用户编号
 }
 ​

修改 provider8001 的 pom.xml

增加

复制代码
 <!--引入  common-api module-->
 <dependency>
     <groupId>com.ly</groupId>
     <artifactId>cloud-eureka-common-api</artifactId>
     <version>1.0-SNAPSHOT</version>
 </dependency>

为 provider8001 增加 controller

复制代码
 package com.ly.controller;
 ​
 import com.ly.entity.User;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 ​
 /**
  * provider8001 --模拟的就是用户管理 模块
  */
 @RestController
 public class UserController {
 ​
     @GetMapping("/user/{id}")
     public User find(@PathVariable("id")int id){
 ​
         // 模拟数据返回
          return  new User(1001,"李四","137526154875");
     }
 ​
 ​
 ​
 ​
 }
 ​

启动 provider8001 进行测试

看到以上 截图表示 成功

子模块 eureka-provider-8002

pom.xml

复制代码
  <dependencies>
         <!--增加 boot web的依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <!--增加 eureka client 依赖-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
         </dependency>
         <!--增加 监控 boot 依赖-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 ​
         <!--引入  common-api module-->
         <dependency>
             <groupId>com.ly</groupId>
             <artifactId>cloud-eureka-common-api</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
 ​
     </dependencies>

application.yaml

复制代码
 ​
 #设置端口号
 server:
   port: 8002
 ​
 ​
 ​
 eureka:
   client:
     fetch-registry: true #是提供者,不是注册中心 ,可省略
     register-with-eureka: true #向注册中心 注册服务,可省略
     service-url: #服务地址
       defaultZone: http://localhost:7001/eureka
 spring:
   application:
     name: provider-8002        # 设置应用名, 注意, 值 不允许使用 下划线
 ​

启动类

复制代码
 package com.ly;
 ​
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 ​
 @SpringBootApplication
 @EnableDiscoveryClient
 public class EurekaProvider8002 {
     public static void main(String[] args) {
         SpringApplication.run(EurekaProvider8002.class,args);
     }
 }
 ​

controller

复制代码
 package com.ly.controller;
 ​
 import com.ly.entity.OrderInfo;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 ​
 import java.time.LocalDateTime;
 ​
 /**
  * provider8002 模拟就是 订单管理 模块
  */
 @RestController
 public class OrderInfoController {
     
     @GetMapping("/order/{userId}")
     public OrderInfo find(@PathVariable("userId")int userId){
         //模拟数据返回
         return new OrderInfo(1003,"保温杯",50,1, LocalDateTime.now(),1001);
     }
 }
 ​

启动 provider8002, 测试 7001

子模块 cloud-eureka-consumer-80

pom.xml

复制代码
<dependencies>

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

        <dependency>
            <groupId>com.ly</groupId>
            <artifactId>cloud-eureka-common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

启动类

复制代码
package com;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

配置文件

复制代码
 ​
 #设置端口号
 server:
   port: 80
 ​
 ​
 eureka:
   client:
     fetch-registry: true #是提供者,不是注册中心 ,可省略
     register-with-eureka: false #向注册中心 不注册服务,因此 是消费服务的
     service-url: #服务地址
       defaultZone: http://localhost:7001/eureka
 spring:
   application:
     name: consumer80      # 设置应用名, 注意, 值 不允许使用 下划线
 ​

配置类 注入 RestTemplate

复制代码
 package com;
 ​
 ​
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.client.RestTemplate;
 ​
 /**
  * 配置类
  */
 @Configuration
 public class MyConfig {
 ​
     @Bean
     public RestTemplate restTemplate(){
         return new RestTemplate();
     }
 }
 ​

controller

复制代码
 package com.controller;
 ​
 import com.ly.entity.OrderInfo;
 import com.ly.entity.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.client.RestTemplate;
 ​
 @RestController
 public class TestController {
 ​
     @Autowired
     private RestTemplate restTemplate;
 ​
     /**
      * 模拟查询 用户信息
      * @param id
      * @return
      */
     @GetMapping("/user/{id}")
     public User queryUser(@PathVariable("id")int id){
          //访问 8001 获得 数据
          return restTemplate.getForObject("http://localhost:8001/user/1",User.class);
 ​
     }
 ​
     /**
      * 模糊查询订单
      * @param id
      * @return
      */
     @GetMapping("/order/{id}")
     public OrderInfo queryOrder(@PathVariable("id")int id){
         //访问 8002 获得数据
       return restTemplate.getForObject("http://localhost:8002/order/3",OrderInfo.class);
     }
 ​
 ​
 }
 ​

启动 consumer 进行 测试

相关推荐
观无3 小时前
若依EasyExcel实现单元格合并
开发语言·前端·javascript
雨晨源码(同名B站)3 小时前
基于Python的网易云音乐评论数据情感化分析系统 音乐爬虫信息可视化 |SnowNLP评论情感分析
开发语言·hadoop·爬虫·python·信息可视化·毕业设计
我不是疯子是傻子3 小时前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆3 小时前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
xiaohaiAIgeo3 小时前
【2026年】HG/T 20656-2024化工暖通空调设计规范:新版标准的变化与影响
java·前端·javascript·科普知识
小强库计算机毕业设计3 小时前
基于 Spring Boot + Vue3 的校园管理系统
java·spring boot·后端·项目实战·管理系统·技术分享·校园管理系统实战
霸道流氓气质3 小时前
Java中信号量(Semaphore):从本地到分布式
java·开发语言·分布式
怪奇云呼军3 小时前
G.711、Opus 和重采样会拖慢识别吗?闪电智能VoiceAgent 的音频入口怎么选
java·人工智能·python·算法·云计算·音视频
编程令我快乐4 小时前
maven部分构建参数讲解
java·maven
ZC跨境爬虫4 小时前
LeetCode 27. 移除元素(双指针详解 + Java Python 多解法对比)
java·python·leetcode