【Java】SpringCloud整合Zookeeper

SpringCloud整合Zookeeper

isWulongbo发布于 今天 03:31

创建项目

使用 Spring Initializr 新建一个项目命名为: springcloud-zookeeper,删除多余的文件,并新建两个子模块分别为:springcloud-zookeeper-memberspringcloud-zookeeper-order
【Java】SpringCloud整合Zookeeper

父工程 pom.xml配置文件spring-boot 版本这里选用 2.0.1.RELEASE

<?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>

<packaging>pom</packaging>

<modules> <module>springcloud-zookeeper-member</module>

<module>springcloud-zookeeper-order</module>

</modules> <parent> <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.1.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.baba.wlb</groupId>

<artifactId>springcloud-zookeeper</artifactId>

<version>1.0-SNAPSHOT</version>

<name>springcloud-zookeeper</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-web</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>

<scope>test</scope>

</dependency> </dependencies>

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</exclude> </excludes> </configuration> </plugin> </plugins> </build>

</project>

子模块

springcloud-zookeeper-member模块:

【Java】SpringCloud整合Zookeeper

pom.xml文件:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent> <artifactId>springcloud-zookeeper</artifactId>

<groupId>com.baba.wlb</groupId>

<version>1.0-SNAPSHOT</version>

</parent> <modelVersion>4.0.0</modelVersion>

<artifactId>springcloud-zookeeper-member</artifactId>

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.M7</version>

<type>pom</type>

<scope>import</scope>

</dependency> </dependencies> </dependencyManagement>

<dependencies>

<!--springboot 整合web组件-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--springcloud 整合zookeeper客户端-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

</dependency> </dependencies>

<!--注意:这里必须添加,否则各种依赖有问题-->

<repositories>

<repository> <id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/libs-milestone</url>

<snapshots> <enabled>false</enabled>

</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号

server:

port: 7001

##dubbo 注册到注册中心的名称

spring:

application:

name: zk-member

cloud:

zookeeper:

connect-string: 39.102.56.91:2181

MemberApiController 控制页面:

package com.baba.wlb.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @Author wulongbo

* @Date 2021/1/9 15:20

* @Version 1.0

*/@RestController

public class MemberApiController {

@Value("${server.port}")

private String serverPort;

@RequestMapping("/getMember")

public String getMember() {

return "我是会员服务!端口号:"+serverPort;

}

}

AppMember 启动类:

package com.baba.wlb.controller;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**

* @Author wulongbo

* @Date 2021/1/9 15:22

* @Version 1.0

*/@EnableDiscoveryClient

@SpringBootApplication

public class AppMember {

// @EnableDiscoveryClient作用是 如果服务使用zookeeper或者connsul,使用@EnableDiscoveryClient向注册中心注册服务

public static void main(String[] args) {

SpringApplication.run(AppMember.class,args);

}

}

springcloud-zookeeper-order模块:

【Java】SpringCloud整合Zookeeper

pom.xml 文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent> <artifactId>springcloud-zookeeper</artifactId>

<groupId>com.baba.wlb</groupId>

<version>1.0-SNAPSHOT</version>

</parent> <modelVersion>4.0.0</modelVersion>

<artifactId>springcloud-zookeeper-order</artifactId>

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.M7</version>

<type>pom</type>

<scope>import</scope>

</dependency> </dependencies> </dependencyManagement>

<dependencies>

<!--springboot 整合web组件-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--springcloud 整合zookeeper客户端-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

</dependency> </dependencies>

<!--注意:这里必须添加,否则各种依赖有问题-->

<repositories>

<repository> <id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/libs-milestone</url>

<snapshots> <enabled>false</enabled>

</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号

server:

port: 7002

##dubbo 注册到注册中心的名称

spring:

application:

name: zk-order

cloud:

zookeeper:

connect-string: 39.102.56.91:2181

OrderApiController 控制页面:

package com.baba.wlb.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

/**

* @Author wulongbo

* @Date 2021/1/9 15:32

* @Version 1.0

*/@RestController

public class OrderApiController {

@Autowired

private RestTemplate restTemplate;

/**

* springCloud中,两种方式调用(rest/feign)

*

* @return

*/

// 订单服务调用会员服务

@RequestMapping("/getOrder")

public String getOrder() {

// 有两种调用方式,一种是采用服务别名方式调用,另一种是使用别名去注册中心上获取对应服务调用地址

// 第一种方式

String url = "http://dy-202006281547:8000/getMember";

// 第二种方式

url = "http://zk-member/getMember";

String result = restTemplate.getForObject(url, String.class);

return "订单服务调用会员服务:" + result;

}

}

AppOrder.java 启动类:

package com.baba.wlb.controller;

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 wulongbo

* @Date 2021/1/9 15:39

* @Version 1.0

*/@SpringBootApplication

@EnableDiscoveryClient

public class AppOrder {

public static void main(String[] args) {

SpringApplication.run(AppOrder.class,args);

// 第二种方式 如果使用rest方式以别名方式进行调用依赖ribbon负载均衡器

// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力

}

// 解决RestTemplate 找不到原因, 把RestTemplate注册到Springboot容器中 @Bean

// 第一种方式

// @Bean

// RestTemplate restTemplate(){

// return new RestTemplate();

// }

// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力

@Bean

@LoadBalanced RestTemplate restTemplate(){

return new RestTemplate();

}

}

启动项目

启动 AppMember.javaAppOrder.java

【Java】SpringCloud整合Zookeeper

【Java】SpringCloud整合Zookeeper

查看zookeeper上新注册了两个临时节点
【Java】SpringCloud整合Zookeeper

浏览器访问 http://localhost:7002/getOrderorder服务可以调用到member服务。
【Java】SpringCloud整合Zookeeper
读者也可以给member服务做集群,启动多个member服务,实现负载均衡。

javaspringboot

阅读 33发布于 今天 03:31

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议


wulongbo

我的专栏

avatar

isWulongbo

在人生的头三十年,你培养习惯,后三十年,习惯铸就你

178 声望

7 粉丝

0 条评论

得票时间

avatar

isWulongbo

在人生的头三十年,你培养习惯,后三十年,习惯铸就你

178 声望

7 粉丝

宣传栏

创建项目

使用 Spring Initializr 新建一个项目命名为: springcloud-zookeeper,删除多余的文件,并新建两个子模块分别为:springcloud-zookeeper-memberspringcloud-zookeeper-order
【Java】SpringCloud整合Zookeeper

父工程 pom.xml配置文件spring-boot 版本这里选用 2.0.1.RELEASE

<?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>

<packaging>pom</packaging>

<modules> <module>springcloud-zookeeper-member</module>

<module>springcloud-zookeeper-order</module>

</modules> <parent> <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.1.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.baba.wlb</groupId>

<artifactId>springcloud-zookeeper</artifactId>

<version>1.0-SNAPSHOT</version>

<name>springcloud-zookeeper</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-web</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>

<scope>test</scope>

</dependency> </dependencies>

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</exclude> </excludes> </configuration> </plugin> </plugins> </build>

</project>

子模块

springcloud-zookeeper-member模块:

【Java】SpringCloud整合Zookeeper

pom.xml文件:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent> <artifactId>springcloud-zookeeper</artifactId>

<groupId>com.baba.wlb</groupId>

<version>1.0-SNAPSHOT</version>

</parent> <modelVersion>4.0.0</modelVersion>

<artifactId>springcloud-zookeeper-member</artifactId>

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.M7</version>

<type>pom</type>

<scope>import</scope>

</dependency> </dependencies> </dependencyManagement>

<dependencies>

<!--springboot 整合web组件-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--springcloud 整合zookeeper客户端-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

</dependency> </dependencies>

<!--注意:这里必须添加,否则各种依赖有问题-->

<repositories>

<repository> <id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/libs-milestone</url>

<snapshots> <enabled>false</enabled>

</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号

server:

port: 7001

##dubbo 注册到注册中心的名称

spring:

application:

name: zk-member

cloud:

zookeeper:

connect-string: 39.102.56.91:2181

MemberApiController 控制页面:

package com.baba.wlb.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @Author wulongbo

* @Date 2021/1/9 15:20

* @Version 1.0

*/@RestController

public class MemberApiController {

@Value("${server.port}")

private String serverPort;

@RequestMapping("/getMember")

public String getMember() {

return "我是会员服务!端口号:"+serverPort;

}

}

AppMember 启动类:

package com.baba.wlb.controller;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**

* @Author wulongbo

* @Date 2021/1/9 15:22

* @Version 1.0

*/@EnableDiscoveryClient

@SpringBootApplication

public class AppMember {

// @EnableDiscoveryClient作用是 如果服务使用zookeeper或者connsul,使用@EnableDiscoveryClient向注册中心注册服务

public static void main(String[] args) {

SpringApplication.run(AppMember.class,args);

}

}

springcloud-zookeeper-order模块:

【Java】SpringCloud整合Zookeeper

pom.xml 文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent> <artifactId>springcloud-zookeeper</artifactId>

<groupId>com.baba.wlb</groupId>

<version>1.0-SNAPSHOT</version>

</parent> <modelVersion>4.0.0</modelVersion>

<artifactId>springcloud-zookeeper-order</artifactId>

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Finchley.M7</version>

<type>pom</type>

<scope>import</scope>

</dependency> </dependencies> </dependencyManagement>

<dependencies>

<!--springboot 整合web组件-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--springcloud 整合zookeeper客户端-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>

</dependency> </dependencies>

<!--注意:这里必须添加,否则各种依赖有问题-->

<repositories>

<repository> <id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/libs-milestone</url>

<snapshots> <enabled>false</enabled>

</snapshots> </repository> </repositories></project>

application.yml配置文件:

##服务器端口号

server:

port: 7002

##dubbo 注册到注册中心的名称

spring:

application:

name: zk-order

cloud:

zookeeper:

connect-string: 39.102.56.91:2181

OrderApiController 控制页面:

package com.baba.wlb.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

/**

* @Author wulongbo

* @Date 2021/1/9 15:32

* @Version 1.0

*/@RestController

public class OrderApiController {

@Autowired

private RestTemplate restTemplate;

/**

* springCloud中,两种方式调用(rest/feign)

*

* @return

*/

// 订单服务调用会员服务

@RequestMapping("/getOrder")

public String getOrder() {

// 有两种调用方式,一种是采用服务别名方式调用,另一种是使用别名去注册中心上获取对应服务调用地址

// 第一种方式

String url = "http://dy-202006281547:8000/getMember";

// 第二种方式

url = "http://zk-member/getMember";

String result = restTemplate.getForObject(url, String.class);

return "订单服务调用会员服务:" + result;

}

}

AppOrder.java 启动类:

package com.baba.wlb.controller;

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 wulongbo

* @Date 2021/1/9 15:39

* @Version 1.0

*/@SpringBootApplication

@EnableDiscoveryClient

public class AppOrder {

public static void main(String[] args) {

SpringApplication.run(AppOrder.class,args);

// 第二种方式 如果使用rest方式以别名方式进行调用依赖ribbon负载均衡器

// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力

}

// 解决RestTemplate 找不到原因, 把RestTemplate注册到Springboot容器中 @Bean

// 第一种方式

// @Bean

// RestTemplate restTemplate(){

// return new RestTemplate();

// }

// 第二种方式 @LoadBalanced能让restTemplate 模板在请求时拥有客户端负载均衡的能力

@Bean

@LoadBalanced RestTemplate restTemplate(){

return new RestTemplate();

}

}

启动项目

启动 AppMember.javaAppOrder.java

【Java】SpringCloud整合Zookeeper

【Java】SpringCloud整合Zookeeper

查看zookeeper上新注册了两个临时节点
【Java】SpringCloud整合Zookeeper

浏览器访问 http://localhost:7002/getOrderorder服务可以调用到member服务。
【Java】SpringCloud整合Zookeeper
读者也可以给member服务做集群,启动多个member服务,实现负载均衡。

以上是 【Java】SpringCloud整合Zookeeper 的全部内容, 来源链接: utcz.com/a/105210.html

回到顶部