Since it is a whole category of electricity supplier shopping platform, so naturally it is the core product. So we have to build the first service, merchandise is micro-services. Which will contain content management for a series of related merchandise, including:
-
Category Management
-
Brand Management
-
Product Specifications Management
-
Commodity Management
-
Inventory Management
Because associated with the category of goods, our engineering named leyou-item
.
It should be noted that our leyou-item is a microblogging service, then the future there will certainly be other systems need to call the interface services provided in the data acquisition interface, but also need the corresponding entity class to encapsulate, and therefore will certainly use an interface to the associated entity class.
So here we need to use polymerization engineering, interfaces and related entity classes to be offered into independent sub-projects in the future when others cited only need to know the coordinates can be.
We will create two sub-projects in leyou-item in:
-
leyou-item-interface: main interfaces and related entities like exposed outside
-
leyou-item-service: all the business logic and internal interfaces
Call the relationship as shown:
2.leyou-item
Still use maven build:
Save location:
Because it is polymerisation project, so the way to package the project pom ( check, perhaps automatically generated )
<! - are packaged as POM -> <Packaging> POM </ Packaging>
3.leyou-item-interface
Right-click on leyou-item projects, select new -> module:
Still use maven to build, pay attention to the parent project is leyou-item:
Click Finish.
At this point the project structure:
4.leyou-item-service
And leyou-item-interface
similar, we choose leyou-item
the right, the new module, and then fill out the project information:
Fill in the storage location
Click Finish.
The entire structure microService
as the picture shows:
We opened leyou-item of pom view, you will find leyou-item-interface and leyou-item-service have become the module:
You can delete the src directory leyou-item project
Next we have to leyou-item-service
add dependencies:
Think about what we need?
-
Eureka client
-
web launcher
-
-
Universal Launcher mapper
-
Paging Assistant Starter
-
Connection pooling, we use the default Hykira
-
mysql driver
-
Do not forget, we also need to own
ly-item-interface
the entity classes
These dependencies, we top-level parent project: Well leyou has been added in. So you can direct introduction:
<dependencies> <!-- web启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- mybatis的启动器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</ artifactId> <-! Universal mapper Launcher -> </ dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> </dependency> <!-- 分页助手启动器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- jdbc启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--wuwei-item-interface实体类--> <dependency> <groupId>com.wuwei.item.interface</groupId> <artifactId>wuwei-item-interface</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- springboot检测服务启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
What leyou-item-interface needs we do not know, so the first matter. After what needs to rely, reintroduction.
7. Write the startup configuration
Throughout leyou-item工程
, only leyou-item-service
it needs to start. So where to start writing class:
/*@SpringBootApplication //eureka客户端 @EnableDiscoveryClient*/ @SpringBootApplication @EnableDiscoveryClient public class WuweiItemServiceApplication { public static void main(String[] args) { SpringApplication.run(WuweiItemServiceApplication.class); } }
Then in the global properties file:
# Modify the port number Server: Port: 8081 # modify the program name the Spring: the Application: name: Item - Service # need to connect to database configuration jdbc the DataSource: url: jdbc: MySQL: /// Wuwei username: root password: root Hikari: max - Lifetime: 28.83 million # a connection life duration (ms), time-out and is not in use were released (Retired), default: 30 minutes, set a specific database 30-second timeout length less, with reference to MySQL wait_timeout parameters (Show Variables like ' timeout%% ' ;) maximum -pool-size: . 9 # connection pool maximum number of connections allowed. Default: 10 ; Recommended formula: ((core_count * 2 ) +effective_spindle_count) path #eureka service side of the Eureka: Client: Service - url: defaultzone: HTTP: // localhost: 10086 / Eureka instance: Lease -renewal-interval- in -seconds: 5 # 5 seconds to send a heartbeat Lease -expiration- duration- in -seconds: 10 # 10 seconds the sending expired
Since the micro goods and services has been created, is sure to add routing rules to Zuul, we do not use the default routing rules.
Modify leyou-gateway project application.yml profile:
Zuul: prefix: / API # routing path prefix routes: Item -service: / Item / * * # commodity to map the path of micro-services
View Eureka panel:
10. Test routing rules
To test whether the flow routing rules, we are not required to write a controller interface item-service as well?
In fact, do not need, SpringBoot provides a dependent: actuator
As long as we rely actuator is added, it will generate a series of access interface for us:
-
/info
-
/health
-
/refresh
-
...
Add rely :( already added over)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
After restarting access Eureka console:
Hover over the item-service, will show an address:
This is the interface provided by the actuator, we click access to:
Because we do not add information, it is an empty json, but it is certain that: we have access to the item-service.
Next we try to access through the route, according to the routing rules, we need to access the address is:
http://127.0.0.1:10010/api/item/actuator/info
Some tools or generic content of the agreement, we hope to share each service, you need to create a tool modules:leyou-common
Right leyou project using maven to build the module:
location information:
structure:
It is not required coding.
12. The project to build a complete environment