1 eureka server
Create a maven project, and the pom file introduces dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Configuration file: application.yml
server:
port: 8761
spring:
application:
name: eureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
Startup class:
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
Start the project and visit: http://localhost:8761/
2 Registration service
Based on the sales and user projects in the previous article, taking the user project as an example, the sales project is similar, and the following files are modified:
2-1 Add pom dependencies
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2-2 update application.yml
server:
port: 8081
spring:
application:
name: sales
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1/test?characterEncoding=UTF-8&useSSL=false
username: root
password: root
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
# mybatis
mybatis:
mapperLocations: classpath:mapper/*.xml
2-3 Modify the startup class and add the @EnableDiscoveryClient annotation
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
2-4 Start the sales and user projects and visit: http://localhost:8761/
3 Code address
eureka:https://code.aliyun.com/995586041/eureka.git
eureka_user:https://code.aliyun.com/995586041/eureka_user.git
eureka_sales:https://code.aliyun.com/995586041/eureka_sales.git