In a real project, a system consists of many microservices. Spring Cloud provides Eureka as a solution to who organizes and coordinates the connections and invocation relationships between microservices. So in this article, we will continue what we did in the last article, and talk about how to implement service registration and discovery through Eureka through scaffolding.
1. Eureka is introduced
Eureka is a C/S architecture, which means that Eureka consists of a server and a client. In our project, both the service provider and the consumer are service clients, and we need to register the service to the Eureka Server, so we need to build a new sub-project springcloud-Eureka-server-8300 as the Eureka Server.
2. Set up the Eureka Server
2.1 Create the Springcloud-Eureka-server-8300 submodule
1.1 POM. XML introduces related JAR packages
<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>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-server-8300</artifactId>
<dependencies>
<! --eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<! --spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Hot deployment -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
2.2 Added application.yml configuration
server:
port: 8300
spring:
application:
name: springcloud-eureka-server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
2.3 New main startup class eurekaserver8300.java
The key is to add the @enableeurekaserver annotation
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8300 {
public static void main(String[] args){ SpringApplication.run(EurekaServer8300.class, args); }}Copy the code
2.4 test
Now that we have set up the Eureka Server project, it is time to start the test by starting the Eureka Server
The browser type http://localhost:8300/ and we find the application instance block empty because we haven’t started configuring the Eureka client yet.
3. Set up the Eureka Client
Eureka Client is springcloud-product-Provider-8100 and Springcloud-product-Consumer-8200, which are newly added in our last article. We need to do three steps. Import the JAR package, add annotations, and modify the configuration.
3.1 POM. XML introduces the JAR package
<! --eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code
3.2 application.yml Adds Eureka configurations
eureka:
instance:
instance-id: ${spring.application.name}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/ # Eureka address
Copy the code
3.3 Add the @enableDiscoveryClient annotation to the primary startup class
package com.elio.springcloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@MapperScan("com.elio.springcloud.dao")
@EnableDiscoveryClient
public class ProductProvider8100 {
public static void main(String[] args){ SpringApplication.run(ProductProvider8100.class, args); }}Copy the code
3.4 test
The steps of the two projects are the above three steps. After the operation, we can start the 8100 and 8200 projects. Then refresh the Eureka service address in the browser
4. Set up the Cluster Eureka Server
In the last two steps, we have successfully built the single-machine version of Eureka Server and Eureka Client. However, in the actual production, the deployment of microservices is by cluster. A microservice may be deployed on multiple servers, and a Eureka Server must also be deployed in cluster. Because when one Eureka Server node fails, other Eureka servers can continue to provide service discovery function, then step by step Eureka Server cluster. In this example, we will create a new Eureka Server 8301 project and Eureka Client 8201 project.
4.1 Adding Eureka Server 8301
4.4.1 springcloud – eureka – new server – 8301
4.1.2 POM. XML introduces the JAR package
<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>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-server-8301</artifactId>
<dependencies>
<! --eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<! --spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Hot deployment -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
4.1.3 Adding the application.yml configuration file
server:
port: 8301
spring:
application:
name: springcloud-eureka-server-8301
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:8300/eureka/
Copy the code
3.1.4 New primary startup class EurekaServer8301
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8301 {
public static void main(String[] args){ SpringApplication.run(EurekaServer8301.class, args); }}Copy the code
4.1.5 test
So far we have successfully set up the second Eureka Server service project, the next step is to start the 8301 project
Modify the configurations of Eureka Server 8300 and Eureka Client 8100 and 8200
Above, we have successfully built Eureka Server 8301 project. Next, we need to modify the configuration and register 8300,8100 and 8200 to 8301. Next, we need to directly modify the Eureka configuration of these three projects
4.1.6 Modifying the Service Provider and Consumer Configurations
8300 application.yml
server:
port: 8300
spring:
application:
name: springcloud-eureka-server-8300
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:8301/eureka/
Copy the code
8100 application.yml
server:
port: 8100 # port
spring:
application:
name: springcloud-product-provider-8100
datasource:
url: jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8&userSSL=false
driverClassName: com.mysql.jdbc.Driver
username: root
password: 111111
mybatis:
mapper-locations: classpath:mapping/*mapper.xml # Mybatis maps file location
type-aliases-package: com.elio.springcloud.entity # Entity class package for table
eureka:
instance:
instance-id: ${spring.application.name}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
Copy the code
8200 application.yml
server:
port: 8200
spring:
application:
name: springcloud-product-consumer-8200
eureka:
instance:
instance-id: ${spring.application.name}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
Copy the code
4.1.7 Cluster Test
The next step is to start projects 8300, 8301, 8100 and 8200 successively. After the four projects are started successfully, the interface of 8300 and 8301 is opened, and it is found that 8100 and 8200 are registered to the two Eureka servers successfully
5. Set up the Cluster Eureka Client
In the previous step, we successfully created the Eureka Server cluster, but we have not yet tested the microservice provider and consumer cluster, so we will create the Eureka Client cluster using the consumer cluster as an example. We next add a 8201 consumer, create the steps and 8200 steps are exactly the same, but the name is not the same, in this author do not repeat, directly posted the key code.
5.1 POM. XML introduces the JAR package
<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>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-product-consumer-8201</artifactId>
<dependencies>
<! --eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<! --spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Hot deployment -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<! --lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<! -- Hot start plugin -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
5.2 Added application.yml configuration
server:
port: 8201
spring:
application:
name: springcloud-product-consumer-8201
eureka:
instance:
instance-id: ${spring.application.name}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
Copy the code
5.3 New main ProductConsumer8201 startup class
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableDiscoveryClient
public class ProductConsumer8201 {
public static void main(String[] args){ SpringApplication.run(ProductConsumer8201.class, args); }}Copy the code
5.4 test
After successful startup, you can see the following figure has been successfully registered to the two servers, so far we have successfully set up the Eureka Server and Eureka Client stand-alone version and cluster version.
6. Build public projects
Remember the two questions we asked in the last post. The first problem, registration between services, we’ve already implemented. The second problem is that common classes between services, such as the Result class, are referenced. When the Result class is modified, we will have to modify multiple microservice projects. This is obviously unwise, so we need a common module to build a common API.
6.1 Adding a Public Module
The new common module is the same as the above submodules. Just post the important code and configuration here
6.2 the pom. The XML configuration
<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>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-common-api</artifactId>
<dependencies>
<! --spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Hot deployment -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<! --lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<! -- Hot start plugin -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
6.3 Adding the Result class
package com.elio.springcloud.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class Result {
private int code;
private String message;
private Object result;
}
Copy the code
6.4 Consumers and providers introduce JAR packages
Since it is only a common entity class module, there is no need to add a new configuration and startup class here. The next step is to modify the POM.xml of the consumer 8200 and provider 8100 to introduce the following configuration
<! --commom-api -->
<dependency>
<groupId>com.elio.springcloud</groupId>
<artifactId>springcloud-common-api</artifactId>
<version>1.0 the SNAPSHOT</version>
</dependency>
Copy the code
After the dTO is successfully imported, the DTO folders in the 8200 and 8100 are deleted
6.5 test
Restart the 8200 and 8100 projects and test them in the browser
7. To summarize
In this article, we successfully configured the Eureka Server and Eureka Client, and set up the stand-alone version and the cluster version respectively. Through the construction steps, we understand the basic use of Eureka. The specific principle is not covered in this paper, and we will write another article to analyze it. The problem with the current project is that in the service consumer, the service provider address is still dead. The second issue, which provider the service consumer should visit if the service provider is deployed in clusters, will be addressed in the next article.