Recently, I have been learning Spring Cloud and came across Eureka. I found that IDEA now supports copying instances to WSL, so I decided to explore it myself.
Configuration of WSL#
If you are not familiar with WSL or have not installed it yet, please search for it on your own.
The following content is based on Ubuntu 20, please replace the commands with the appropriate ones for other versions.
Installing JDK#
If you don't have JDK, you won't be able to run Spring Boot or Cloud projects in your WSL environment. Therefore, installing JDK is the first step.
Installing JDK on Ubuntu is much simpler compared to Windows, as it doesn't require setting environment variables. Just one command is enough.
apt install openjdk-8-jre-headless # Install openjdk 8
After installation, use the following command to check the version:
java -version # Check the version
Configuring JDK Path#
After installation, you need to configure the JDK path in IDEA for WSL.
The default configuration is:
/usr/lib/jvm/java-8-openjdk-amd64
Configuration of Copied Instance#
Select an instance running on your local Windows machine, right-click and choose "Copy Configuration" to copy the instance.
Next, you need to modify the configuration of the copied instance, such as the port. If you don't make any changes, there may be port conflicts.
It is recommended to write the configuration information in the format of application.properties
for easy copying.
Configure the port in VM options using the shortcut Alt
+ V
:
-Dserver.port=8082 # Use 8082 to override the original port
Configure the Eureka and database information in Program arguments using the shortcut Alt
+ R
:
# Replace 192.168.0.192 with the IP address of your local Windows machine, and 10086 with the port configured for Eureka
--eureka.client.service-url.defaultZone=http://192.168.0.192:10086/eureka
# Modify the database configuration
--spring.datasource.url=jdbc:mysql://192.168.0.192:3306/cloud_user?useSSL=false
--spring.datasource.username=root
--spring.datasource.password=root
--spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Notes#
If you want to test whether you can access the MySQL database on Windows, you can install MySQL:
apt-get install mysql-server
Then, test remote access to MySQL:
mysql -h 192.168.0.192 -uroot -p
If you encounter a timeout error, you need to open port 3306 on Windows.
Open the Windows Firewall and select "New Rule" on the right side of Inbound Rules.
Choose "Port".
Enter the default MySQL port 3306.
Click "Next" and proceed with the default settings.
Summary#
As you can see, Eureka has discovered 2 instances: one running on Windows and one running on WSL.
There is no problem accessing the database.