spring cloud eureka service discovery registration

Keywords: Programming Spring Maven Apache xml

Environment description: spring boot 2.0 + JDK1.8

Step 1: Build the eureka server, create a maven project, or directly idea or https://start.spring.io/ Quick creation pom.xml file

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hht</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <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>

</project>

Startup class

package com.hht.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

configuration file

spring.application.name=spring-cloud-eureka-server
server.port=12345

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=\
  http://localhost:${server.port}/eureka

Note the single node situation:

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

Access after successful startup http://localhost:12345/

The second step is to build the end part of eureka client, including service registration, discovery and use. The overall structure is as follows

user-api module is used to define interfaces

package com.hht.user.domain;

/**
 * @author hht
 * @ClassName User
 * @Description TODO
 * @Date 2019/4/30 15:50
 * @VERSION 1.0
 */
public class User {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.hht.user.service;

import com.hht.user.domain.User;
import org.springframework.stereotype.Service;

import java.util.Collection;

public interface UserService {
    /**
     * Save user
     *
     * @param user
     * @return If the save is successful, return < code > true </code>.
     * Otherwise return < code > false </code >
     */
    boolean save(User user);

    /**
     * Query all user objects
     *
     * @return Will not return < code > null </code >
     */
    Collection<User> findAll();
}

user-service-consumer module is used to simulate service discovery and usage

pom.xml

<?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>eureka-client</artifactId>
        <groupId>com.hht</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.hht</groupId>
            <artifactId>user-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>
# apply name
spring.application.name=user-service-client
server.port=8080

# Eureka server address
eureka.client.serviceUrl.defaultZone= http://localhost:12345/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=192.168.1.160
@SpringBootApplication
@EnableDiscoveryClient
public class UserConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumerApplication.class,args);
    }
}

Implement the interface class through restTemplate (experiment, general production environment has alternatives)

package com.hht.service;

import com.hht.user.domain.User;
import com.hht.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @author hht
 * @ClassName com.hht.service.UserServiceProxy
 * @Description TODO
 * @Date 2019/4/30 16:16
 * @VERSION 1.0
 */
@Service
public class UserServiceProxy implements UserService {
    private static final String PROVIDER_SERVER_URL_PREFIX = "http://user-service-provider";

    /**
     * Proxy to server provider through REST API
     */
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public boolean save(User user) {
        User returnValue =
                restTemplate.postForObject(PROVIDER_SERVER_URL_PREFIX + "/user/save", user, User.class);
        return returnValue != null;
    }

    @Override
    public Collection<User> findAll() {
        return restTemplate.getForObject(PROVIDER_SERVER_URL_PREFIX + "/user/list", Collection.class);
    }
}

user-service-provider module is used to simulate service providers

pom.xml

<?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>eureka-client</artifactId>
        <groupId>com.hht</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.hht</groupId>
            <artifactId>user-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.9.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.9.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>
# apply name
spring.application.name=user-service-provider
# Eureka Client Port
server.port=8081
# Eureka server address
eureka.client.serviceUrl.defaultZone= http://localhost:12345/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=192.168.1.160

Interface Implementation Class, True Logic of Business

package com.hht.service;

import com.hht.respository.UserRespository;
import com.hht.user.domain.User;
import com.hht.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserRespository respository;

    @Override
    public boolean save(User user) {
        return respository.save(user);
    }

    @Override
    public Collection<User> findAll() {
        return respository.findAll();
    }
}

This concludes: The overall logic is that both client s are registered on the server side and invoke another service by accessing the service name + method Additional: General production environments require high availability Just modify the configuration file of eureka server and start more than one

eureka.client.register-with-eureka=false  #Remove
eureka.client.fetch-registry=false        #Remove
eureka.client.service-url.defaultZone=\   #Fill in the address of other server s except yourself
  http://localhost:${server.port}/eureka

Test code address: https://gitee.com/huhaitao/spring_cloud_test/tree/master/eureka

Posted by timgetback on Sat, 05 Oct 2019 23:55:00 -0700