SpringCloud Eureka服務注冊及發現——服務端/客戶端/消費者搭建


Eureka 是 Netflix 出品的用於實現服務注冊和發現的工具。 Spring Cloud 集成了 Eureka,並提供了開箱即用的支持。其中, Eureka 又可細分為 Eureka Server 和 Eureka Client。

Eureka服務端,實現服務注冊中心,eg:mima-cloud-eureka
Eureka客戶端,將服務注冊到 Eureka,分為服務提供者和服務消費者,eg:mima-cloud-eureka-producer和mima-cloud-eureka-consumer

mima-cloud-eureka——Eureka服務注冊,注冊mima-cloud-eureka-producer和mima-cloud-eureka-consumer服務
mima-cloud-eureka-producer——Eureka服務提供者
mima-cloud-eureka-consumer——Eureka服務消費者

模擬mima-cloud-eureka-producer和mima-cloud-eureka-consumer服務注冊到mima-cloud-eureka注冊中心;
模擬mima-cloud-eureka-producer服務提供get()方法供mima-cloud-eureka-consumer服務消費,訪問mima-cloud-eureka-consumer服務,瀏覽器輸出"hi,${id}"字符串。

1、Eureka 服務端
a、新建mima-cloud-eureka項目

b、pom.xml

<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>com.mimaxueyuan</groupId>
        <artifactId>mima-cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>mima-cloud-eureka</artifactId>
    <packaging>jar</packaging>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
</project>

c、application.yml

debug: false
spring:
  application:
    name: mima-cloud-eureka
security:
  basic:
    enabled: false
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    #不向Eureka注冊自己
    register-with-eureka: false
    #不檢查其他的EurekaServer節點
    fetch-registry: false
    service-url:
      #設置eureka服務器所在的地址,查詢服務和注冊服務程序都注冊到這個地址(服務暴露的地址)
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/   

d、在啟動類中使用EnableEurekaServer注解

package com.mimaxueyuan.cloud.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args);
    }
    
}

e、啟動Eureka服務端,效果如下

No instances available,表示目前還沒有服務注冊進來。

現在,我們已經注冊了一個服務注冊中心。

2、Eureka 客戶端,將服務注冊到Eureka
2.1、mima-cloud-eureka-producer——Eureka 客戶端,服務提供者
a、新建mima-cloud-eureka-producer項目

b、pom.xml

<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>com.mimaxueyuan</groupId>
        <artifactId>mima-cloud-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>mima-cloud-eureka-producer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
</project>

c、application.yml

debug: false
spring:
  application:
    name: mima-cloud-eureka-producer
server:
  port: 9907
eureka:
  client: 
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port} 

d、ProducerController業務類,提供get()方法

package com.mimaxueyuan.producer.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.mimaxueyuan.producer.entity.User;

@RestController
public class ProducerController {
    
    @GetMapping("/get/{id}")
    public String get(@PathVariable String id) {
        return "hi,"+id;
    }
}

e、啟動類中使用@EnableEurekaClient注解

package com.mimaxueyuan.producer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ProducerApplication { public static void main(String[] args) { new SpringApplicationBuilder(ProducerApplication.class).web(true).run(args); } }

 

f、啟動Eureka客戶端
訪問http://localhost:8761/

看到MIMA-CLOUD-EUREKA-PRODUCER,表示mima-cloud-eureka-producer服務已注冊進來,交給mima-cloud-eureka注冊中心維護管理。


2.2、mima-cloud-eureka-consumer——Eureka 客戶端,服務消費者
a、新建mima-cloud-eureka-consumer項目

b、pom.xml

<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>com.mimaxueyuan</groupId>
    <artifactId>mima-cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>mima-cloud-eureka-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
</project>

c、application.yml

spring: application: name: mima-cloud-eureka-consumer server: port: 8802 eureka: instance: prefer-ip-address: true client: serviceUrl: defaultZone: http://localhost:8761/eureka/

d、ConsumerController業務類,調用get()方法

package com.mimaxueyuan.consumer.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.mimaxueyuan.consumer.entity.User; @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping("/c/get/{id}") public String get(@PathVariable String id) { String result = restTemplate.getForObject("http://mima-cloud-eureka-producer/get/"+id, String.class); return result; } }

e、啟動類中使用@EnableEurekaClient注解

package com.mimaxueyuan.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class ConsumerApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }

 

f、啟動Eureka客戶端
訪問http://localhost:8761/

看到MIMA-CLOUD-EUREKA-CONSUMER,表示mima-cloud-eureka-consumer服務已注冊進來,交給mima-cloud-eureka注冊中心維護管理。

訪問http://localhost:8802/c/get/100

現在,Eureka 服務的注冊和發現的例子模擬完成。

3、父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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mimaxueyuan</groupId>
    <artifactId>mima-cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>mima-cloud-eureka</module>
        <module>mima-cloud-eureka-producer</module>
        <module>mima-cloud-eureka-consumer</module>
    </modules>
</project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM