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 进行 测试

相关推荐
ZZHow102413 分钟前
JavaWeb开发_Day05
java·笔记·web
CHEN5_0218 分钟前
【Java虚拟机】垃圾回收机制
java·开发语言·jvm
Warren9821 分钟前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
HalvmånEver1 小时前
在 C++ :x86(32 位)和 x64(64 位)的不同
开发语言·c++·学习
amy_jork3 小时前
npm删除包
开发语言·javascript·ecmascript
浪成电火花4 小时前
(deepseek!)deepspeed中C++关联部分
开发语言·c++
茉莉玫瑰花茶4 小时前
Qt 常用控件 - 9
开发语言·qt
艾伦~耶格尔4 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试
杜子不疼.4 小时前
《Python列表和元组:从入门到花式操作指南》
开发语言·python
爪洼传承人5 小时前
18- 网络编程
java·网络编程