SpringCloud study notes 4 - SpringCloud build Zookeeper

 

Seven, SpringCloud build Zookeeper registry

     1, the local start (execute ./zkServer.sh start command to enter zk bin directory) zookeeper

     2, a graphical view is opened tool zookeeper

         zookeeper graphical client tools Download: https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip ;

         Open: Enter the build directory, find a file download execution java -jar zookeeper-dev-zooinspector.jar command, the effect is as follows:

3, the establishment maven projects, pom.xml import relevant jar package

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gonghua</groupId>
  <artifactId>springcloud-zookeeper-member</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>2.0.1.RELEASE</version>
  </parent>
  
  <!-- 管理依赖 -->
  <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>
  	<!-- springboot整合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>

 

4, the configuration or application.properties yml

##服务端口号
server.port=8003
##服务别名--服务注册到注册中心名称
spring.application.name=zk-member
##注册到zookeeper服务地址
spring.cloud.zookeeper.connect-string=127.0.0.1:2181

 

5, start writing class

package com.gonghua.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperMemberController {
	
	@Value("${server.port}")
	private String serverPort;
	
	@RequestMapping("/getMember")
	public String getMember(){
		return "hello zookeeper member!!! the port is "+serverPort;
	}
	
	//@EnableDiscoveryClient作用:如果服务使用consul、zookeeper则使用该注解。目的是向注册中心上注册服务
	public static void main(String[] args) {
		SpringApplication.run(ZookeeperMemberController.class, args);
	}
}

 

6, started successfully, zk client is shown below

Use zookeeper do load balancing

 1, the service provider has been established as well, followed by the establishment of a consumer service, as follows:

1、pom.xml文件通服务提供者的配置一致,见上述配置。

2、application.yml 或application.properties配置如下:

##服务端口号
server.port=8004
##服务别名--服务注册到注册中心名称
spring.application.name=zk-order
##注册到zookeeper服务地址
spring.cloud.zookeeper.connect-string=127.0.0.1:2181




3、启动类代码:
package com.gonghua;


import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperOrderController {
	//RestTemplate 是由springboot web组件提供,默认整合ribbon负载均衡器,rest方式底层是采用httpclient技术
	@Autowired
	private RestTemplate restTemplate;
	
	/**
	 * 在springcloud中有两种方式调用
	 * 1、rest调用
	 * 2、fegin(springcloud)
	 * @return
	 */
	@RequestMapping("/getOrder")
	public String getOrder(){
		//别名调用,别名见服务提供者的yml配置
		String url = "http://zk-member/getMember";
		String result1 = restTemplate.getForObject(url, String.class);
		System.out.println("order 别名调用 member 服务:"+result1);
		return result1;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(ZookeeperOrderController.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}

}

2, start the service provider, then the case of non-stop services, change the provider's port, and then restart (the equivalent of two to start the service provider to do load balancing), and then start the service consumer, this time zookeeper registration FIG centers are:

         

 

Published 27 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/gonghua0502/article/details/103258947