1. Create a parent project: packaged in the form of the parent project is pom, delete the rest unrelated files
Modify the contents of the parent project's pom files are as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.offcn</groupId> <artifactId>microservice_cloud_01</artifactId> <version>1.0-SNAPSHOT</version> <!--手动指定 pom--> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <!--依赖声明--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Create a sub-project Eureka01
Add rely ereka of service daunt
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
Add a application.yml profile, and configure the relevant content (note that the configuration file written specification)
# Built-in tomcat service start listening port number Server: Port: 6001 #EurekaServer configuration Eureka: instance: hostname: eureka6001.com Client: the Register-with-Eureka: false # This EurekaServer not registered to other registries fetch-registry: false # server is not pulling information from other centers of Service-url: defaultzone: http://eureka6002.com:6002/eureka # registry access address
Creating a startup class
@SpringBootApplication @EnableEurekaServer public class AppStart { public static void main(String[] args) { SpringApplication.run(AppStart.class, args); } }
The equivalent of more than three steps to create a server ereka
3. In order to solve the problem of users accessing too much cause the collapse of the server appear to build a cluster
Step consistent with the above steps HTTP: // localhost: $ { the server.port } / Eureka is a generally perform their url server, multiple servers like time as the above, points to another server http://eureka6002.com : 6002 / eureka
4. simulator into which the server
Create a sub-class project as a public entity, here only we need to create the corresponding database table and the corresponding entity class can, and packaged into a local warehouse
5. Create a sub-project, to achieve a simple function check once excision
Increased reliance parent project
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency>
(1) introducing specific dependency, where the need to use particular entity class, the entity class need to rely incorporated herein
<dependencies> <dependency> <groupId>com.offcn</groupId> <artifactId>microservice_cloud_02_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies>
(2) Create and set up a profile application.yml
Server: Port: 8001 MyBatis: config-LOCATION: CLASSPATH: MyBatis / mybatis.cfg.xml # MyBatis profile path where type-aliases-package: com.offcn.springcloud.entities # Entity alias classes where all packet mapper-locations: classpath:. mybatis / mapper / ** / * xml # mapper mapping file the Spring: the Application: name: microService-Product # this is very important, which call each other after the service and between services are generally based on the name the DataSource: of the type : com.alibaba.druid.pool.DruidDataSource # type of the current source operand data driver-class-name: com.mysql.jdbc.Driver # mysql driver package url: jdbc: mysql: //127.0.0.1: 3306 / springboot serverTimezone? = GMT% 2B8 # name database username: the root password: 111111 dbcp2: min-IDLE: database connection pool. 5 # minimum number of connections is maintained initial-size: 5 # initialization connections max-total: 5 # maximum number of connections max-wait-millis: 150 # wait a maximum connection timeout acquired #EurekaServer configuration Eureka: Client: Register Eureka-with-: This EurekaServer to true # is not registered to other registries fetch-registry: true # do not pull the server information from other centers Center Service-url: defaultzone: http://eureka6002.com:6002/eureka,http://eureka6001.com:6001/ eureka # registry access address
(3) Create mapper layer, service layer, controller layer to achieve a simple function CRUD
Here mapper mapping file in the resources directory
mybatis global configuration file is also the same resources directory
(4) Create a startup class
@SpringBootApplication @MapperScan("com.offcn.springcloud.mapper") @EnableEurekaClient public class SpringCloudApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudApplication.class,args); } }
Above is a small entry eureka discovery registration implemented