1.创建新的项目
2.创建的项目类型为SpringBoot
选择创建后再选择新建项目
3. 将新建的项目中不需要的东西都给删了,只留下pom文件即可
修改pom文件的版本
<version>2.1.6.RELEASE</version>
导入所需要的jar包,将原来<dependencies>中的依赖覆盖掉
<!-- SpringBoot启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- SpringBoot测试启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 日志依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- Lombok工具 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency> <!-- Actuator可以帮助你监控和管理Spring Boot应用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
如果你的java版本比8高,还需要导入以下依赖
<!-- 引入Jaxb开始 --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.2.10-b140310.1920</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- 引入Jaxb结束 -->
导入微服务依赖的组件依赖
<dependencyManagement> <dependencies> <!-- Spring Cloud Neflix 公司出品的微服务组件的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--Spring Cloud Alibaba微服务组件的依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
4.创建存在实体类的微服务
在这个微服务下的pom文件中导入所需要的依赖
<dependencies> <!-- mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <!-- pojo持久化使用 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
创建实体类
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private Integer id; private String username; private String password; }
5.创建用于编写逻辑的微服务,步骤和上面的一样,将名字编写为: leq-server-product
引入实体类微服务的地址
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
导入第二代微服务组件nacos所需要的依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
创建启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class,args); } }
编写配置文件,在resources创建application.properties文件
server.port=3000 spring.application.name=leq-server-product spring.datasource.url=jdbc:mysql://localhost:3306/leq_pro?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=0216 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
6.创建业务访问的微服务,名字为: leq-server-page
也是先导入实体类的项目的地址
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
再导入以下依赖
<!--nacos的客户端依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 引入alibaba-nacos-config依赖,可以从Nacos配置中心获得配置信息 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Sentinel核心环境依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
创建page的启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class PageApplication { public static void main(String[] args) { SpringApplication.run(PageApplication.class,args); } }
编写配置文件,
在resources创建application.properties文件
server.port=3100 spring.application.name=leq-server-page spring.datasource.url=jdbc:mysql://localhost:3306/leq_pro?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=0216 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
到这里我们的微服务项目就算是搭建完成了,接下来进行测试
没有安装nacos的小伙伴可以看这个链接安装nacos : 快速安装nacos
1.先启动naco服务注册中心
http://10.48.185.7:8848/nacos/index.html
访问该地址在浏览器可以看到该页面就证明服务注册中心启动了
启动page和product两个微服务项目,然后刷新该地址,就可以看到我们的项目注册到服务中心了文章来源:https://www.toymoban.com/news/detail-708359.html
文章来源地址https://www.toymoban.com/news/detail-708359.html
到了这里,关于在idea中搭建微服务项目(22版),详细教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!