In Spring Cloud(04) — Introduction and deployment of Eureka, we used Eureka as our microservice registry, and now we use Zookeeper instead of Eureka. See Spring Boot – Simple use of Dubbo and Zookeeper.

1. Register the payment service in ZooKeeper

1. Create the cloud-provide- Payment8004 module

2. Import POM dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
           <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.cheng.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>
Copy the code

3. Write yML configuration files

server:
  port: 8004

spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
    #192.168.96.1 IP address of the local server 2181Zookeeper port number
      connect-string: 192.168111.144.: 2181
Copy the code

4, the main boot class

@SpringBootApplication
// @enableDiscoveryClient Suitable for Zookeeper or Consul as the registry
// The @enableDiscoveryClient annotation enables the registry to discover and scan the service.
@EnableDiscoveryClient
public class PaymentMain8004 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); }}Copy the code

5. Write controller

@RestController
@Slf4j
public class PaymentController {

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

    @RequestMapping(value = "/payment/zk")
    public String paymentZk(a){
        return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString(); }}Copy the code

6, test,

  1. Start the ZooKeeper server
  2. Start the ZooKeeper client
  3. Start the cloud-provide- Payment8004 module

On the ZooKeeper client, test whether the service is registered with the ZooKeeper server. Run the ‘ ‘ls/command to check whether a services node exists. Run the’ ‘ls /services’ command to check the information about the Services node:

Registration successful!

Access request http://localhost:8004/payment/zk:

Successful visit!

2. Register the order service in ZooKeeper

Create the module cloud-consumerzk-order80

2. Import POM dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.cheng.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>



    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>
Copy the code

3. Write yML configuration files

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      #192.168.96.1 IP address of the local server 2181Zookeeper port number
      connect-string: 192.16896.1.: 2181
Copy the code

4, the main boot class

@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
    public static void main(String[] args) { SpringApplication.run(OrderZkMain80.class,args); }}Copy the code

5. Configure the restTemplate function

Copy the config directory from the cloud-consumer-Order80 module to cloud-consumerzk-order80

6. Write controller

@RestController
@Slf4j
public class OrderZkController {

    // Extract the remote address prefix
    public static final String REST1_URL_PREFIX = "http://cloud-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/zk")
    public String paymentZk(a){
        String result = restTemplate.getForObject(REST1_URL_PREFIX + "/payment/zk", String.class);
        returnresult; }}Copy the code

7, test,

  1. Start the ZooKeeper server
  2. Start the ZooKeeper client
  3. Start the cloud-provide- Payment8004 module
  4. Start the cloud-Consumerzk-Order80 module

On the ZooKeeper client, test whether the service is registered with the ZooKeeper server. Run the ‘ ‘ls/command to check whether a services node exists. Run the’ ‘ls /services’ command to check the information about the Services node:

You can view that both services have been registered with ZooKeeper. To view specific information about a service, perform the following operations:

Take the cloud-provide- Payment8004 service as an example:

The json string in the red box is adjusted using the JSON tool to facilitate the display of service information:

Access request; http://localhost/consumer/payment/zk

Successful visit!