springboot整合最新版dubbo以及dubbo-admin的安裝


springboot整合最新版dubbo以及dubbo-admin的安裝

 

一、安裝前准備

  由於dubbo被阿里捐獻給了apache,這次安裝admin時,參考網上的資料,地址還是停留在之前的鏈接,踩了不少坑,這里記錄下。

  dubbo-admin下載地址:

  地址一:https://github.com/apache/incubator-dubbo/releases 

    該地址2.6版本以上的包中沒有dubbo-admin ,2.5x版本的有

  地址二:https://github.com/apache/incubator-dubbo-ops

    該地址中的dubbo-admin模塊被單獨拎出來了,springboot方式啟動,可以直接運行main方法,或者使用 java -jar 方式啟動,很方便,有github賬號的可以fork一下,推薦使用這個版本,本文介紹的就是該版本  。

二、dubbo-spring-boot-starter的使用

  github地址:https://github.com/alibaba/dubbo-spring-boot-starter 最新版本為2.0 ,本文也是使用的該版本。

2.1、添加依賴

1
2
3
4
5
<dependency>
     <groupId>com.alibaba.spring.boot</groupId>
     <artifactId>dubbo-spring-boot-starter</artifactId>
     <version> 2.0 . 0 </version>
</dependency>

2.2、新建接口模塊

  目錄結構

  接口類

1
2
3
4
5
6
7
8
9
package  com.dc.sb.service;
 
public  interface  RemoteUserService {
 
 
     String sayHello(String name);
 
 
}

2.3、新建provider模塊

  目錄結構

  接口實現類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package  com.dc.sb.dubbo.provider.service.user;
 
import  com.alibaba.dubbo.config.annotation.Service;
import  com.dc.sb.service.RemoteUserService;
import  org.slf4j.Logger;
import  org.slf4j.LoggerFactory;
import  org.springframework.stereotype.Component;
 
/**
  * dubbo 服務service
  *
  * @author DUCHONG
  * @since 2018-07-03 18:29
  **/
@Component
@Service (version =  "1.0.0" ,timeout =  10000 ,interfaceClass = RemoteUserService. class )
public  class  RemoteUserServiceImpl  implements  RemoteUserService {
 
     private  static  final  Logger logger = LoggerFactory.getLogger(RemoteUserServiceImpl. class );
 
 
     @Override
     public  String sayHello(String name) {
 
         return  "Hello " +name;
     }
}

  配置文件

1
2
3
4
5
6
server.port= 8081
server.context-path=/
 
spring.application.name=dubbo-spring-boot-starter
spring.dubbo.server= true
spring.dubbo.registry=zookeeper: //127.0.0.1:2181

  啟動類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package  com.dc.sb.dubbo.provider;
 
import  com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@EnableDubboConfiguration
public  class  SbDubboProviderApplication {
 
     public  static  void  main(String[] args) {
         SpringApplication.run(SbDubboProviderApplication. class , args);
     }
}

2.4、 消費者模塊

  目錄結構

  啟動類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package  com.dc.sb.web;
 
import  com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import  com.dc.sb.config.DruidProperties;
import  org.mybatis.spring.annotation.MapperScan;
import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
import  org.springframework.boot.context.properties.EnableConfigurationProperties;
import  org.springframework.context.annotation.ComponentScan;
 
@SpringBootApplication
@EnableConfigurationProperties ({DruidProperties. class })
@MapperScan (basePackages =  "com.dc.sb.dao" )
@ComponentScan ( "com.dc.sb.*" )
@EnableDubboConfiguration
public  class  SbWebApplication {
 
     public  static  void  main(String[] args) {
         SpringApplication.run(SbWebApplication. class , args);
     }
}

  配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#server
server.port= 8080
server.context-path=/
 
#mybatis
mybatis.type-aliases- package = com.dc.sb.dao.dataobject
mybatis.mapper-locations= classpath*:META-INF/mybatis/mapper/*.xml
 
#druid
druid.initialSize=  5
druid.minIdle=  0
druid.maxActive=  20
druid.maxWait=  6000
druid.timeBetweenEvictionRunsMillis=  60000
druid.minEvictableIdleTimeMillis=  300000
druid.validationQuery= SELECT  1  FROM DUAL
druid.testWhileIdle=  false
druid.testOnBorrow=  false
druid.testOnReturn=  false
druid.poolPreparedStatements=  true
druid.maxPoolPreparedStatementPerConnectionSize=  20
druid.filters= stat,log4j
 
#mysql
spring.datasource.driver- class -name= com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql: //127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&allowMultiQueries=true
spring.datasource.username= root
spring.datasource.password=root
 
#redis
spring.redis.host= 127.0 . 0.1
spring.redis.port= 6379
spring.redis.password=redis
 
#dubbo
spring.application.name=dubbo-spring-boot-starter
spring.dubbo.registry=zookeeper: //127.0.0.1:2181

  引用端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package  com.dc.sb.web.controller.remote;
 
 
import  com.alibaba.dubbo.config.annotation.Reference;
import  com.dc.sb.service.RemoteUserService;
import  org.springframework.web.bind.annotation.PathVariable;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RestController;
 
/**
  * dubbo消費者controller
  * @author DUCHONG
  * @since 2018-07-03 18:44
  **/
@RestController
public  class  RemoteUserController {
 
     //timeout 可以不指定,但是version一定要指定 不然會找不到服務 直連需要加url="dubbo://localhost:20880"
     @Reference (version =  "1.0.0" )
     private  RemoteUserService remoteUserService;
 
 
     @RequestMapping (value= "/dubbo/say/{name}" )
     public  String sayHello( @PathVariable ( "name" ) String name){
 
         String result=remoteUserService.sayHello(name);
         return  result;
     }
 
 
}

三、dubbo-admin的啟動

下載地址二的包,直接在idea中打開,結構如下:

install完成之后,直接右鍵DubboAdminApplication  Run 

修改配置文件,位置在 dubbo-admin /src/main/resource下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#
# 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

根據自己的需要修改,包括dubbo-admin用戶名密碼,當然zk的配置也是少不了的,具體怎么安裝zk,啟動以及配置,這里就不多說了,

默認的的用戶名和密碼為:

1
2
3
4
5
root
root
 
guest
guest

3.1、訪問

localhost:7001

 首頁

 service

 provider

  consumer

 

完整代碼已上傳github 歡迎fork  傳送門

沉得住氣,沉淀自己。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM