Spring Boot 2.2 integrates Spring Cloud Zookeeper - Ribbon distributed services consumers

1 Summary

Spring Cloud Zookeeper distributed service registry building may refer to:

33 Spring Boot 2.2 integrates Spring Cloud Zookeeper - distributed service registry --2020-02-23

This article will introduce Spring Cloud Zookeeper distributed service consumers - Ribbon

2 core Maven relies

./cloud-zookeeper-ribbon/pom.xml
        <!-- Spring mvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring cloud zookeeper -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-all</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Which ${zookeeper.version}version is 3.4.12( not arbitrarily change the version number, there will be compatibility issues )

Note: SpringBoot version 2.2 and above in need

3 Profiles

3.1 bootstrap.yml

./cloud-zookeeper-ribbon/src/main/resources/bootstrap.yml
## Application bootstrap config


## spring config
spring:
  cloud:
    zookeeper:
      connect-string: 172.16.140.10:2181

3.2 application.yml

./cloud-zookeeper-ribbon/src/main/resources/application.yml
## Application config

## Server
server:
  port: 8101

## Spring config
spring:
  application:
    name: cloud-zookeeper-ribbon

4 core Java classes

4.1 Service Layer Service call

./cloud-zookeeper-ribbon/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/ribbon/service/CloudZookeeperRibbonService.java
package com.ljq.demo.springboot.cloud.zookeeper.ribbon.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: Spring Cloud Zookeeper Ribbon 服务消费者业务层
 * @Author: junqiang.lu
 * @Date: 2020/2/24
 */
@Service("cloudZookeeperRibbonService")
public class CloudZookeeperRibbonService {

    /**
     * Zookeeper 服务注册中心服务名称
     */
    private static final String CLOUD_ZOOKEEPER_PROVIDER_NAME = "cloud-zookeeper-provider";
    /**
     * Zookeeper 服务注册中心接口地址-打印用户名称
     */
    private static final String API_PATH_ZOOKEEPER_HELLO = "/api/cloud/zookeeper/hello";


    @Resource
    private RestTemplate restTemplate;

    /**
     * 打印用户名称
     *
     * @param name
     * @return
     */
    public String sayHello(String name) {
        StringBuilder reqUrl = new StringBuilder("http://");
        reqUrl.append(CLOUD_ZOOKEEPER_PROVIDER_NAME);
        reqUrl.append(API_PATH_ZOOKEEPER_HELLO);
        reqUrl.append("?name=").append(name);

        System.out.println(new Date() + "-" + reqUrl.toString());

        return restTemplate.getForEntity(reqUrl.toString(), String.class).getBody();
    }


}

Use RestTemplateto request calling Zookeeperservice, where Ribbonservice name (ServiceId) instead of ip + port (port), RestTemplatethe default is a normal httprequest, where the need for RestTemplatethe use of Ribbonannotations provided @LoadBalanced, see the specific use SpringBootstartup classcom.ljq.demo.springboot.cloud.zookeeper.ribbon.CloudZookeeperRibbonApplication

On Ribbonthe @LoadBalancednotes, whose role is to implement the client load balancing, as described in the following sections:

@LoadBalanced understood by the use of annotations springcloud ribbon

4.2 Controller layer

./cloud-zookeeper-ribbon/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/ribbon/controller/CloudZookeeperRibbonController.java
package com.ljq.demo.springboot.cloud.zookeeper.ribbon.controller;

import com.ljq.demo.springboot.cloud.zookeeper.ribbon.service.CloudZookeeperRibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: Spring cloud Zookeeper Ribbon 服务消费者控制层
 * @Author: junqiang.lu
 * @Date: 2020/2/24
 */
@RestController
@RequestMapping("/api/cloud/zookeeper/ribbon")
public class CloudZookeeperRibbonController {

    @Autowired
    private CloudZookeeperRibbonService cloudZookeeperRibbonService;


    /**
     * 打印用户名称
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/sayHello", method = {RequestMethod.GET, RequestMethod.POST},
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public String sayHello(@RequestParam("name") String name) {
        return cloudZookeeperRibbonService.sayHello(name);
    }


}

4.3 SpringBoot startup class

./cloud-zookeeper-ribbon/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/ribbon/CloudZookeeperRibbonApplication.java
package com.ljq.demo.springboot.cloud.zookeeper.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author junqiang.lu
 */
@EnableDiscoveryClient
@SpringBootApplication
public class CloudZookeeperRibbonApplication {

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

    @LoadBalanced
    @Bean
    RestTemplate restTemplate(){
        return new RestTemplate();
    }


}

@EnableDiscoveryClientThis annotation can be used to discover Cloudservices

@LoadBalancedFor load balancing, this must be manually injected RestTemplate, and the use of @LoadBalancedannotations, otherwise, the RestTemplatedefault normal httprequest

5 Test

Since this test need to call Spring Cloud Zookeeperservices, project services registry ( cloud-zookeeper-provider) must be started

Interface requests:

GET http://127.0.0.1:8101/api/cloud/zookeeper/ribbon/sayHello?name=Are%20you%20%E6%AC%A7%E5%85%8B

Return result:

Hello !Are you 欧克
server port : 8100
server timestamp: 1582599140197

As can be seen from the results returned, the port is returned to service registry items ( cloud-zoopeeper-provider) http port, use the Ribboncall Cloud Zoopeeperservice success

6 Recommended References

Official documentation Spring Cloud Zookeeper

Zookeeper complete series of tutorials Spring-Cloud-Zookeeper-Based-Demo

@LoadBalanced understood by the use of annotations springcloud ribbon

7 Github source

Gtihub Source Address : https://github.com/Flying9001/springBootDemo

Personal Public Number: 404Code , Internet technology and think half the share of people interested can pay attention.
404Code

Published 61 original articles · won praise 65 · Views 200,000 +

Guess you like

Origin blog.csdn.net/Mrqiang9001/article/details/104495167