好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring/springboot整合curator遇到的坑及解决

近期本人在搭建自己的调度平台项目使用到了zookeeper做执行器自动注册中心时,使用到了springboot2.0+curator4.0版本整合

整个代码

pom.xml 

?

1

2

3

4

5

6

7

8

9

10

< dependency >

      < groupId >org.apache.curator</ groupId >

      < artifactId >curator-framework</ artifactId >

      < version >4.0.0</ version >

</ dependency >

< dependency >

      < groupId >org.apache.curator</ groupId >

      < artifactId >curator-recipes</ artifactId >

      < version >4.0.0</ version >

</ dependency >

zookeeper的config类:

?

1

2

3

4

5

6

7

8

application.properties

 

################################## zookeeper ##################################

kafka.zookeeper.host=127.0.0.1:2181

kafka.zookeeper.maxRetry=3

kafka.zookeeper.sessionTimeout=60000

kafka.zookeeper.connectTimeout=10000

kafka.zookeeper.namespace=sensecrowd

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

@Configuration

@ConfigurationProperties (prefix = "kafka.zookeeper" )

public class ZookeeperConfig {

    private final Logger LOGGER = LoggerFactory.getLogger(ZookeeperConfig. class ); 

    private String host; 

    private int maxRetry; 

    private int sessionTimeout; 

    private int connectTimeout; 

    private String namespace;

 

    @Bean

    public CuratorFramework curatorFramework(){

        RetryPolicy retryPolicy = new ExponentialBackoffRetry( 1000 , 1 );

        CuratorFramework client =

                CuratorFrameworkFactory.builder()

                        .connectString(host)

                        .sessionTimeoutMs(sessionTimeout)

                        .connectionTimeoutMs(connectTimeout)

                        .retryPolicy(retryPolicy)

                        /*.aclProvider(new ACLProvider() {

                            private List<ACL> acl ;

                            @Override

                            public List<ACL> getDefaultAcl() {

                                if(acl ==null) {

                                    ArrayList<ACL> acl = ZooDefs.Ids.CREATOR_ALL_ACL;

                                    acl.clear();

                                    acl.add(new ACL(ZooDefs.Perms.ALL, new Id("auth", "admin:admin")));

                                    this.acl = acl;

                                }

                                return acl;

                            }

                            @Override

                            public List<ACL> getAclForPath(String s) {

                                return acl;

                            }

                        })*/

//                        .namespace(namespace)

                        .build();

        client.start();

        client.getConnectionStateListenable().addListener( new ZookeeperConnectionListener(host,maxRetry,sessionTimeout,connectTimeout,namespace));

        return client;

    }

 

    @PreDestroy

    private void destroyClient(){

        curatorFramework().close();

        LOGGER.info( "==================关闭成功==================" );

    }

 

    public String getHost() {

        return host;

    }

 

    public void setHost(String host) {

        this .host = host;

    }

 

    public int getMaxRetry() {

        return maxRetry;

    }

 

    public void setMaxRetry( int maxRetry) {

        this .maxRetry = maxRetry;

    }

 

    public int getSessionTimeout() {

        return sessionTimeout;

    }

 

    public void setSessionTimeout( int sessionTimeout) {

        this .sessionTimeout = sessionTimeout;

    }

 

    public int getConnectTimeout() {

        return connectTimeout;

    }

 

    public void setConnectTimeout( int connectTimeout) {

        this .connectTimeout = connectTimeout;

    }

 

    public String getNamespace() {

        return namespace;

    }

 

    public void setNamespace(String namespace) {

        this .namespace = namespace;

    }

}

可项目遇到了两个问题

1)、项目运行控制台会报Log4j日志警告,原因是我的项目使用的springboot整合的log4j模块而curator包含slf4j的日志包,zookeeper包含log4j的日志包,所以log4j的版本冲突导致。

2)、curator可以查看节点信息,但创建节点会导致程序进程阻塞,根据zookeeper版本不同报出不同的问题,我使用的是默认的curator4.0.0自带的zookeeper版本,3.5.+-beta版,添加节点报,并且程序阻塞:

Unable to read additional data from server sessionid 0x1002fd7768a015f

解决办法

修改pom依赖,第一解决log4j和slaf4j依赖版本冲突问题,第二curator依赖的zookeeper版本修改成你服务器安装的zookeeper服务的版本,完美解决。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<!-- zookeeper -->

            < dependency >

                < groupId >org.apache.zookeeper</ groupId >

                < artifactId >zookeeper</ artifactId >

                < version >3.4.10</ version >

                < exclusions >

                    < exclusion >

                        < groupId >org.slf4j</ groupId >

                        < artifactId >slf4j-log4j12</ artifactId >

                    </ exclusion >

                    < exclusion >

                        < groupId >org.slf4j</ groupId >

                        < artifactId >slf4j-api</ artifactId >

                    </ exclusion >

                    < exclusion >

                        < groupId >log4j</ groupId >

                        < artifactId >log4j</ artifactId >

                    </ exclusion >

                </ exclusions >

            </ dependency >

 

            < dependency >

                < groupId >org.apache.curator</ groupId >

                < artifactId >curator-framework</ artifactId >

                < version >4.0.0</ version >

                < exclusions >

                    < exclusion >

                        < groupId >org.slf4j</ groupId >

                        < artifactId >slf4j-api</ artifactId >

                    </ exclusion >

                </ exclusions >

            </ dependency >

            < dependency >

                < groupId >org.apache.curator</ groupId >

                < artifactId >curator-recipes</ artifactId >

                < version >4.0.0</ version >

            </ dependency >

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。 

原文链接:https://blog.csdn.net/lishentao_1122/article/details/89375222

查看更多关于spring/springboot整合curator遇到的坑及解决的详细内容...

  阅读:18次