1. Create a service registry
Here, I will continue to use Eureka as a component for service registration and discovery, which Consul will cover in more detail later.
1.1 First create a Maven main project.
First, create a main Maven project and introduce dependencies in its POM file with spring Boot version 2.0.3.RELEASE and Spring Cloud version finchley.release. This POM file acts as a parent that depends on version control and is inherited by other Module projects. This is the pattern for all articles in this series, and the POM for the other articles is the same as this POM. Again, I’m not going to introduce it again. The code is as follows:
<?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.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>pom</packaging>
<name>sc-f-chapter1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>. The 2.0.3 RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>eureka-server</module>
<module>service-hi</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
1.2 Then create two Model projects:
One model project acts as a service registry, Eureka Server, and the other as a Eureka Client.
The following uses server creation as an example to explain the creation process in detail:
Right-click project -> Create Model -> Select Spring Initialir as shown below:
Next -> Select Cloud Discovery -> Eureka Server and proceed to the next step.
The poM. XML inherits the parent POM file and introduces the spring-cloud-starter-Netflix-Eureka-server dependency as follows:
<?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.forezp</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Copy the code
1.3 Starting a Service registry
All you need is a @enableeurekaserver annotation, which needs to be added to the SpringBoot project’s startup Application class:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) { SpringApplication.run( EurekaServerApplication.class, args ); }}Copy the code
Eureka is a highly available component, it has no back-end cache, and every instance needs to send a heartbeat to the registry after registration (so it can be done in memory). Erureka Server is also a Eureka Client by default, and a server must be specified. Eureka server configuration file appication. Yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
Copy the code
Through eureka. Client. RegisterWithEureka: false and fetchRegistry: false to show itself to be a eureka server.
Eureka Server has an interface. Start the project, open a browser and visit http://localhost:8761. The interface is as follows:
No application available No service found…… Because no service is registered, no service can be found.
Create a service provider (Eureka Client)
When a client registers with a server, it provides metadata such as host and port, URL, home page, and so on. Eureka Server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually removed from the registry server.
The process for creating pom.xml is similar to that for server.
<?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.forezp</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-hi</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter1</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
Indicate that you are a Eurekaclient by annotating @enableeurekaclient.
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceHiApplication.class, args );
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi " + name + " ,i am from port:"+ port; }}Copy the code
@enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient @enableeurekaclient
server:
port: 8762
spring:
application:
name: service-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Copy the code
It is important to specify spring.application.name, which will be used for future calls between services. To start the project, go to http://localhost:8761, which is eureka Server’s website:
You will find that a SERVICE is already registered with the SERVICE, service-hi, and port 7862
Open http://localhost:8762/hi? Name =forezp, you will see in your browser:
hi forezp,i am from port:8762
Reprinted from: blog.csdn.net/forezp/arti…