Spring Cloud Zookeeperの使用
Spring Cloud Zookeeperは、Apache Zookeeperを使用して自分のspring bootサービスを登録するのに役立ちます.
1.zookeeperをインストールし(ここでは、zkのクライアントとサービス側のバージョンが一致することに注意してください)、zkサービスを開始します.
2.パッケージpomをインポートする.xml、ここでは主に2つの部分、spring bootとspring cloud zookeeper
3.spring bootサービスプロファイルアプリケーションを構成する.yml
4.spring bootサービスコード
以上の手順でspring bootサービスをzkに登録する操作を完了します.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
サービス発見をアクティブにするにはspring-cloud-starter-zookeeper-discoveryパッケージを導入する必要があります.サービスに@EnableDiscoveryClientを追加します.
Webにはspring-boot-starter-webパッケージを導入する必要があります
spring boot 2.0.6.RELEASE
zookeeper 3.4.12 3.4.8
spring cloud zookeeper 2.0.0.RELEASE
1.zookeeperをインストールし(ここでは、zkのクライアントとサービス側のバージョンが一致することに注意してください)、zkサービスを開始します.
2.パッケージpomをインポートする.xml、ここでは主に2つの部分、spring bootとspring cloud zookeeper
4.0.0
com.hc
z_11_2
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-zookeeper-dependencies
org.apache.zookeeper
zookeeper
2.0.0.RELEASE
pom
import
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.httpcomponents
httpclient
4.5.3
org.apache.zookeeper
zookeeper
3.4.12
org.slf4j
slf4j-log4j12
3.spring bootサービスプロファイルアプリケーションを構成する.yml
spring:
cloud:
zookeeper:
connect-string: 192.168.37.131:2181
application:
name: zkserv
server:
port: 8888
4.spring bootサービスコード
package zoo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world"+serviceUrl();
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
@Autowired
private DiscoveryClient discoveryClient;
public String serviceUrl() {
List list = discoveryClient.getInstances("zkserv");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri().toString();
}
return null;
}
}
以上の手順でspring bootサービスをzkに登録する操作を完了します.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
サービス発見をアクティブにするにはspring-cloud-starter-zookeeper-discoveryパッケージを導入する必要があります.サービスに@EnableDiscoveryClientを追加します.
Webにはspring-boot-starter-webパッケージを導入する必要があります