In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor of this article gives you a detailed introduction to "practical example Analysis of Service Registration and Discovery of Micro Services". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "practical example Analysis of Service Registration and Discovery of Micro Services" can help you solve your doubts.
1 service registry
Earlier, we introduced several common registries in the industry: Eureka, Zookeeper, Consul, Etcd.
And made a comparison in various indicators: registration method (watch\ polling), health check, avalanche protection, security and permissions, and the degree of support on Spring Cloud, Dubbo, Kubernets. It is convenient for us to make the correct technology selection in different scenarios.
Four registry technical comparison indicators EurekaZookeeperConsulEtcd conformance protocol APCP (Paxos algorithm) CP (Raft algorithm) CP (Raft algorithm) health check TTL (Time To Live) TCP Keep AliveTTL\ HTTP\ TCP\ ScriptLease TTL KeepAlivewatch/long polling does not support watchlong pollingwatch avalanche protection does not support security and permissions does not support ACLACLRBAC whether it supports multiple data centers whether there is a management interface (third-party ZkTools is available) No Spring Cloud integration supports Dubbo integration does not support K8S integration does not
We can see that all four technology types have high support for Spring Cloud. Spring Cloud is an one-stop solution for micro-service architecture. We usually need to do some operations in the process of building micro-service, such as configuration management, service discovery, load balancing, circuit breaker, intelligent routing, control bus, global lock, decision campaign, distributed session and cluster state management. Spring Cloud provides us with a simple programming model that enables us to easily build micro-service projects on the basis of Spring Boot.
Spring Cloud includes a number of different open source products to ensure one-stop micro-service solutions, such as: Spring Cloud Config, Spring Cloud Netflix, Spring Cloud Security, Spring Cloud Commons, Spring Cloud Zookeeper, Spring Cloud CLI and other projects.
2 implementation under the framework of Spring Cloud
Spring Cloud provides a layer of abstraction for service governance, which can support a variety of different service governance frameworks, such as Netflix Eureka, Consul. Let's take these two as examples to see how service governance is implemented.
Under the action of the Spring Cloud service governance abstraction layer, the service governance implementation can be switched seamlessly without affecting any other service registration, discovery, and invocation logic.
So, let's experience the benefits of the Spring Cloud layer of abstraction by introducing the implementation of these two types of service governance.
2.1 Spring Cloud Eureka
Spring Cloud Eureka is a service governance module under the Spring Cloud Netflix project. The Spring Cloud Netflix project is one of the sub-projects of Spring Cloud, which mainly focuses on the packaging of a series of open source products of Netflix, which provides self-configured Netflix OSS integration for Spring Boot applications.
With some simple annotations, developers can quickly configure common modules in applications and build huge distributed systems. Its main modules include: service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent routing (Zuul), client load balancing (Ribbon) and so on.
Next, let's take a specific look at how to implement service governance using Spring Cloud Eureka.
2.1.1 create a registry
Create a Spring Cloud project, which we call micro-service-center, and introduce the required dependencies in pom.xml:
Pom
Indicates that there can be no Java code or any code execution in this project, just to aggregate the project or pass dependencies, so you can delete the src folder. This is a parent project, because we also need to set up Eureka's registry, client, and other subprojects below.
Under micro-service-center, create a new Module named eureka-service, which is still the Spring Cloud project. After the project is completed, pom.xml makes the following changes:
Com.microservice center 1.0.0 org.springframework.cloud spring-cloud-netflix-eureka-server
After the modification, go back to the parent project micro-service-center and modify the information in pom:
Com.microservice center pom 1.0.0 center Demo project for Spring Boot eureka-service eureka-client
Clean + install for two projects should be successful.
Eureka-service is used as a registry, so add the @ EnableEurekaServer annotation to its main class Application to turn on the registry function.
@ SpringBootApplication @ EnableEurekaServer public class ServiceApplication {public static void main (String [] args) {SpringApplication.run (ServiceApplication.class, args); System.out.println ("Start Eureka Service");}
But by default, the registry also treats itself as a client, so it registers itself. This can be deleted. Let's take a look at the detailed configuration in its YAML. The comments are relatively clear:
Server: port: 1000 spring: application: name: eureka-server eureka: instance: hostname: localhost client: register-with-eureka: false # do not register as a client fetch-registry: false # do not get the registration list service-url: # registered address, the client needs to register to this address defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
The notes in the article are relatively clear. As you can see here, the port number is 1000, so when the project starts, you can see the Eureka registry page when you visit http://localhost:1000/. No services have been found yet.
2.1.2 create a client
At present, the service center is still empty, so we create a client that can provide the service and register it with the registry.
Similarly, we create a subproject of Spring Cloud, named eureka-client,pom.xml, and the configuration is as follows:
Com.microservice center 1.0.0 org.springframework.cloud spring-cloud-netflix-eureka-server org.projectlombok lombok
By adding the @ EnableDiscoveryClient annotation to the application main class Application file, this annotation ensures that the current service is discovered by Eureka as provider.
@ SpringBootApplication @ EnableDiscoveryClient public class ClientApplication {public static void main (String [] args) {SpringApplication.run (ClientApplication.class, args); System.out.println ("start client!");}
Add the following configuration to the YAML file:
Server: port: 1001 spring: application: name: eureka-client eureka: client: service-url: # this is guaranteed to register with the eureka-service registry defaultZone: http://localhost:1000/eureka
The spring.application.name attribute, which specifies the name of the microservice that can be accessed when invoked. The eureka.client.serviceUrl.defaultZone attribute specifies the location of the service registry, corresponding to the configuration content of the service registry.
As you can see, the port here is set to 1001, because the service provider and service registry need to be tested on the local machine, so the port property of server needs to be set to a different port.
Finally, we write another interface to get all the service information of the registry in the client through the DiscoveryClient object.
@ Controller @ RequestMapping ("/ eurekacenter") public class EuServiceController {@ Autowired DiscoveryClient discoveryClient; / * get registration service information * / @ RequestMapping (value = "/ service", method = {RequestMethod.GET}) @ ResponseBody public String getServiceInfo () {return "service:" + discoveryClient.getServices () + ", memo:" + discoveryClient.description ();}
At this time, run for a try, continue to visit the previous address: http://localhost:1000/, and you can see that the Eureka registry page already contains a service we defined, which is the newly created port 1001 service above.
Similarly, we can call the above interface to obtain registration service information to see how many services are registered with the registry from the point of view of service discovery. Http://localhost:1001/eurekacenter/service
As shown in the figure above, eureka-client in square brackets gets a list of all the services obtained in the implementation of eureka through the getServiceInfo interface defined by Spring Cloud. He is a List of String, and if multiple providers are registered, all of them will be displayed.
2.2 Spring Cloud Consul
Consul is used to realize service discovery and configuration in distributed systems. Like other distributed service registration and discovery solutions, Consul's solution is more "one-stop", with built-in service registration and discovery framework, distributed conformance protocol implementation, health check, Key/Value storage, multi-data center solution, and no longer need to rely on other tools (such as ZooKeeper).
Spring Cloud Consul, on the other hand, is a tool that provides service discovery and service configuration for our infrastructure in the micro-service architecture as a whole.
2.2.1 benefits of Consul
1. Using Raft algorithm to ensure consistency is more direct than complex Paxos algorithm.
2. Multiple data centers are supported, and different ports are used to monitor the services of internal and external networks. Multi-data center cluster can avoid a single point of failure of a single data center, while its deployment needs to consider network delay, fragmentation and so on. Neither zookeeper nor etcd provides support for multiple data center capabilities, as shown in the table above.
3. Support health examination.
4. Supports http and dns protocol interfaces. The integration of zookeeper is complex, and etcd only supports http protocol.
5. The official web management interface is provided, but etcd does not have this function.
2.2.2 Features of Consul
1. Service discovery
2. Health examination
3. Key/Value storage
4. Multiple data centers
2.2.3 install the Consul registry
1. Official download version 64: https://www.consul.io/downloads.html
2. Unzip and copy it to the directory / usr/local/bin
3. Start the terminal and see what version it is.
Wengzhihua@B000000147796DS ~% consul-- version Consul v1.10.4 Revision 7bbad6fe Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol > 2 when speaking to compatible agents)
4. Execute the installation command and you can see that the port of his Client Addr is 8500. So visit the port 8500 site, http://127.0.0.1:8500/ui/dc1/services
Wengzhihua@B000000147796DS ~% consul agent-dev = = > Starting Consul agent... Version: '1.10.4' Node ID:' 6db154b4-62ff-e67d-e745-1a7270fa1ce8' Node name: 'B000000147796DS' Datacenter:' dc1' (Segment:'') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS:-1, gRPC: 8502, DNS: 8600) Cluster Addr: 127.0.0.1 (LAN: 8301) WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
As we can see, there is no client registration, only one instance of itself.
2.2.4 create a service provider
Due to the implementation of the Spring Cloud Consul project, we can easily register Spring Boot-based micro-service applications with Consul, through which we can achieve service governance in the micro-service architecture.
We create a new cloud project consul-client under micro-service-center, and the project pom file is added as follows:
Com.microservice center 1.0.0 org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.boot spring-boot-starter-actuator
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.