1七、SpringBoot------整合dubbo

SpringBoot整合Dubbo+Zookeaper

1.安裝運行zookeeper

(1)下載zookeeper

官網:http://zookeeper.apache.org/html

(2)解壓縮

(3)修改配置文件

  1. 拷貝zoo_sample.cfg重命名爲zoo.cfgjava

  2. 修改配置文檔git

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=../data
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1
  3. 建立data文件夾github

(4)cmd啓動zkServer.sh

2.安裝dubbo web管理客戶端

(1)下載Duboo OPS

下載地址:https://github.com/apache/incubator-dubbo-ops/tree/masterweb

(2)修改配置

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest

dubbo.registry.address=zookeeper://127.0.0.1:2181

(3)maven打包dubbo-admin

(4)啓動生成jar

(5)訪問http://localhost:7001/

3.安裝dubbo監控服務中心

(1)maven打包dubbo-monitor-simple

(2)解壓dubbo-monitor-simple-2.0.0-assembly.tar.gz

(3)修改配置文件

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

dubbo.container=log4j,spring,registry,jetty-monitor
dubbo.application.name=simple-monitor
dubbo.application.owner=dubbo
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.protocol.port=7070
dubbo.jetty.port=8080
dubbo.jetty.directory=${user.home}/monitor
dubbo.charts.directory=${user.home}/monitor/charts
dubbo.statistics.directory=${user.home}/monitor/statistics
dubbo.log4j.file=logs/dubbo-monitor-simple.log
dubbo.log4j.level=WARN

(4)啓動start.bat

(5)訪問http://localhost:8080/

4.整合springboot

(1)新建Maven項目:d_api :一個公用service的api

  1. 新建一個接口:HelloServiceredis

    package com.xm.dubbo.service;
    
    public interface HelloService {
    
    	String sayHello();
    
    }

(2)新建Springboot項目:Hello_Producer

參考連接:https://github.com/apache/incubator-dubbo-spring-boot-projectspring

  1. 添加依賴express

    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.xm.dubbo</groupId>
    	<artifactId>hello_producer</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>hello_producer</name>
    	<description>This is a Web about springcloud</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.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>
    	</properties>
    
    	<dependencies>
    		<dependency>
    		    <groupId>com.alibaba.boot</groupId>
    		    <artifactId>dubbo-spring-boot-starter</artifactId>
    		    <version>0.2.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>com.xm.dubbo</groupId>
    			<artifactId>d_api</artifactId>
    			<version>0.0.1-SNAPSHOT</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>
  2. 修改配置文件apache

    dubbo.application.name=hello_producer
    dubbo.registry.protocol=zookeeper
    dubbo.registry.address=127.0.0.1:2181
    dubbo.scan.base-packages=com.xm.dubbo.service
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=20080
    dubbo.monitor.protocol=registry
  3. 新建HelloServiceImplapi

    package com.xm.dubbo.service.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.xm.dubbo.service.HelloService;
    
    @Service
    @Component
    public class HelloServiceImpl implements HelloService {
    
    	@Override
    	public String sayHello() {
    		System.out.println("生產者已被調用!");
    		return "Hello dubbo!";
    	}
    
    }
  4. 項目入口添加@EnableDubbo註解

    package com.xm.dubbo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    
    @EnableDubbo
    @SpringBootApplication
    public class HelloProducerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(HelloProducerApplication.class, args);
    	}
    }

(3)新建Springboot項目:Hello_Consumer

  1. 添加依賴

    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.xm.dubbo</groupId>
    	<artifactId>hello_consumer</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>hello_consumer</name>
    	<description>This is a Web about springcloud</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.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>
    	</properties>
    
    	<dependencies>
    		<dependency>
    		    <groupId>com.alibaba.boot</groupId>
    		    <artifactId>dubbo-spring-boot-starter</artifactId>
    		    <version>0.2.0</version>
    		</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>com.xm.dubbo</groupId>
    			<artifactId>d_api</artifactId>
    			<version>0.0.1-SNAPSHOT</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>
  2. 修改配置文件

    server.port=8081
    
    dubbo.application.name=hello_consumer
    dubbo.registry.protocol=zookeeper
    dubbo.registry.address=127.0.0.1:2181
    dubbo.monitor.protocol=registry
  3. 新建HelloController

    package com.xm.dubbo.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.xm.dubbo.service.HelloService;
    
    @RestController
    public class HelloController {
    
    	@Reference
    	private HelloService helloService;
    
    	@GetMapping("/hello")
    	public String sayHello() {
    		return helloService.sayHello();
    	}
    
    }
  4. 項目入口添加@EnableDubbo註解

    package com.xm.dubbo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    
    @EnableDubbo
    @SpringBootApplication
    public class HelloConsumerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(HelloConsumerApplication.class, args);
    	}
    }
相關文章
相關標籤/搜索