Moment For Technology

Spring Cloud uses ZooKeeper as the service registry and configuration center

Posted on Dec. 3, 2022, 9:47 a.m. by Adam Shields
Category: The back-end Tag: The back-end Spring Cloud

Now it is quite popular to use Spring Cloud framework to build microservices. After all, this is a framework provided by Spring, which can be used well with Spring Boot. It will soon be possible to build a microservice, greatly reducing the workload. Here's how each microservice calls each other. This requires a service with service registry and discovery capabilities, and all microservices are registered with this service, so that all microservices can be called by customers to each other. There are two forms of service registration and discovery: one is client discovery (EUREKA), which is more popular. The other is server discovery (ZooKeeper or Consul). This section focuses on using ZooKeeper as the service registry and configuration center.

1. Service Registry

1. Install the zookeeper

Unpack the zookeeper:

The tar - XVF zookeeper - 3.4.10. Tar. GzCopy the code

Start the zookeeper:

CD zookeeper-3.4.10 CD conf cp zoo_sample.cfg zoo. CFG CD.. /bin sh zkServer.sh startCopy the code

2. Introduce dependencies

dependency
   groupIdorg.springframework.cloud/groupId
   artifactIdspring-cloud-starter-zookeeper-discovery/artifactId
/dependency
Copy the code

3. Create microservices and use ZooKeeper as the service registry.

package com.garlic.springcloudzookeeperclientapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * ZooKeeper as service registry, Application Startup class * @Author llSydn */ @SpringBootApplication @EnableDiscoveryClient public class SpringCloudZookeeperClientAppApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudZookeeperClientAppApplication.class, args); }}Copy the code

The corresponding application. The properties

Name =spring-cloud-zookeeper-client-app ## Configure service port server.port=8080 ## Disable security control Management. Security. Enabled = false # # configuration zookeeper address spring. Cloud. Zookeeper. Connect - string = localhost: 2181Copy the code

Use the DiscoveryClient to obtain the registration service list

package com.garlic.springcloudzookeeperclientapp.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; /** * provide Rest Api, * * @author llsydn * @create 2018-5-11 20:47 */ @RestController @RequestMapping("/zookeeper") public * * @author llsydn * @create 2018-5-11 20:47 */ @RestController @RequestMapping("/zookeeper") public class ZookeeperController { @Value("${spring.application.name}") private String instanceName; private final DiscoveryClient discoveryClient; @Autowired public ZookeeperController(DiscoveryClient discoveryClient) { this.discoveryClient = discoveryClient; } @GetMapping public String hello() { return "Hello,Zookeeper."; } @GetMapping("/services") public ListString serviceUrl() { ListServiceInstance list = discoveryClient.getInstances(instanceName); ListString services = new ArrayList(); if (list ! = null  list.size()  0 ) { list.forEach(serviceInstance - { services.add(serviceInstance.getUri().toString()); }); } return services; }}Copy the code

Note: You can start different instances. Here I start two instances of ports 8080 and 8081 and use the endpoints to query the list of registered services. You can also run zooKeeper commands to query the list of registered services

sh zkCli.sh
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[spring-cloud-zookeeper-client-app]
[zk: localhost:2181(CONNECTED) 3] ls /services/spring-cloud-zookeeper-client-app
[be61af3d-ffc2-4ffc-932c-26bc0f94971c, bcf21ece-e9e1-4a91-b985-8828688370b8]
[zk: localhost:2181(CONNECTED) 4]
Copy the code

2. Configuration center

1. Use the zkCli to create configuration information

[zk: localhost:2181(CONNECTED) 27] create /config ""
Created /config
[zk: localhost:2181(CONNECTED) 28] create /config ""
Created /config/garlic
[zk: localhost:2181(CONNECTED) 29] create /config/garlic/name "default"
Created /config/garlic/name
[zk: localhost:2181(CONNECTED) 30] set /config/garlic-dev/name "dev"
Node does not exist: /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 31] create /config/garlic-dev/name "dev"
Created /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 32] create /config/garlic-test/name "test"
Created /config/garlic-test/name
[zk: localhost:2181(CONNECTED) 33] create /config/garlic-prod/name "prod"
Copy the code

2. Use the Controller to dynamically obtain the ZooKeeper configuration center data

2.1 the maven rely on

dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-zookeeper-config/artifactId  /dependencyCopy the code

2.2 the bootstrap. The properties

# # enable zookeeper as configuration center spring. Cloud. Zookeeper. Config. Enabled = true # # configuration root spring. Cloud. Zookeeper. Config. The root = config # # to configure default context spring. Cloud. Zookeeper. Config. DefaultContext = garlic # # configuration profile separator spring.cloud.zookeeper.config.profileSeparator = -Copy the code

Spring. Cloud. Zookeeper. Config. Root corresponding zkCli create config directory, defaultContext corresponding to create garlic or garlic - * directory, Determine whether to get dev or Test or PROd configuration based on profile

2.3 application. The properties

Name =spring-cloud-zookeeper-config-app ## Configure service port server.port=10000 ## Disable security control management.security.enabled=false spring.profiles.active=devCopy the code

2.4 Using the Controller to dynamically Obtain ZooKeeper configuration Center Data

package com.garlic.springcloudzookeeperconfigapp.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * provide Rest Api, * * @author llsydn * @create 2018-5-11 20:50 */ @restController @requestMapping ("/zookeeper") * * @author llsydn * @create 2018-5-11 20:50 */ @restController @RequestMapping("/zookeeper") Public class ZookeeperController {@autowired private Environment Environment; public class ZookeeperController {@autowired private Environment Environment; @Value("${name}") private String name; @GetMapping public String hello() { return "Hello, " + name; } @GetMapping("/env") public String test() { String name = environment.getProperty("name"); System.out.println(name); return "Hello," + name; }}Copy the code

After starting the configuration instance, you can change the value of name in garlic through zkCli, and then access the endpoint to see if the value has changed. At this point, using ZooKeeper as the service registry and configuration center is complete. We can configure dynamic routing and database connection information for the service provider by using ZooKeeper as the configuration center and Zuul as the API gateway.

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.