Framework: SpringBoot + SpringCloudAlibaba
Project deployment: dockerfile + docker compose
Software architecture: MySQL + Nacos
Splitting of microservices:
Bank admin API: main project (parent project)
Bank Commons: public services
Bank accounts: Account Service
Bank records: transfer recording service
Bank gateway: Gateway Service
Framework: SpringBoot + SpringCloudAlibaba
Project deployment: dockerfile + docker compose
Software architecture: MySQL + Nacos
1. Create an ordinary Maven project
bank-admin-api
Added dependencies
<?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.yf</groupId> <artifactId>bank-admin-api</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>bank-commons</module> <module>bank-accounts</module> <module>bank-records</module> <module>bank-gateway</module> </modules> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> <!--springcloud edition--> <springcloud-version>2020.0.1</springcloud-version> <!--springcloud-alibaba edition--> <cloud-alibaba-version>2021.1</cloud-alibaba-version> <!--mysql Version of--> <mysql-version>8.0.13</mysql-version> <!--mybatis-plus Version of--> <mybatis-plus.version>3.4.3.1</mybatis-plus.version> </properties> <dependencyManagement> <dependencies> <!--Here you need to provide springcloud Version of--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${springcloud-version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--This is springcloud-alibaba Version of--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${cloud-alibaba-version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-version}</version> </dependency> </dependencies> </dependencyManagement> </project>
Note: it only manages the version, does not depend on the version, and only packages the main project
2. Create another module (select Maven)
bank-commons
3. Create a new module again (select Maven)
bank-records
5. Create a bank gateway, the most important service gateway
After the project environment is set up
Create a new docker-compose.yml file under the bankadmin API main project
version: "3.3" # The whole project needs to globalize the networks on a network networks: bank: # Volume group volumes: data: services: mysql: # image image: mysql:5.7 # port ports: - "3306:3306" # environment environment: # MYSQL_ROOT_PASSWORD password - "MYSQL_ROOT_PASSWORD=root" # "MYSQL_DATABASE=bank" database is bank - "MYSQL_DATABASE=bank" # Create from data volume volumes: - data:/var/lib/mysql - ./bank.sql:/docker-entrypoint-initdb.d/bank.sql networks: - bank nacos: image: nacos/nacos-server:1.4.1 ports: - "8848:8848" environment: - "MODE=standalone"
Then drag the bank.sql file into the main project
Create a new remote connection
Use docker ps -a to view all containers
Right click bank(192.168.37.134/root) to create a new folder named bank
Then drag the bank.sql file and docker-compose.yml into the bank folder
Verify that the docker-compose.yml file is correct
Use the docker compose config command to verify. If it is correct, it will output docker-compose.yml. If it is wrong, it will output ERROR
Use docker compose up - D to run the container from the background
Use docker ps to view the started container
Check whether the virtual machine can be connected to MySQL
View two tables of data
Drag the docker-compose.yml file into the bank folder again to overwrite it
Then use docker compose up nacos to start nacos
Then visit 192.168.37.134:8848/nacos If the visit is successful, it means success
Next, write pom.xml in bank accounts to add related dependencies
<?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"> <parent> <artifactId>bank-admin-api</artifactId> <groupId>com.yf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>bank-accounts</artifactId> <dependencies> <!--web rely on--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Service registration and discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
Create a BankAccountsApplication class under the java package under the bank accounts module
package com.yifeng; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient /** * Open service and registration */ public class BankAccountsApplication { public static void main(String[] args) { SpringApplication.run(BankAccountsApplication.class,args); } }
Create application.yml in the resource directory of the bank accounts module
server: port: 8881 spring: application: name: bank-accounts
Add related dependencies in pom.xml in bank gateway
<?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"> <parent> <artifactId>bank-admin-api</artifactId> <groupId>com.yf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>bank-gateway</artifactId> <dependencies> <!-- Service registration and discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- gateway gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Load Balancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies> </project>
Create a BankGatewayApplication class under the java package under the bank gateway module
package com.yifeng; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class BankGatewayApplication { public static void main(String[] args) { SpringApplication.run(BankGatewayApplication.class,args); } }
Create application.yml in the resource directory under the bank gateway module
server: port: 8888 spring: application: name: bank-gateway
Add related dependencies to pom.xml in bank records
<?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"> <parent> <artifactId>bank-admin-api</artifactId> <groupId>com.yf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>bank-records</artifactId> <dependencies> <!--web rely on--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Service registration and discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Load Balancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies> </project>
Create a BankRecordsApplication class under the java package under the bank records module
package com.yifeng; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class BankRecordsApplication { public static void main(String[] args) { SpringApplication.run(BankRecordsApplication.class,args); } }
Create application.yml in the resource directory under the bank records module
server: port: 8882 spring: application: name: bank-records
Create a controller package and a RecordsController class under the bank record module
package com.yifeng.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RecordsController { @GetMapping("/test") public String test(){ return "records ok"; } }
Create a controller package and a RecordsController class under the bank accounts module
package com.yifeng.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AccountsController { @GetMapping("/test") public String test(){ return "records ok"; } }
Open the Services run window of IDEA
① Double click the. idea folder to find the workspace.xml file
② Edit the workspace.xml file and add the following configuration code at the end
<component name="RunDashboard"> <option name="configurationTypes"> <set> <option value="SpringBootApplicationConfigurationType" /> </set> </option> </component>
④ At this time, you can see the execution window as shown in the following figure in the Services window of the toolbar
After three runs
To access BankAccountsApplication from the browser: http://localhost:8881/test
To access BankRecordsApplication from a browser: http://localhost:8882/test
To access the BankGatewayApplication from the browser: http://localhost:8888/test
Next, configure the gateway
In the resources directory under the bank gateway module, configure some gateway related things in the original application.yml
server: port: 8888 spring: application: name: bank-gateway cloud: nacos: discovery: server-addr: 192.168.37.134:8848 gateway: routes: - id: Accounts uri: lb://bank-accounts predicates: - Path=/accounts/** filters: - StripPrefix=1 - id: Records uri: lb://bank-records predicates: - Path=/records/** filters: - StripPrefix=1
be careful: The gateway must be at the same level as nacos, or the page will 404
Visit localhost:8888/accounts/test
Visit localhost:8888/records/test
If the access is successful and something is returned, it means that the gateway has been configured
Remember to create a docker file under each module
1. First create a Dockerfile under the bank accounts module
FROM daocloud.io/library/java:8-jre-alpine #FROM... Is equivalent to introducing Java 8 MAINTAINER yifeng # Who is the author of MAINTAINER ARG JAVA_OPTS="-Xmx128M" ENV JAVA_OPTS=$JAVA_OPTS ARG RUN_ARGS="--spring.profiles.active=prod" ENV RUN_ARGS=$RUN_ARGS COPY ./account.jar /account.jar ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar account.jar $RUN_ARGS"]
Then package
Add a packaging plug-in to pom.xml under the bank accounts module
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
After adding it, the Plugins of Maven's bank accounts will have an additional spring boot
As long as you have this, you can pack it
Next, relevant plug-ins should be added to bank gateway and bank records
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
There's a lightning sign in Maven on the right
This lightning symbol represents Toggle 'Skip Tests' Mode Meaning of skipping the test
Someone might ask what to skip the test? Because there may be some wrong code or nonstandard code in the tested code
It will give us verification and generate test reports
First step
First, clean the Lifecycle under the bank admin API
Step 2
Then package the Lifecycle package under the bank admin API
Create another accounts folder in the bank folder under the Remote Host
Then drag the bank-accounts-1.0-SNAPSHOT.jar and Dockerfile files into the accounts folder
Later, rename bank-accounts-1.0-SNAPSHOT.jar to account.jar
Then use the docker build -f Dockerfile -t account:1.0. Command
Until it appears Successfully
Then use docker images to view the image
Rerun container
docker run --name account -p 8881:8881 -d account:1.0
After running, use docker ps to view
Using your own host path to access 192.168.37.134:8881/test
Finally, copy the Dockerfile file under bank accounts to the bank gateway and bank records modules
Dockerfile under gateway
FROM daocloud.io/library/java:8-jre-alpine #FROM... Is equivalent to introducing Java 8 MAINTAINER yifeng # Who is the author of MAINTAINER ARG JAVA_OPTS="-Xmx128M" ENV JAVA_OPTS=$JAVA_OPTS ARG RUN_ARGS="--spring.profiles.active=prod" ENV RUN_ARGS=$RUN_ARGS COPY ./gateway.jar /gateway.jar ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar gateway.jar $RUN_ARGS"]
Dockerfile under records
FROM daocloud.io/library/java:8-jre-alpine #FROM... Is equivalent to introducing Java 8 MAINTAINER yifeng # Who is the author of MAINTAINER ARG JAVA_OPTS="-Xmx128M" ENV JAVA_OPTS=$JAVA_OPTS ARG RUN_ARGS="--spring.profiles.active=prod" ENV RUN_ARGS=$RUN_ARGS COPY ./records.jar /records.jar ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar records.jar $RUN_ARGS"]
be careful:
Possible bug s
Error: Unable to access jarfile account.jar
There is no jar package in the container, not without permission
application-pord.yml the YML file needs to be placed in nacos
Create the application-pord.yml file in the resources directory under each module
Note: hierarchical relationship
application-pord.yml under bank accounts
server: port: 8881 spring: application: name: bank-accounts cloud: nacos: discovery: server-addr: 192.168.37.134:8848
"I wrote it directly into the application.yml file" under the bank gateway
server: port: 8882 spring: application: name: bank-records cloud: nacos: discovery: server-addr: 192.168.37.134:8848 profiles: active: prod
application-pord.yml under bank records
server: port: 8882 spring: application: name: bank-records cloud: nacos: discovery: server-addr: 192.168.37.134:8848 profiles: active: prod
Remember: profiles is at the same level as application
profiles: active: prod
Create three more folders later and drop the gateway and records into the folder respectively
Next, configure the nacos service
Create namespace first
ID of the namespace, which is automatically generated using UUID
Then click the configuration of the bank namespace
be careful:
bootstrap.yaml cannot be read by default in springcloudAlibaba 2020/2021
You need to add two dependencies to pom.xml under the bank account module
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <!--Configuration of services--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
Right click to create the bootstrap.yaml file under the bank account module
spring: cloud: nacos: config: server-addr: 192.168.37.134:8848 file-extension: yaml namespace: cfd852a7-d97c-42b2-87ff-022a403b0d66 group: bank-accounts application: name: bank-accounts profiles: active: prod
Finally, run locally
Return the result after running!