丽江市建设局网站,软件开发流程8个步骤,wordpress 迁移 hexo,房屋建筑设计网站本文介绍如何使用 springboot3及cloud2023 进行微服务模块化开发
采用父-module 模块开发
父工程 demo-java
pom.xml !--配置 springboot的依赖的版本号, 方便 module 进行继承--dependencyManagementdependencies!--增加 springboot的依赖--!--配置 springboot的依赖的版本号, 方便 module 进行继承--dependencyManagementdependencies!--增加 springboot的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion3.2.5/versiontypepom/typescopeimport/scope/dependency!--增加 springcloud的依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion2023.0.1/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement 子模块 cloud-eureka-server-7001
pom.xml dependencies!--增加 boot web的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--增加 eureka-server 的依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-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;SpringBootApplicationEnableEurekaServerpublic class CloudEurekaServer7001 {public static void main(String[] args) {SpringApplication.run(CloudEurekaServer7001.class,args);}} 配置文件 application.yml # 设置端口号为 7001server:port: 7001eureka:instance:hostname: localhostclient: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的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--增加 eureka client 依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!--增加 监控 boot 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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;SpringBootApplicationEnableDiscoveryClientpublic class EurekaProvider8001 {public static void main(String[] args) {SpringApplication.run(EurekaProvider8001.class,args);}} application.yaml #设置端口号server:port: 8001eureka: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 dependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies 创建 实体层 User.java 与 OrderInfo.java package com.ly.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.ToString;/*** 用户 实体*/DataAllArgsConstructorNoArgsConstructorToStringpublic 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;/*** 订单 实体*/DataAllArgsConstructorNoArgsConstructorToStringpublic 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--dependencygroupIdcom.ly/groupIdartifactIdcloud-eureka-common-api/artifactIdversion1.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 --模拟的就是用户管理 模块*/RestControllerpublic 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的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--增加 eureka client 依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!--增加 监控 boot 依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--引入 common-api module--dependencygroupIdcom.ly/groupIdartifactIdcloud-eureka-common-api/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies application.yaml #设置端口号server:port: 8002eureka:client:fetch-registry: true #是提供者,不是注册中心 ,可省略register-with-eureka: true #向注册中心 注册服务,可省略service-url: #服务地址defaultZone: http://localhost:7001/eurekaspring: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;SpringBootApplicationEnableDiscoveryClientpublic 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 模拟就是 订单管理 模块*/RestControllerpublic 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 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.ly/groupIdartifactIdcloud-eureka-common-api/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-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: 80eureka:client:fetch-registry: true #是提供者,不是注册中心 ,可省略register-with-eureka: false #向注册中心 不注册服务,因此 是消费服务的service-url: #服务地址defaultZone: http://localhost:7001/eurekaspring:application:name: consumer80 # 设置应用名, 注意, 值 不允许使用 下划线 配置类 注入 RestTemplate package com;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;/*** 配置类*/Configurationpublic class MyConfig {Beanpublic 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;RestControllerpublic class TestController {Autowiredprivate 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 进行 测试