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

相关推荐
程序员-珍16 分钟前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
弱冠少年23 分钟前
websockets库使用(基于Python)
开发语言·python·numpy
长天一色24 分钟前
C语言日志类库 zlog 使用指南(第五章 配置文件)
c语言·开发语言
一般清意味……36 分钟前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言
卑微求AC37 分钟前
(C语言贪吃蛇)16.贪吃蛇食物位置随机(完结撒花)
linux·c语言·开发语言·嵌入式·c语言贪吃蛇
2401_8572979143 分钟前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
技术无疆1 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
福大大架构师每日一题1 小时前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
菜鸟一皓1 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea