eureka环境搭建

139 阅读3分钟

eureka介绍

eureka的作用

通过一些列的RESTful-api来实现服务注册和发现。

eureka可以将服务进行统一的管理,保管着每个服务的信息(ip、port...)

eureka的restful-api

api名称HTTP地址描述
注册实例POST /eureka/v2/apps/appID将指定appid的实例注册到eureka
注销实例DELETE /eureka/v2/apps/appID/instanceID将指定instanceId的实例在实例列表中剔除
发送心跳PUT /eureka/v2/apps/appID/instanceID发送实例心跳信息,如果返回404代表实例不存在
返回查询的实例信息,ip、端口健康状态等信息
查询所有的实例信息GET /eureka/v2/apps返回eureka服务器中的所有实例信息
查询指定的appid下的实例信息GET /eureka/v2/apps/appID/instanceID返回查询的实例信息,ip、端口健康状态等信息
查询某个实例的信息GET /eureka/v2/instances/instanceID
停止实例PUT /eureka/v2/apps/appID/instanceID/status?value=OUT_OF_SERVICE修改实例的状态为下线的服务
删除覆盖实例信息GET /eureka/v2/vips/vipAddress
查询指定安全的vip下的所有实例GET /eureka/v2/svips/svipAddress
查询指定appid下的所有实例GET /eureka/v2/apps/appID

eureka依赖

父工程pom

<properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.cloud-version>2021.0.0</spring.cloud-version>
        <spring.boot-version>2.6.3</spring.boot-version>
        <spring.cloud-alibaba.version>2.2.7.RELEASE</spring.cloud-alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

eureka-server

在较高版本的eureka依赖中server和client已经分开,在server端只需引入server依赖即可

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

eureka-client

引入client端的eureka依赖即可

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

服务端配置

在pom中引入eureka-server的依赖

启动类

需要在启动类标注@EnableEurekaServer开启eureka服务端

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

/**
 * @author Wangkangsheng
 */
@SpringBootApplication
@EnableEurekaServer
public class YjEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(YjEurekaServerApplication.class,args);
    }
}

application.yml配置文件

server:
 port: 10001
eureka:
  client:
    # 是否向eureka注册  默认true 单点服务端需要设置为false
    register-with-eureka: false
    # 是否拉取注册的客户端信息 默认true 单点服务端需要设置为false
    fetch-registry: false
  instance:
    # 主机地址 (ip)
    hostname: localhost
    # 实例名称
    appname: EurekaServer
  server:
    # 是否启用自我保护
    enable-self-preservation: false

客户端配置

在pom中引入eureka-client的依赖

启动类

需要在启动类标注@EnableDiscoveryClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author WangKangSheng
 */
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentApplication.class, args);
    }
}

配置文件

spring:
  application:
    name: PaymentService
server:
  port: 9001
  
eureka:
  client:
    service-url:
      # eureka服务端地址
      defaultZone: http://127.0.0.1:10001/eureka
    # 获得注册的服务
    fetch-registry: true
    # 注册到eureka
    register-with-eureka: true
  instance:
    # 服务名称
    appname: ${spring.application.name}

启动测试

注:需要先启动服务端,否则启动客户端会报错。原因很简单,既然要想eureka注册中心注册服务,那么肯定注册中心肯定要先存在才能注册

访问eureka注册中心地址即可看到eureka注册中心的控制台http://localhost:10001/

启动后的控制台中会看到客户端信息,status代表现在状态,如果丢失心跳或客户端下线状态会成为down

eureka+openFeign负载均衡

consumer服务调用端

创建consumer服务调用端,同样需要注册到eureka

注册配置和依赖同上

pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author WangKangSheng
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 开启feign客户端功能
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

feign接口编写

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author WangKangSheng
 */
@FeignClient(name = "PaymentService") // 服务名称
public interface UnifiedOrderService {

    /**
     * feign接口调用
     * @param username String 用户名
     * @return String
     */
    @PostMapping("/unifiedOrderService") // 服务端的调用地址
    String unifiedOrder(@RequestParam("username") String username);

}

对外暴露controller编写

import com.yunju.consumer.feign.UnifiedOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangKangSheng
 */
@RestController
public class UnifiedOrderController {

    @Autowired
    private UnifiedOrderService unifiedOrderService;

    @GetMapping("/unifiedOrder")
    public String unifiedOrder(String username){

        return unifiedOrderService.unifiedOrder(username);
    }


}

启动个应用程序

idea启动多服务端技巧

点击Edit Configurations

点击左上角的复制小图标,复制启动配置

复制启动实例后修改实例名称,并且在Override Parameter中修改启动端口号即可

在底部services中可以看到实例

  • 没有services功能 在顶部View->Tool Windows -> Services开启services
  • 打开services后看不到springboot实例:

在列表中点击springboot即可,如果找不到不要忽略底部有more items 如果还没有,在顶部启动项目后就能出现

编写controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangKangSheng
 */
@RestController
public class UnifiedOrderController {

    @Value("${server.port}")
    private Integer port;


    @PostMapping("/unifiedOrderService")
    public String unifiedOrder(String username){
        return "unified ["+port+"] -> "+username;
    }

}

启动服务提供方

调用测试

可以看到接口调用被负载到两个不同的客户端中