中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置

發布時間:2020-06-17 20:27:12 來源:網絡 閱讀:774 作者:沙漏半杯 欄目:編程語言

創建新項目

  • 我們使用maven的空項目,不用任何模板直接創建


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


  • 輸入項目信息


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


  • idea會自動去掉橫線,自己加上


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


  • 在項目中新建空Maven的module如下:


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


  • 父項目依賴文件:使用dependencyManagement做依賴管理

<?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>io.ilss.dubbo</groupId>
????<artifactId>spring-boot-dubbo-ilss</artifactId>
????<version>1.0</version>
????<modules>
????????<module>ilss-provider</module>
????????<module>ilss-service</module>
????????<module>ilss-consumer</module>
????</modules>
????<packaging>pom</packaging>
????<properties>
????????<java.version>1.8</java.version>
????????<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
????????<dubbo-boot.version>0.2.0</dubbo-boot.version>
????????<mybatis-boot.version>2.0.0</mybatis-boot.version>
????????<druid.version>1.1.10</druid.version>
????</properties>

????<dependencyManagement>
????????<dependencies>
????????????<!--?Spring?Boot?-->
????????????<dependency>
????????????????<groupId>org.springframework.boot</groupId>
????????????????<artifactId>spring-boot-dependencies</artifactId>
????????????????<version>${spring-boot.version}</version>
????????????????<type>pom</type>
????????????????<scope>import</scope>
????????????</dependency>
????????????<!--?Dubbo?-->
????????????<dependency>
????????????????<groupId>com.alibaba.boot</groupId>
????????????????<artifactId>dubbo-spring-boot-starter</artifactId>
????????????????<version>${dubbo-boot.version}</version>
????????????</dependency>
????????????<!--?mybatis?-->
????????????<dependency>
????????????????<groupId>org.mybatis.spring.boot</groupId>
????????????????<artifactId>mybatis-spring-boot-starter</artifactId>
????????????????<version>${mybatis-boot.version}</version>
????????????</dependency>
????????????<dependency>
????????????????<groupId>com.alibaba</groupId>
????????????????<artifactId>druid</artifactId>
????????????????<version>${druid.version}</version>
????????????</dependency>

????????</dependencies>
????</dependencyManagement>

????<!--?dubbo官方提供的repository?-->
????<repositories>
????????<repository>
????????????<id>sonatype-nexus-snapshots</id>
????????????<url>https://oss.sonatype.org/content/repositories/snapshots</url>
????????????<releases>
????????????????<enabled>false</enabled>
????????????</releases>
????????????<snapshots>
????????????????<enabled>true</enabled>
????????????</snapshots>
????????</repository>
????</repositories></project>

Provider配置

  • provider的依賴

<?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">
????<parent>
????????<artifactId>spring-boot-dubbo-ilss</artifactId>
????????<groupId>io.ilss.dubbo</groupId>
????????<version>1.0</version>
????</parent>
????<modelVersion>4.0.0</modelVersion>

????<artifactId>ilss-provider</artifactId>

????<dependencies>
????????<!--?本地模塊依賴?-->
????????<dependency>
????????????<groupId>io.ilss.dubbo</groupId>
????????????<artifactId>ilss-service</artifactId>
????????????<version>1.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.alibaba.boot</groupId>
????????????<artifactId>dubbo-spring-boot-starter</artifactId>
????????</dependency>

????????<!--?mysql?-->
????????<dependency>
????????????<groupId>mysql</groupId>
????????????<artifactId>mysql-connector-java</artifactId>
????????????<scope>runtime</scope>
????????</dependency>
????????<!--?mybatis?-->
????????<dependency>
????????????<groupId>org.mybatis.spring.boot</groupId>
????????????<artifactId>mybatis-spring-boot-starter</artifactId>
????????</dependency>
????????<!--?druid?-->
????????<dependency>
????????????<groupId>com.alibaba</groupId>
????????????<artifactId>druid</artifactId>
????????</dependency>
????</dependencies>

????<build>
????????<plugins>
????????????<plugin>
????????????????<groupId>org.springframework.boot</groupId>
????????????????<artifactId>spring-boot-maven-plugin</artifactId>
????????????</plugin>
????????????<plugin>
????????????????<groupId>org.apache.maven.plugins</groupId>
????????????????<artifactId>maven-compiler-plugin</artifactId>
????????????????<configuration>
????????????????????<source>1.8</source>
????????????????????<target>1.8</target>
????????????????</configuration>
????????????</plugin>
????????</plugins>
????</build></project>
  • provider的yml配置文件

spring:
??application:
????name:?ilss-provider??datasource:
????type:?com.alibaba.druid.pool.DruidDataSource
????driver-class-name:?com.mysql.jdbc.Driver
????url:?jdbc:mysql://localhost:3306/xxx?useSSL=false
????username:?root????password:?xxxmybatis:
??mapperLocations:?classpath:mapper/*.xmldubbo:
??registry:
????protocol:?zookeeper????address:?127.0.0.1:2181
????id:?registry-1
??protocol:
????port:?20888
????name:?dubbo????id:?dubbo????status:?server??application:
????name:?ilss-provider????id:?ilss-provider??scan:
????basePackages:?io.ilss.dubbo.service.impl??provider:
????version:?1.0
  • provider的啟動類

package?io.ilss.dubbo;import?org.springframework.boot.WebApplicationType;import?org.springframework.boot.autoconfigure.SpringBootApplication;import?org.springframework.boot.builder.SpringApplicationBuilder;/**
*?className?IlssProviderApplication
*?description?IlssProviderApplication
*
*?@author?feng
*?@version?1.0
*?@date?2019-01-30?18:08
*/@SpringBootApplicationpublic?class?IlssProviderApplication?{???public?static?void?main(String[]?args)?{???????//由于Provider不需要web服務,所以我們使用new?SpringApplicationBuilder時web指定NONE
???????new?SpringApplicationBuilder(IlssProviderApplication.class).web(WebApplicationType.NONE).run(args);
???}
}

Consumer配置

  • consumer依賴

<?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">
????<parent>
????????<artifactId>spring-boot-dubbo-ilss</artifactId>
????????<groupId>io.ilss.dubbo</groupId>
????????<version>1.0</version>
????</parent>
????<modelVersion>4.0.0</modelVersion>

????<artifactId>ilss-consumer</artifactId>

????<dependencies>
????????<dependency>
????????????<groupId>io.ilss.dubbo</groupId>
????????????<artifactId>ilss-service</artifactId>
????????????<version>1.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>
????????<!--?dubbo?-->
????????<dependency>
????????????<groupId>com.alibaba.boot</groupId>
????????????<artifactId>dubbo-spring-boot-starter</artifactId>
????????</dependency>
????????<!--?boot-web?依賴?-->
????????<dependency>
????????????<groupId>org.springframework.boot</groupId>
????????????<artifactId>spring-boot-starter-web</artifactId>
????????</dependency>
????</dependencies></project>
  • consumer的yml配置文件

server:??port:?8080dubbo:??application:????id:?ilss-consumer
????name:?ilss-consumer
??registry:????protocol:?zookeeper
????address:?127.0.0.1:2181
????id:?registry-1
??protocol:????name:?dubbo
????port:?20889??consumer:????version:?1.0spring:??application:????name:?ilss-consumer
  • consumer的啟動類

package?io.ilss.dubbo;import?org.springframework.boot.SpringApplication;import?org.springframework.boot.autoconfigure.SpringBootApplication;/**
?*?className?IlssConsumerApplication
?*?description?IlssConsumerApplication
?*
?*?@author?feng
?*?@version?1.0
?*?@date?2019-01-30?18:18
?*/@SpringBootApplicationpublic?class?IlssConsumerApplication?{????public?static?void?main(String[]?args)?{
????????SpringApplication.run(IlssConsumerApplication.class);
????}
}

服務實現

  • service接口:在service中新建interface ?TestService


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置



    代碼如下:

package?io.ilss.dubbo.service;/**
?*?className?TestService
?*?description?TestService
?*
?*?@author?feng
?*?@version?1.0
?*?@date?2019-01-30?18:07
?*/public?interface?TestService?{????String?test(String?text);
}
  • 在provider中新建ServiceImpl


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


代碼如下:

package?io.ilss.dubbo.service.impl;import?com.alibaba.dubbo.config.annotation.Service;import?io.ilss.dubbo.service.TestService;/**
?*?className?TestServiceImpl
?*?description?TestServiceImpl
?*
?*?@author?feng
?*?@version?1.0
?*?@date?2019-01-30?18:08
?*/@Service(
????????version?=?"${dubbo.provider.version}",
????????application?=?"${dubbo.application.id}",
????????protocol?=?"${dubbo.protocol.id}",
????????registry?=?"${dubbo.registry.id}")public?class?TestServiceImpl?implements?TestService?{????@Override
????public?String?test(String?text)?{????????return?"Success!?Your?String?is?"?+?text;
????}
}
  • Consumer中添加測試的TestController

package?io.ilss.dubbo.web;import?com.alibaba.dubbo.config.annotation.Reference;import?io.ilss.dubbo.service.TestService;import?org.slf4j.Logger;import?org.slf4j.LoggerFactory;import?org.springframework.web.bind.annotation.GetMapping;import?org.springframework.web.bind.annotation.RequestParam;import?org.springframework.web.bind.annotation.RestController;/**
?*?className?TestController
?*?description?TestController
?*
?*?@author?feng
?*?@version?1.0
?*?@date?2019-01-30?18:19
?*/@RestControllerpublic?class?TestController?{????private?static?final?Logger?logger?=?LoggerFactory.getLogger(TestController.class);????@Reference
????private?TestService?testService;????@GetMapping("/test")????public?String?test(@RequestParam?String?text)?{
????????logger.info("text?value?:?{}",text);????????return?testService.test(text);
????}
}
  • 完整結構


    SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


  • 運行

  1. 啟動Zookeeper端口為2181,怎么安裝啟動自行Google

  2. 啟動IlssProviderApplication

  3. 啟動IlssConsumerApplication

  4. 游覽器訪問http://localhost:8080/test?text=abc 看到如下頁面和log即完成

SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


SpringBoot2整合dubbo使用Zookeeper作為注冊中心,使用yml配置


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

砚山县| 太谷县| 建德市| 浪卡子县| 万源市| 孟津县| 溆浦县| 望奎县| 肃北| 吉林省| 文昌市| 乌海市| 星子县| 临洮县| 固安县| 莆田市| 亚东县| 日喀则市| 高雄县| 抚宁县| 和林格尔县| 民乐县| 阿鲁科尔沁旗| 灵川县| 南雄市| 鹤峰县| 蓬莱市| 鄂伦春自治旗| 嘉善县| 托克托县| 西青区| 拉孜县| 兴城市| 榆中县| 榕江县| 靖宇县| 永顺县| 右玉县| 沭阳县| 蒲江县| 武功县|