Spring Cloud Eureka Server集群和客户端调用

目录

1、Eureka 简介

2、Eureka 集群搭建

1)、在start.spring.io中,添加Eureka Server、Web、Actuator模块

2)、在SpringBootApplication启动类中添加@EnableEurekaServer注解

3)、配置文件

4)、Spring Profiles启动服务

3、Eureka 客户端实例注册到Eureka Server

1)、在start.spring.io中,添加Eureka Discovery、Web、Actuator模块

2)、在启动类中添加@EnableDiscoveryClient 或@EnableEurekaClient注解

3)、配置文件

4)、启动客户端后,在Eureka Server中查看是否注册成功


1、Eureka 简介

    Spring Cloud微服务的服务注册中心,可以使用Eureka、Zookeeper、Consul或者etcd,并且Spring Cloud对其都有较好的支持。之前的时候老是看很多文章,介绍某一个技术大概的情况就那样去理解,现在觉得实践非常的重要,不管怎么样都需要自己去实现一遍,理解才会更加深刻。所以先搭建Eureka Server集群,后面再去比较几个服务集群。

1、Eureka分为服务端和客户端,Eureka Server多个实例相互注册形成集群中心(服务节点注册到其他节点后,相对为为客户端)

2、服务调用的服务端和客户端都为Eureka的客户端,都需要注册到注册中心(如:价格系统调用商品系统的接口,相对于Eureka Server注册中心,价格系统和商品系统都是Eureka的客户端)

2、Eureka 集群搭建

    集群搭建的demo地址为:https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/eureka-server

1)、在start.spring.io中,添加Eureka Server、Web、Actuator模块

2)、在SpringBootApplication启动类中添加@EnableEurekaServer注解

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

/**
 *	Spring Cloud Eureka Server端,使用不同的Spring profile启动即可组成 Eureka Server 集群
 *
 * @author kevin
 * @date 2019/5/20 9:08
 * @since 1.0
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
	public static void main(String[] args) {

		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

3)、配置文件

    其实eureka相关的配置应该写到bootstrap.properties的配置文件中,但是为了方便直接将所有的配置都写到了application.properties中,若启动的是单节点Eureka Server,则只要进入Eureka Server的maven配置,在启动类上添加了@EnableEurekaServer注解即可。但是当组成集群的时候,服务本身需要将自己以Eureka Client的身份注册到其他的Eureka Server。如下:

# 应用名称
spring.application.name=eureka-register
# 服务的端口号,若在同一台服务器上配置伪集群,则注意端口不能一样
server.port=8761
# eureka的host
eureka.instance.hostname=localhost
# 该Eureka Server是否将自己注册到注册中心
eureka.client.registerWithEureka=true
# 是否从eureka上获取注册信息
eureka.client.fetchRegistry=true
# 将Eureka自身注册到哪台Eureka服务器上,多个直接使用逗号间隔
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8760/eureka/,\
  http://${eureka.instance.hostname}:8759/eureka/

若需要在单台机器上启动一个Eureka Server集群,只需要将刚才的配置文件copy三份,使用Spring Profiles进行启动,即可组成集群,比如profile名为如下:

其他两个eureka-2、eureka-3的profile的配置为:

spring.application.name=eureka-register
server.port=8760
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/,\
  http://${eureka.instance.hostname}:8759/eureka/
spring.application.name=eureka-register
server.port=8759
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/,\
  http://${eureka.instance.hostname}:8760/eureka/

4)、Spring Profiles启动服务

在idea中修改启动项的profile 和 将Single instance only的选项去除(否则不能启动第二个实例),如下:

修改一个启动一个,启动的过程中会相互查找、注册,可能会报com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server等错误,当三个实例都启动后则恢复正常,在浏览器中分别访问三台服务器,如:http://localhost:8759/,如下:

 

3、Eureka 客户端实例注册到Eureka Server

客户端的地址为:https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/eureka-client

1)、在start.spring.io中,添加Eureka Discovery、Web、Actuator模块

2)、在启动类中添加@EnableDiscoveryClient 或@EnableEurekaClient注解

    很显然@EnableEurekaClient注解只能作为Eureka Server的客户端,但是@EnableDiscoveryClient注解可以用于所以的注册中心的客户端。为了方便直接在启动了中添加@RestController注解和接口。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *  Eureka客户端的demo, 将自己注册到注册中心(Eureka-Server{@link EnableEurekaServer})后
 *  可以在注册中心,查看自己是否注册为一个服务
 *  在浏览器端访问 http://localhost:8762/ 访问自己是否启动OK
 */
@RestController
// @EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

	public static void main(String[] args) {

		SpringApplication.run(EurekaClientApplication.class, args);
	}

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

	@RequestMapping("/")
	public String home() {
		return "hello world from port " + port;
	}
}

3)、配置文件

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,\
  http://localhost:8760/eureka/,http://localhost:8759/eureka/
server.port=8762
spring.application.name=eureka-client

4)、启动客户端后,在Eureka Server中查看是否注册成功

    Eureka Client 服务只会注册到 eureka.client.serviceUrl.defaultZone 的第一台服务上,其他节点上并不会进行注册,但是当该台Eureka Server宕机时,则会注册到集群的下一个节点上(当前会注册到8760上),注册成功如下:

 

 

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值