What is Spring Cloud Eureka

Eureka is an open source service registration and discovery component developed by Netflix. Service discovery can be said to be the core function of micro-service development. After micro-service deployment, there must be service registration and discovery capability, and Eureka plays this role. If you’ve used Dubbo, service registration and discovery in Dubbo is done through the Zookeeper framework. Eureka is currently in version 2.2.x, which has been officially announced as no longer being maintained or updated. However, Eureka has been widely used in production as a registry and is stable. From my personal point of view, the current use of Ali’s Nacos and Consul components is more than service discovery and registration, microservice development does not have to rely on more components and frameworks. This article simulates Eureka Server clusters and service provider clusters and service consumption.

Release notes

SpringCloud + SpringBoot development microservices is not the newer the better. SpringCloud officially provides a version mapping. The latest is Hoxton, which corresponds to SpringBoot 2.2.x.

The preparatory work

  • Create a parent project, the main agreement is SpringCloud, SpringBoot version number, I used Hoxton.SR1, SpringBoot2.2.
  • Create 5 sub-modules, there are two Eureka servers, two Services, one consumer, and two Eureka servrs. I use ports 7001 and 7002 to simulate and distinguish the Eureka cluster. Two services are simulated with 8001 and 8002. The main purpose is to reflect client load balancing in service consumption.

The project structure is shown below

The parent project pom. XML

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - dependencies < / artifactId > < version > 2.2.2. RELEASE < / version > < type > pom < type > <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code

Service Registry

Service provider I used 7001 and 7002 simulation cluster, both program code is the same. Create a Module and add Spring-cloud-starter-Netflix-Eureka-Server. pom.xml

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency>  <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>Copy the code

application.yml

server: port: 7001 eureka: client: service-url: defaultZone: http://eureka7002.com:7002/eureka # cluster center is to point to other configuration register - with - eureka: false fetch - registry: false instance: hostname: Eureka7001.com #eureka server: enable-self-preservation: falseCopy the code

Modifying the startup class

@SpringBootApplication
@EnableEurekaServer
public class MyEurekaServer7001 {

    public static void main(String[] args) { SpringApplication.run(MyEurekaServer7001.class,args); }}Copy the code

The only difference for 7002 is in the application.yml configuration. Eureka.client.service-url.defaultzone needs to be configured for other server nodes in the cluster. In this case, instance.hostname is set to Eureka7001.com. You need to modify the local hosts

127.00.1 eureka7001.com.127.00.1 eureka7002.com.Copy the code

Start two registry services Eureka7001









Service provider

Service providers HERE I use 8001 and 8002 to simulate the cluster.


<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency>  <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>Copy the code

application.yml

server: port: 8001 spring: application: name: CLOUD-STUDENT-SERVICE eureka: client: register-with-eureka: True # Set this parameter to true for the cluster node to use the Ribbon load balancing service with clients. Fetch -registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka # need to configure the cluster all registry instance: the instance - id: Student-service8001 prefer-ip-address: true # The IP address can be displayed in the access pathCopy the code

Add a service interface

@RestController
@RequestMapping("/student")
public class StudentController {

    @GetMapping("/list")
    public List<String> list(a){
        return Arrays.asList("Kobe"."Lin"."Tim"."James");
    }

    @GetMapping("/version")
    public String version(a){
        return "8001202071231"; }}Copy the code

Modifying the startup class

@SpringBootApplication
@EnableEurekaClient
public class MyStudentService8001 {
    public static void main(String[] args) { SpringApplication.run(MyStudentService8001.class,args); }}Copy the code

Start two service providers 8001 and 8002 in turn. The Eureka Server page is refreshed without error after the service is started. eureka7001









Service consumer

Service consumer I just wrote a Module with port 8087 POM.xml

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>Copy the code

Application. Yml Where clients need to configure two registry addresses in the cluster when consuming services.

server:
  port: 8087

spring:
  application:
    name: student-consumer

eureka:
  client:
    fetch-registry: true
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
Copy the code

Service consumption interface Service consumption we still use the RestTemplate. The client load balancing is actually the Ribbon component, which uses the rotation algorithm by default.

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/version")
    public String index(a){
        return restTemplate.getForObject("http://CLOUD-STUDENT-SERVICE/student/version",String.class); }}Copy the code

Modify the startup class in the startup class I also configured the RestTemplate, the startup class two key annotations SpringBootApplication and EnableEurekaClient.

@SpringBootApplication
@EnableEurekaClient
public class StudentConsumer8087 {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(a){
        return new RestTemplate();
    }
    public static void main(String[] args) { SpringApplication.run(StudentConsumer8087.class,args); }}Copy the code

Up and access the service interface http://localhost:8087/student/version, consumers can see from the output will be training in rotation call interface in 8001 and 8002, 8002202071231, 0800, 1202071231 0.