SpringCloud专题之开篇及Eureka

Spring Cloud简介
Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具,它为微服务中设计的配置管理、服务治理、智能路由、微代理、控制总线、决策竞选等提供了一种简单的开发方式。

Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要服务完成微服务架构中的服务治理功能,Spring Cloud通过为Eureka增加了Spring Boot风格的自动化配置,我们只需要简单的引入依赖和注解配置就能让Spring Boot构建微服务应用轻松地与Eureka服务治理体系进行整合。

Eureka服务端,也称服务注册中心。同其他的服务注册中心一样,支持高可用配置,它依托于强一致性提供良好的服务实例可用性,可以应对多种不同的故障场景。如果Eureka以集群模式部署,当集群中有分片出现故障时,那么Eureka就转入自我保护模式。它允许在分片出现故障起间继续提功服务的发现和注册,当故障分片恢复运行时,集群中的其他分片会把他们的状态再此同步回来。

Eureka客户端,主要处理服务的注册与发现,客户端服务通过注解和参数配置的方式,嵌入在客户端应用程序的代码中,在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务,并周期性的发送心跳来更新它的服务续约。同时,它也能从服务端查询当前注册的服务信息并把他们缓存到本地并周期性得刷新服务状态。

代码实践
服务端
创建一个基础的Spring Boot并引入依赖:

org.springframework.boot spring-boot-starter-parent 2.3.10.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-server 启动类,通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行注册。

/**

  • @className: EurekaServerApplication

  • @description: eureka服务注册中心启动类

  • @author: charon

  • @create: 2021-05-17 21:55
    */
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

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

}
配置,服务注册中心也会将自己作为客户来尝试注册自己,所以要禁用他的客户端注册行为,配置内容如下:

server.port=9001

eureka.instance.hostname=localhost

代表不向注册中心注册自己

eureka.client.register-with-eureka=false

由于注册中心的职责就是维护服务实例,并不需要去检索服务,所以也设置为false

eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
启动之后在浏览器访问如下图所示,在instances currently registered with eureka这一栏是空的,说明还没有注册任何服务。

客户端
引入eureka客户端的依赖:

org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web 客户端启动类,通过@EnableDiscoveryClient注解,激活Eureka中的EnableDiscoveryClient实现。

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

}
controller类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • @className: HelloController

  • @description: 测试controller类

  • @author: charon

  • @create: 2021-05-17 22:46
    */
    @RestController
    public class HelloController {

    /**

    • 日志记录类
      */
      private final Logger logger = LoggerFactory.getLogger(getClass());

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

    @Value("${spring.application.name}")
    private String instanceName;

    @RequestMapping("/sayHello")
    public String sayHello(){
    logger.info(“你好,服务名:{},端口为:{}”,instanceName,host);
    return “你好,服务名:”+instanceName+",端口为:"+host;
    }
    }
    配置:

server.port=9002

spring.application.name=hello-server

eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/
启动之后重新访问eureka的信息面板,在instances currently registered with eureka这一栏可以看到我们hello-server的服务注册上了。

在服务注册中心的控制台中,可以看到这样的输出,表示名为hello-service的服务被注册成功了。

2021-05-17 23:10:48.673 INFO 3156 — [nio-9001-exec-5] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-SERVER/DESKTOP-4LG5AMO:hello-server:9002 with status UP (replication=false)
通过浏览器访问http://localhost:9002/sayHello,直接向该服务发起请求,在控制台中可以看到如下的输出信息:

2021-05-17 23:15:24.216 INFO 3820 — [nio-9002-exec-1] c.c.e.controller.HelloController : 你好,服务名:hello-server,端口为:9002
配置高可用的注册中心
在微服务架构这样的分布式环境中,需要充分考虑发生故障的情况,所以在生产环境中,必须对各个组件进行高可用的部署,对于注册中心也是一样的。在Eureka的服务治理设计中,所有节点既是服务提供方,也是服务消费方。上面在单节点的配置中,配置过如下两个参数,让服务注册中心不注册自己:

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Eureka服务的高可用其实就是将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组互相注册的服务注册中心,以实现服务清单的相互同步,达到高可用的效果。

下面我们来构建一个双节点的服务注册中心集群:

1.创建application-server1.properties作为EurekaServer1服务中心的配置,并将serviceUrl指向EurekaServer2:

server.port=9001

spring.application.name=eureka-server
eureka.instance.hostname=eureka-server1
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://eureka-server2:9002/eureka/
2.创建application-server2.properties作为EurekaServer2服务中心的配置,并将serviceUrl指向EurekaServer1:

server.port=9002

spring.application.name=eureka-server
eureka.instance.hostname=eureka-server2
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://eureka-server1:9001/eureka/
3.在配置文件中配置路径的转换,由于我这是windows上,所有我这的路径为:C:\Windows\System32\drivers\etc\hosts.(linux上的路径为:/etc/hosts)

127.0.0.1 eureka-server1
127.0.0.1 eureka-server2
4.项目打包,通过spring.profiles.active属性来分别启动eureka-server1和eureka-server2.

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=server1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2
如下图所示,在9001的server1上可以看到DS Replicas为eureka-server2的集群高可用服务,同理,在9002的server2上也可以看到eureka-server1的服务。
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值