eureka + kafka +zookeeper

eureka 配置中心,註冊中心環境搭建kafka 消息中間件node

下載安裝 zookeeper, kafka,kafka啓動前保證zookeeper啓動狀態。web

①、https://www.apache.org/dyn/closer.cgi/zookeeper/  spring

進入Zookeeper解壓目錄,E:\WindowsOS_ApacheKafka_20160126\Zookeeper\zookeeper-3.4.6\confapache

②、將「zoo_sample.cfg」重命名爲「zoo.cfg」。bootstrap

③、配置啓動日記目錄,用#註解調 dataDir=/tmp/zookeeper 
dataDir=E:\WindowsOS_ApacheKafka_20160126\Zookeeper\data windows

④、系統環境變量中添加:ZOOKEEPER_HOME = E:\WindowsOS_ApacheKafka_20160126\Zookeeper\zookeeper-3.4.6服務器

⑤、編輯系統變量path,加上: ZOOKEEPER_HOME%\bin; ⑥、確認zoo.cfg文件中默認的Zookeeper端口(默認端口2181)。 
打開新的cmd,輸入zkserver,運行Zookeeper。app

        http://archive.apache.org/dist/kafka/1.1.0/ 下載kafka_2.11-0.9.0.0.tgz,解壓後重命名爲kafka_2.11,進入Kafka配置目錄,E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka_2.11\config 
編輯文件「server.properties」 
找到並用#註解「log.dirs=/tmp/kafka-logs」 
添加本身的日記目錄:log.dirs=E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka-logsmaven

 若是Zookeeper在某些其餘的機器或集羣上運行,能夠將「zookeeper.connect:2181」修改成自定義IP與端口。在這裏使用了同一個機器,因此沒其餘作修改。文件中的Kafka端口和broker.id也是能夠配置的。默認設置不變。 
機器的localhost也爲127.0.0.1,這裏我也修改成ipv4的,防止localhost爲ipv6時受影響。 afka會按照默認,在9092端口上運行,並鏈接zookeeper的默認端口:2181。 
在zookeeper的基礎上,運行Kafka服務 
進入Kafka安裝目錄,E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka_2.11 
切換到命令行窗口,運行kafka。 
.\bin\windows\kafka-server-start.bat .\config\server.propertiesspring-boot

如今建立主題,命名爲「test」,replication factor=1(由於只有1個Kafka服務器在運行)。若是集羣中所運行的Kafka服務器不止1個,能夠相應增長replication-factor,從而提升數據可用性和系統容錯性。 
2. 在E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka_2.11\bin\windows打開新的命令行。 
3. 輸入下面的命令,回車: 
kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic test

建立Producer及Consumer來測試服務器。 
1.在E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka_2.11\bin\windows打開新的命令行。 
2.輸入如下命令,啓動producer,能夠輸入消息: 
kafka-console-producer.bat –broker-list localhost:9092 –topic test 
3.在一樣的位置E:\WindowsOS_ApacheKafka_20160126\Kafka\kafka_2.11\bin\windows再次打開新的命令行。 
4.如今輸入下列命令啓動consumer,能夠獲取消息: 
kafka-console-consumer.bat –zookeeper localhost:2181 –topic test 

兩個命令行窗口,producer能夠輸入任何消息,consumer能夠獲取消息 

1. eureka-server
1.1 pom.xml
<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>org.eureka.server</groupId>
    <artifactId>eureka_server1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
    </parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>    
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
1.2 ====== application.yml=======
server:
  port: 8761
  
# 是否要開啓基本的鑑權  
security:
  basic:
    enabled: false
  user:
    name: admin
    password: 123456

management:
  security:
    enabled: false

spring:
  profiles:
    active: peer2,native
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/Users/xuzhi268/zhongchou/config_profiles

eureka:
  instance:
    hostname: peer1
    lease-renewal-interval-in-seconds: 30 #指定續約更新頻率,默認是 30s
  environment: dev
  client:
    register-with-eureka: false # 禁用eureka做爲客戶端註冊本身
    fetch-registry: false       # 表示是否從eureka server獲取註冊信息,若是是單一節點,不須要同步其餘eureka server節點,則能夠設置爲false,但此處爲集羣,應該設置爲true,默認爲true,可不設置
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8766/eureka/
      #http://admin:123456@peer1:8761/eureka/,http://admin:123456@peer2:8766/eureka/ #多個用逗號隔開
      

-------------------------------- server_2 ------------------------------
server:
  port: 8766
  
security:
  basic:
    enabled: false
  user:
    name: admin
    password: 123456
   
management:
  security:
    enabled: false

spring:
  application:
    name: eureka
  profiles:
    active: peer1,native
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/Users/xuzhi268/zhongchou/config_profiles
    stream:
      default-binder: kafka
      kafka:
        binder:
          brokers: localhost:9092
          zk-nodes: localhost:2181    
           
eureka:
  instance:
    hostname: peer2
    lease-renewal-interval-in-seconds: 30
  environment: dev
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8766/eureka/
  #http://admin:123456@peer1:8761/eureka/,http://admin:123456@peer2:8766/eureka/
  
      
1.3 ======= eureka-server 啓動類 =========== 
package org.eureka.server;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
@EnableConfigServer
public class EurekaServerStarter {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServerStarter.class).run(args);
    }
}

2 ========== ucenter 用戶中心 ==========               
2.1 pom.xml
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cloud.eureka.client</groupId>
    <artifactId>ucenter</artifactId>
    <packaging>war</packaging>
    <version>1.0.1-SNAPSHOT</version>
    <name>ucenter Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
    
    </dependencies>
    
    
    <build>
        <finalName>ucenter</finalName>
    </build>
</project>

2.2 application.yml 和 

#server:
  #當前服務端口號
 # port: 8762
spring:
  application:
    #當前應用名稱
    name: ucenter
    index: 1
  cloud:
    stream:
      bindings:
        springCloudBusInput:  
            destination: springCloudBus
            group: cloud-bus-testgroup:${spring.application.index}   
    
    
eureka:
  client:
    serviceUrl:
      #註冊中心的地址
      defaultZone: http://peer1:8761/eureka/,http://peer2:8766/eureka/
      
------------------------bootstrap.yml---------------------------

#禁用配置中心權限驗證
management:
  security:
    enabled: false

spring:
  cloud:
    config:
      uri: http://localhost:8761/       
        
        
feign:
  httpclient:
    enabled: true
    max-connections: 200 # 默認值
    max-connections-per-route: 50 # 默認值      
    
2.3 啓動類

package com.cloud.eureka.client.ucenter;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication @EnableEurekaClient public class UcenterApplicationRunner {          public static void main(String[] args) {         new SpringApplicationBuilder(UcenterApplicationRunner.class).properties("server.port=" + 8765).run(args);     } }

相關文章
相關標籤/搜索