Spring Cloud利用zookeeper爲服務註冊中心

Spring Cloud Eureka可以作爲微服務的服務註冊和發現中心。Spring Cloud還支持其他方式的註冊和發現中心比如Spring Cloud Zookeeper和Consul,etcd等

先完成 Zookeeper的Windows集羣搭建,接下來要做的是搭建基於Zookeeper集羣的微服務的註冊和調用。其中使用Feign進行負載均衡。

項目結構

外層pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springcloudzookeeper</groupId>
	<artifactId>springcloud-zookeeper</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud-zookeeper</name>
	<description>Demo project for Spring Boot</description>


	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<!-- 與服務器安裝的Zookeeper版本相同 -->
			<version>3.4.10</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 熱部署工具 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

	</dependencies>


	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

zookeeper-client-provider

pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springcloudzookeeper</groupId>
		<artifactId>springcloud-zookeeper</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springcloudzookeeper</groupId>
	<artifactId>zookeeper-client-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zookeeper-client-provider</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

接下來看下列文件

啓動類上加上註解@EnableDiscoveryClient

package com.springcloudzookeeper.zookeeperclientprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperClientProviderApplication {

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

}

User.java

package com.springcloudzookeeper.zookeeperclientprovider.model;

/**
 * @Author zhaomengxia
 * @create 2019/9/5 13:42
 */

public class User {

    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
UserController.java
package com.springcloudzookeeper.zookeeperclientprovider.controller;

import com.springcloudzookeeper.zookeeperclientprovider.model.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zhaomengxia
 * @create 2019/9/5 11:22
 */
@RestController
public class UserController {

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

    @GetMapping("user")
    public String getUserInfo(@RequestParam("id") Long id){

        User user=new User();
        user.setId(id);
        user.setName("userName"+id);
        return user.toString()+"port:"+port;
    }
}

application.properties

server.port=8000
spring.application.name=zookeeper-user-provider
spring.cloud.zookeeper.connect-string=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
spring.cloud.zookeeper.discovery.enabled=true
spring.cloud.zookeeper.discovery.register=true

demozookeeper-client-consumer

pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springcloudzookeeper</groupId>
		<artifactId>springcloud-zookeeper</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springcloudzookeeper</groupId>
	<artifactId>demozookeeper-client-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demozookeeper-client-consumer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

啓動類DemozookeeperClientConsumerApplication.java添加註解@EnableFeignClients @EnableDiscoveryClient

package com.springcloudzookeeper.demozookeeperclientconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemozookeeperClientConsumerApplication {

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

}
FeignFallBack.java
package com.springcloudzookeeper.demozookeeperclientconsumer.config;

import com.springcloudzookeeper.demozookeeperclientconsumer.service.UserService;
import org.springframework.stereotype.Component;

/**
 * @Author zhaomengxia
 * @create 2019/9/5 11:50
 */
@Component
public class FeignFallBack implements UserService{
    @Override
    public String getUserInfo(Long id) {
        return null;
    }
}

接口UserService.java

package com.springcloudzookeeper.demozookeeperclientconsumer.service;

import com.springcloudzookeeper.demozookeeperclientconsumer.config.FeignFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author zhaomengxia
 * @create 2019/9/5 11:34
 */
@FeignClient(value = "zookeeper-user-provider",fallback = FeignFallBack.class)
public interface UserService {

    @GetMapping("user")
    String getUserInfo(@RequestParam(value = "id") Long id);
}
UserConsumerController.java
package com.springcloudzookeeper.demozookeeperclientconsumer.controller;

import com.springcloudzookeeper.demozookeeperclientconsumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zhaomengxia
 * @create 2019/9/5 11:51
 */
@RestController
public class UserConsumerController {
    @Autowired
    private UserService userService;

    @GetMapping("getUser/{id}")
    public String getUserInfo(@PathVariable Long id){
        return userService.getUserInfo(id);
    }
}

分別啓動DemozookeeperClientConsumerApplication類和ZookeeperClientProviderApplication類

瀏覽器訪問http://localhost:8000/user?id=1

瀏覽器訪問http://localhost:8001/getUser/1

源代碼

https://gitlab.com/zhaomengxia/springcloud-zookeeper.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章