須要org.apache.dubbo.dubbo-dependencies-bom, 須要org.apache.dubbo.dubbo-spring-boot-starter, 當前版本有2.7.0和2.7.1, dubbo版本已經發布到2.7.2了, 可是starter還沒更新到2.7.2, 因此總體使用2.7.1版本html
另外就是spring-boot的org.springframework.boot.spring-boot-dependencies, org.springframework.boot.spring-boot-starter-testjava
org.apache.dubbo.dubbo中帶了springframework, javax.servlet, log4j, 在pom中要排除掉web
在項目的maven settings.xml中, 只須要配public就能夠, 不須要Apache Snapshot. 檢查一下public底下有沒有org.apache.dubbo, 沒有的話, 把Central的proxy源配置到 http://repo1.maven.org/maven2/ 這個是最源頭的倉庫了, 如今從牆裏訪問速度也不慢.redis
給項目隨便起了個名字叫DB, 這個是root的pom內容spring
<?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.rockbb</groupId> <artifactId>db</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>DB: Root</name> <modules> <module>../db-commons-api</module> <module>../db-commons-impl</module> <module>../db-admin</module> </modules> <prerequisites> <maven>3.3.9</maven> </prerequisites> <properties> <!-- Global encoding --> <project.jdk.version>1.8</project.jdk.version> <project.source.encoding>UTF-8</project.source.encoding> <!-- Global dependency versions --> <springframework.boot.version>2.1.6.RELEASE</springframework.boot.version> <dubbo.version>2.7.1</dubbo.version> <development.mode>1</development.mode> <development.developer>milton</development.developer> <timestamp>${maven.build.timestamp}</timestamp> <maven.build.timestamp.format>yyyyMMdd_HHmm</maven.build.timestamp.format> <dubbo.application.version>develop-${development.developer}</dubbo.application.version> <log.path>d:/logs</log.path> <log.level>INFO</log.level> <log.appender>stdout</log.appender> <admin.root.path></admin.root.path> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot Dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springframework.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo Dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${springframework.boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>${project.jdk.version}</source> <target>${project.jdk.version}</target> <encoding>${project.source.encoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>${project.source.encoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${springframework.boot.version}</version> </plugin> </plugins> </pluginManagement> </build> </project>
root模塊用於規定全局的版本號, 以及完整構建apache
這是用於服務端暴露接口信息給消費端的模塊, 要儘可能簡單幹淨依賴少, 避免依賴污染. 這個是api模塊的pom內容json
<?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> <parent> <groupId>com.rockbb</groupId> <artifactId>db</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../db/pom.xml</relativePath> </parent> <artifactId>db-commons-api</artifactId> <packaging>jar</packaging> <name>DB: Commons API</name> <dependencies> </dependencies> </project>
這個是服務端接口具體實現的模塊, 須要啓動服務, 註冊本身到服務註冊中心(這裏使用的是zookeeper).api
pom文件session
<?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> <parent> <groupId>com.rockbb</groupId> <artifactId>db</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../db/pom.xml</relativePath> </parent> <artifactId>db-commons-impl</artifactId> <packaging>jar</packaging> <name>DB: Commons Implementation</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.rockbb</groupId> <artifactId>db-commons-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>db-commons</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
.由於zookeeper裏帶的logger和外面的logger衝突而且版本較低, 因此將zookeeper裏的logger排除掉了. mvc
.在build模塊中, 用maven-deploy-plugin.configruation.skip: true 避免將本模塊install到本地. 由於這是最終部署用的末端模塊, 不會再有模塊依賴於這個模塊
application.yml文件
server: port: 8760 spring: application: name: db-commons dubbo: application: name: db-commons-@development.developer@ # Application name is the unique identifier of an application. It is for registry combing the dependencies of applications. Note: Consumer and provider application name should not be the same, and this parameter is not a match condition. As a suggestion, you can name it as your project name. qos: enable: false # Activate QoS or not port: 33333 # The port QoS would bind to accept: foreign: ip: false # Enable remote access or not registry: protocol: zookeeper # The protocol of the registry center. dubbo, multicast, zookeeper, redis, consul(2.7.1), sofa(2.7.2), etcd(2.7.2), nacos(2.7.2) are available. address: 127.0.0.1:2181 provider: delay: -1 # The delay time(ms) for registering services. When set to -1, it indicates that the services will expose to registry after the Spring context is initialized timeout: 30000 # The RPC timeout(ms) retries: 0 # The retry count for RPC, not including the first invoke. Please set it to 0 if don't need to retry. version: @dubbo.application.version@ protocol: id: dubbo # Bean Id of the protocol, can be referenced in <dubbo:service protocol=""> The default value is equal to the value of name attribute while id is not filled. If name value has already existed, it will add index to it's suffix. name: dubbo accepts: 500 # The maximum connection count of the service provider # dubbo協議缺省端口爲20880,rmi協議缺省端口爲1099,http和hessian協議缺省端口爲80;若是配置爲-1 或者 沒有配置port則會分配一個沒有被佔用的端口。Dubbo 2.4.0+,分配的端口在協議缺省端口的基礎上增加,確保端口段可控。 port: 10080 serialization: hessian2 # The default serialization of dubbo protocol is hessian2, rmi protocol is java, http protocol is json
ApplicationBoot.java文件
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class ApplicationBoot { public static void main(String[] args) { SpringApplication.run(ApplicationBoot.class, args); } }
DemoServiceImpl文件
import com.rockbb.db.commons.api.service.DemoService; import org.apache.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @Service @Component public class DemoServiceImpl implements DemoService { @Override public String hello(String name) { return "Hello " + name; } }
這裏用到了web, 展現層使用的是freemarker.
pom文件
<?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> <parent> <groupId>com.rockbb</groupId> <artifactId>db</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../db/pom.xml</relativePath> </parent> <artifactId>db-admin</artifactId> <packaging>jar</packaging> <name>DB: Admin</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.rockbb</groupId> <artifactId>db-commons-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>db-admin</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>*.yml</exclude> <exclude>*.xml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>*.yml</include> <include>*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
application.yml文件
server: port: 8764 servlet: session: timeout: 600 context-path: @admin.root.path@ spring: application: name: db-admin freemarker: cache: true settings: auto_import: spring.ftl as S number_format: '#' template_update_delay: 0 default_encoding: UTF-8 datetime_format: yyyy-MM-dd HH:mm:ss classic_compatible: true template_exception_handler: ignore mvc: view: prefix: /templates/ suffix: .ftl dubbo: application: name: db-admin-@development.developer@ # Application name is the unique identifier of an application. It is for registry combing the dependencies of applications. Note: Consumer and provider application name should not be the same, and this parameter is not a match condition. As a suggestion, you can name it as your project name. qos: enable: false # Activate QoS or not port: 33333 # The port QoS would bind to accept: foreign: ip: false # Enable remote access or not registry: protocol: zookeeper # The protocol of the registry center. dubbo, multicast, zookeeper, redis, consul(2.7.1), sofa(2.7.2), etcd(2.7.2), nacos(2.7.2) are available. address: 127.0.0.1:2181 consumer: version: @dubbo.application.version@ check: false
ApplicationBoot.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ApplicationBoot { public static void main(String[] args) { SpringApplication.run(ApplicationBoot.class, args); } }
DemoController.java
import com.rockbb.db.commons.api.service.DemoService; import org.apache.dubbo.config.annotation.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller public class DemoController { private static Logger logger = LoggerFactory.getLogger(DemoController.class); @Reference private DemoService demoService; @RequestMapping(value = {"/", "/index"}) public String doIndex(HttpServletRequest request) { String name = request.getParameter("name"); if (name != null) { name = demoService.hello(name); } logger.info("Name: {}", name); return "index"; } }