SpringCloud the single-node Eureka registration center set up with the practical application

EUREKA works:

        Due to business needs, during the recent remodeling project, for future high system availability, stability, development and reconstruction based on the final selection of micro-services architecture system, this use of " service registration and discovery --Eureka ."

        About Eureka : Eureka is based on REST (Representational State Transfer) services, mainly in the AWS cloud services as a support service to discover and load balancing and failover. We call this service for the Eureka service.

       Eureka core components: Eureka Server Eureka Client

       Eureka Server : Eureka Server service registration function, after each service node starts, it will be to Eureka Server service registration, Eureka Server stored information of each service node and use the " heartbeat " mechanism of service monitoring, maintaining Eureka Client services in a timely update . It should be noted that: Eureka Server single node is not required to register themselves to Eureka registry, because it is itself a service. Itself is already a registration center, the current service is currently registered to act as a registry service, this logic is illogical. (Except Enreka Server Cluster)

       Eureka Client: Eureka Client is an application to interact with the Eureka Server, all incoming registration services are able to visually see where in Eureka Client, when after the Eureka Client serving node starts, it will always be serving node and Eureka Server life state "report" to work, the frequency of every 30 seconds to send Eureka "heartbeat" command tells it: "I was in a healthy state" , if Ereka Server does not receive Eureka Client sent within 90 seconds "heartbeat" command, the node will serve as a "dead state" be removed. Enreka Client Services included in two roles: namely: Application Server (service provider) and Application Client (customer service), the service is no fixed role, it may be a service to consumers, it could be a service provider. Meanwhile, Eureka Client itself has a " load balancer ", consumers only need to tell Eureka Client What services do I need it, regardless of a service node pressure is too large.

      Eureka Client in Application Client call mechanism when the Application Server:

               Once the service is registered to the  Eureka Server registry , Eureka Client will be pulled from the registry an updated "list of services" is stored in the local cache , so the Application Client when each service call, will first check their " local cache " whether there is the list of services, and if not, it will pull the list of services to Eureka Server, the benefits of this mechanism is that not every service call will visit Eureka Server, greatly reducing the pressure control. But single-node Eureka registry there will be a drawback, is that when Eureka Server because after some factors downtime, Application Client was able to properly access the service, but the question is, if you register a new service to Eureka Server what happens ? The answer was: Registration Failed! So set up Eureka clusters is very necessary. (Explained next issue to build a two-node cluster Eureka and works)

   Eureka principle Chart:

  

    When a service registers with the Eureka Server, Eureka Client service will replicate each other in the service and address information, to achieve consistency of results, so when Eureka Server downtime, calls between the service will not be any problems.

Single-node Eureka Server build:

     Eureka Server build:

File——new——Project

 

 

Next we need to pay attention to that, because we want to build Eureka Server (registry), so that the following options to choose on.

The next direct Finish, and you can visually see:

找到启动类,在启动类加上 @EnableEurekaServer (表明该模块为Eureka注册中心)

Eureka Server application.properties配置:

现在我们就可以启动项目进行验证该配置是否正确,在浏览器输入:服务器地址+端口号。

以上可以看到Eureka Server搭建成功!

Eureka Client搭建:

接下来我们进行Eureka Client搭建,将它作为服务注册进去,和Eureka Server搭建基本一致。由于Eureka是我们这次重构用到的技术,所以以下的Eureka Client就用我现在已有的模块,你们可以自己搭建模块进行测试。下面我将把"smarthospital-basicdata"与"smarthospital-treatment"分别作为Application Server和Application Client 注册到我们的控制中心。

找到Eureka Client启动类(smarthospital-basicdata):

在启动类上加上@EnableDiscoveryClient,表明它是一个Eureka Client。也可以加@EnableEurekaClient,他们的作用都是一样的,唯一的区别就是:pringCloud中的“Discovery Service”有多种实现(@EnableDiscoveryClient),比如:eureka, consul, zookeeper。而@EnableEurekaClient只能为eureka作用;如果你的注册中心确定是Eureka的话,建议使用@EnableEurekaClient, 比较单一

Eureka Client application.properties配置:

下面我们启动该Eureka Client试试:

在注册中心可以清晰的看到,basicdata这条实例以及注册到我们的注册中心里了。

我们再将"smarthospital-treatment"注册进来(和上面步骤一样,只需要改下端口号和服务名,其他不变)。

可以看到第二个服务已经注册进来了,接下来我们进行服务之间的调用:

我们现在要用TREATMENT调用BASICDATA,找到服务提供者提供的Controller。

比如我们要调用该接口,现在我们在smarthospital-treatment中的接口(interface)里将地址配置好。

在上图中我们可以看到@FeignClient注解,这个注解是服务调用时所需要的注解,要调用哪个服务的接口就写哪个服务名。

 

现在我们可以在实现类里将FeignOperativeService注入进来,然后进行测试。(具体实现细节不写了)

然后进行smarthospital-treatment实现类对应的Controller调用,试试有没有服务调用成功,如果数据存在,则成功。

  可以看到数据已经成功获取,Eureka Client 服务之间调用成功!

  以上就是Eureka Server 与 Eureka Client 之间的关系,下期发布Eureka集群搭建以及服务调用。谢谢大家阅读!

发布了3 篇原创文章 · 获赞 0 · 访问量 147

Guess you like

Origin blog.csdn.net/weixin_41950725/article/details/103911169