SpringCloud使用Kafka消息總線、Kafka的安裝與測試(CentOS 7)

SpringCloud使用Kafka消息總線、Kafka的安裝與測試(CentOS 7)

主要內容:

  • CentOS7靜態地址配置
  • Kafka安裝與測試
  • SpringCloud使用Kafka做消息總線

CentOS7靜態地址配置

打開配置文件:

[root@localhost network-scripts]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

修改內容如下:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #修改
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=bd4786ea-5587-45de-ad19-77786fe0d6f9
DEVICE=ens33
ONBOOT=yes #修改
IPADDR0=192.168.129.130 #增加
PREFIXO0=24 #增加
GATEWAY0=192.168.129.2 #增加(虛擬機中網關)
DNS1=8.8.8.8 #增加
DNS2=8.8.4.4 #增加

重啓網絡

[root@localhost network-scripts]# service network restart

查看IP地址

[root@localhost network-scripts]# ip addr

安裝JDK

[root@localhost home]# mkdir jdk
[root@localhost home]# ls
jdk
[root@localhost home]# cd jdk
[root@localhost jdk]# ls
jdk-8u161-linux-x64.tar.gz

解壓文件

[root@localhost jdk]# tar -zxvf jdk-8u161-linux-x64.tar.gz
[root@localhost jdk]# ls
jdk1.8.0_161 jdk-8u161-linux-x64.tar.gz

配置環境變量

[root@localhost ~]# vi /etc/profile

文本末尾加入以下內容

export JAVA_HOME=/home/jdk/jdk1.8.0_161
export CLASSPATH=.
export PATH=$PATH:${JAVA_HOME}/bin

配置生效

[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version “1.8.0_161”
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

Kafka安裝與測試

[root@localhost kafka]# tar -zxvf kafka_2.11-2.0.0.tgz
[root@localhost kafka_2.11-2.0.0]# ls
bin config libs LICENSE NOTICE site-docs
[root@localhost kafka_2.11-2.0.0]# cd config
[root@localhost config]# vi zookeeper.properties

可配置zookeeper的端口日誌
配置kafka:端口號,連接zookeeper等

[root@localhost kafka_2.11-2.0.0]# cd config/
[root@localhost config]# vi server-1.properties

啓動zookeeper和kafka

[root@localhost kafka_2.11-2.0.0]# nohup ./bin/zookeeper-server-start.sh config/zookeeper.properties &
[1] 1119
[root@localhost kafka_2.11-2.0.0]# nohup: 忽略輸入並把輸出追加到”nohup.out”
[root@localhost kafka_2.11-2.0.0]# nohup ./bin/kafka-server-start.sh ./config/server-1.properties &
[2] 1408
[root@localhost kafka_2.11-2.0.0]# nohup: 忽略輸入並把輸出追加到”nohup.out”
[root@localhost kafka_2.11-2.0.0]# jps
1408 Kafka
1744 Jps
1119 QuorumPeerMain

創建topic、查看topic狀態

[root@localhost kafka_2.11-2.0.0]# bin/kafka-topics.sh –create –zookeeper 192.168.129.130:2181 –replication-factor 1 –partitions 1 –topic test
Created topic “test”.
[root@localhost kafka_2.11-2.0.0]# bin/kafka-topics.sh -zookeeper 192.168.129.130:2181 -list
test
[root@localhost kafka_2.11-2.0.0]# bin/kafka-topics.sh –zookeeper 192.168.129.130:2181 –describe –topic test
Topic:test PartitionCount:1 ReplicationFactor:1 Configs:
Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0

發佈消息

[root@localhost kafka_2.11-2.0.0]# bin/kafka-console-producer.sh –broker-list 192.168.129.130:9092 –topic test

>oooo
>aaaa

訂閱消息

[root@localhost kafka_2.11-2.0.0]# bin/kafka-console-consumer.sh –bootstrap-server 192.168.129.130:9092 –topic gxl –from-beginning
oooo
aaaa

之前遇到個報錯:

ERROR Error when sending message to topic test with key: null, value: 5 byt…
原因:發佈消息和訂閱命令中kafka端口寫錯或者是要發佈的topic不存在

SpringCloud使用Kafka做消息總線

總體架構:
這裏寫圖片描述
1、註冊中心地址:
eureka.client.serviceUrl.defaultZone=http://frog:10000/eureka
2、配置中心(configserver):
pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 配置中心加密 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- 服務化的配置中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- kafka消息總線 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
    </dependencies>

    <!-- SpringCloud -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

application.properties

spring.application.name=frog-config
server.port=10010
# 通過本地文件系統(指定位置)
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/properties/
# 安全保護(添加security依賴)
# 不使用隨機密碼(不配置默認使用隨機密碼),使用指定密碼
security.user.name=user
security.user.password=1234567890
# 服務化的配置中心
eureka.client.serviceUrl.defaultZone=http://frog:10000/eureka
# 服務註冊中心實例的主機名
eureka.instance.hostname=frog

kafka-dev.properties

#Kafka的服務端列表,默認localhost
spring.cloud.stream.kafka.binder.brokers=192.168.129.130:9092
#Kafka服務端的默認端口,當brokers屬性中沒有配置端口信息時,就會使用這個默認端口,默認9092
spring.cloud.stream.kafka.binder.defaultBrokerPort=9092
#Kafka服務端連接的ZooKeeper節點列表,默認localhost
spring.cloud.stream.kafka.binder.zkNodes=192.168.129.130:2181
#ZooKeeper節點的默認端口,當zkNodes屬性中沒有配置端口信息時,就會使用這個默認端口,默認2181
spring.cloud.stream.kafka.binder.defaultZkPort=2181

frog-config-client-dev.properties

str=dev2.3333333333333

config-server項目結構:
這裏寫圖片描述
配置中心需要啓動後加載kafka-dev.properties配置文件中的配置信息需要在啓動類中做如下改動:

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {

    public static void main(String[] args) throws IOException {
        // 加載自定義配置文件
        Properties properties = new Properties();
        InputStream inputStream = ConfigApplication.class.getClassLoader().getResourceAsStream("properties/kafka-dev.properties");
        properties.load(inputStream);
        // 測試有沒有加載
        System.out.println(properties.getProperty("spring.cloud.stream.kafka.binder.brokers"));
        SpringApplication sa = new SpringApplication(ConfigApplication.class);
        sa.setDefaultProperties(properties);
        sa.run(args);
    }
}

啓動
這裏寫圖片描述
3、客戶端(client)
pom文件與configserver相同
application.properties

server.port=10011

# 配置中心設置賬戶密碼,需要驗證
spring.cloud.config.username=user
spring.cloud.config.password=1234567890

spring.application.name=frog-config-client
# 服務化的配置中心(啓動時先啓動註冊中心,在啓動配置中心server,穩定後啓動服務端)
eureka.client.serviceUrl.defaultZone=http://frog:10000/eureka
# 服務註冊中心實例的主機名
eureka.instance.hostname=frog
# 開啓通過服務來訪問config server的功能
spring.cloud.config.discovery.enabled=true
# 指定config server註冊的服務名
spring.cloud.config.discovery.service-id=frog-config
# 指定配置中心的資源
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.name=frog-config-client,kafka

# /refresh刷新時需要將驗證關閉
management.security.enabled=false

測試接口

@RestController
@RefreshScope
public class TestController {

    @GetMapping("/test")
    public String test() {
        return this.str;
    }

    @Value("${str}")
    private String str;
}

啓動後請求/test看是否正常,因configserver配置了權限校驗,需要輸入用戶名和密碼
這裏寫圖片描述
4、刷新配置
修改frog-config-client-dev.properties配置文件中str值,再次請求client的/test發現沒有刷新。
通過postman請求configserver的/bus/refresh來刷新配置,頭部信息添加權限校驗信息。

這裏寫圖片描述
這裏寫圖片描述
刷新後再次請求客戶端/test接口發現值發生改變。
5、bug
我目前使用的springcloud版本在請求配置中心/bus/refresh後會導致註冊中心所有服務處於離線狀態,使用Dalston新版本即可,比如:
pom:

<spring-cloud.version>Dalston.SR5</spring-cloud.version>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章