1. Create a new project
2. The type of project created is SpringBoot
Select Create and then select New Project
3. Delete all unnecessary things in the newly created project, leaving only the pom file
Modify the version of the pom file
<version>2.1.6.RELEASE</version>
Import the required jar package and overwrite the dependencies in the original <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>
If your java version is higher than 8, you also need to import the following dependencies
<!-- 引入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结束 -->
Import component dependencies that microservices depend on
<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. Create microservices with entity classes
Import the required dependencies in the pom file under this microservice
<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>
Create entity class
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. Create a microservice for writing logic, the steps are the same as above, write the name as: leq-server-product
Introduce the address of the entity class microservice
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
Import the dependencies required by the second-generation microservice component nacos
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
Create a startup class
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); } }
Write configuration files and create application.properties files in resources
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. Create a microservice for business access, named: leq-server-page
It is also the address of the project that first imports the entity class
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
Then import the following 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>
Create a startup class for the 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); } }
write configuration files,
Create application.properties file in resources
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
Even if our microservice project is built here, we will test it next
Friends who have not installed nacos can see this link to install nacos: Quickly install nacos
1. Start the naco service registration center first
http://10.48.185.7:8848/nacos/index.html
Visiting the address and seeing the page in the browser proves that the service registration center is started
Start the two microservice projects of page and product, and then refresh the address, you can see that our project is registered to the service center