Spring Cloud's Zookeeper-based Microservice Cluster Implementation

Spring cloud integrates the use of zookeeper, registers the service through the server, the client discovers the service and uses polling to achieve load balancing. Let's see the specific usage method:
server implementation

1. The pom file needs to introduce the following components

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

2. Contents of the application.yml configuration file: Specify the application name so that the client can call the service using
spring:
  application:
    name: zkserver

3. The implementation of the main method:

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
public class ZkServerApp {

	@RequestMapping("/user")
	public Map<String,Object> info() {
		//The simulation returns a user information
		Map<String, Object> map = new HashMap<>();
		map.put("name", "john");
		map.put("age", 22);
		return map;
	}
	public static void main(String[] args) {
		SpringApplication.run(ZkServerApp.class, args);
	}
}

 
4. Server startup:
The default port is 8080. You can specify the startup port through --server.port. In this example, three service ports of 8080, 8081, and 8082 are started to provide service calls.
The implementation of the client
1. The pom file needs to introduce the following components
. The dependencies of the client can be imported to the server. Same as above
. 2. Contents of the application.yml configuration file: specify the startup port as 8888 to
prevent port conflicts.
server:
  port: 8888
The registration discovery is set to false
spring:
  cloud:
    zookeeper:
      discovery:
        register: false

3. The implementation of the main method:

@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class ZkClientApp {
	
	//Get the application name of the server
	@Value("${spring.application.name:zkserver}")
	private String appName;

	@Bean
	@LoadBalanced
	RestTemplate loadBalancedRestTemplate() {
		return new RestTemplate();
	}
	
	@Autowired
	LoadBalancerClient loadBalance;

	@Autowired
	private RestTemplate rt;

	@RequestMapping("/user")
	public String getUser() {
		System.out.println(loadBalance.choose(appName)); //Print the currently called server address
		@SuppressWarnings("unchecked")
		Map<String, Object> user = rt.getForObject("http://" + this.appName + "/user", Map.class);
		return user.toString();
	}

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

 

4. Start the client and visit http://localhost:8888/user. The following result appears on the page:
{name=john, age=22}. After multiple calls, the console prints the following information:
RibbonServer{serviceId='zkserver', server =windows10.microdone.cn:8080, secure=false, metadata={}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8081, secure=false, metadata={}}
RibbonServer{serviceId=' zkserver', server=windows10.microdone.cn:8082, secure=false, metadata={}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8080, secure=false, metadata={}}
RibbonServer {serviceId='zkserver', server=windows10.microdone.cn:8081, secure=false, metadata={}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8082, secure=false, metadata= {}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8080, secure=false, metadata={}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8081, secure=false, metadata={}}
RibbonServer{serviceId='zkserver', server=windows10.microdone.cn:8082, secure=false, metadata={}}

Complete code address: https://github.com/hjguang/spring-cloud

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326393552&siteId=291194637