<?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>top.free</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.free</groupId>
<artifactId>dubboserviceimpl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--依赖dubboservice接口-->
<dependency>
<groupId>top.free</groupId>
<artifactId>dubboservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--dubbo与springboot整合依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- zookeeper依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(2) xml文件配置 provider.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
">
<!-- 发布服务到注册中心 -->
<!-- 为服务起一个别名 -->
<dubbo:application name="provider" />
<!--address 注册中心的地址 protocol 指的是注册中心的协议的格式 -->
<dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/>
<!-- interface告诉注册中心是哪个类型
ref说明具体是哪个服务
timeout连接超时的时间
-->
<dubbo:service interface="top.free.dubboservice.TestDubboService" ref="serviceImpl" timeout="60000"></dubbo:service>
<!--, 配置端口,协议是dubbo,消费者消费的时候必须通过端口来消费,端口可以随便写,但是不能被占用,一个dubbo,独占一个端口 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
</beans>
(3) 配置实现类ServiceImpl:
package top.free.dubboserviceimpl;
import org.springframework.stereotype.Component;
import top.free.dubboservice.TestDubboService;
@Component
public class ServiceImpl implements TestDubboService {
@Override
public String getData(String user) {
return "你好,我们通信了:"+user;
}
}
(4)主启动类上加上:@ImportResource(locations = "classpath:provider.xml")
package top.free;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = "classpath:provider.xml")
public class DubboserviceimplApplication {
public static void main(String[] args) {
SpringApplication.run(DubboserviceimplApplication.class, args);
}
}
(5)在application.properties中加上项目启动的端口号: server.port=8080
3.2、配置dubboweb项目(消费者)
<?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>top.free</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.free</groupId>
<artifactId>dubboweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubboweb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>top.free</groupId>
<artifactId>dubboservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(2)配置controller:
package top.free.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.free.dubboservice.TestDubboService;
@RestController
public class TestDubboWeb {
//serviceImpl会显示红色,这个是因为编译时找不到bean注入,不影响结果
@Autowired
private TestDubboService servcieImpl;
@RequestMapping("/test/{user}")
public String getData(@PathVariable("user")String user){
String data =servcieImpl.getData(user);
return data;
}
}
(3)配置消费者的consumer.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
">
<!-- 消费者起一个别名 -->
<dubbo:application name="consumer"/>
<!--address 注册中心的地址 protocol 指的是注册中心的协议的格式 -->
<dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/>
<!-- 告诉注册中心我需要什么 -->
<dubbo:reference interface="top.free.dubboservice.TestDubboService" id="serviceImpl"></dubbo:reference>
</beans>
(4)在主启动类上加:@ImportResource(locations = "classpath:consumer.xml")
package top.free;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = "classpath:consumer.xml")
public class DubbowebApplication {
public static void main(String[] args) {
SpringApplication.run(DubbowebApplication.class, args);
}
}
(5)在application.properties文件中加上启动的端口号:server.port=8081
(7)、首先在本地安装这三个项目:
package top.free.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DubboProviderConfig {
/*相当于xml中服务提供者的别名:<dubbo:application name="provider" />*/
@Bean
public ApplicationConfig myApplicationConfig(){
ApplicationConfig ac = new ApplicationConfig();
ac.setName("provider3");
return ac;
}
/*相当于注册中心配置:<dubbo:registry address="XXXXXXXX:2181" protocol="zookeeper"/>*/
@Bean
public RegistryConfig myregistryConfig(){
RegistryConfig rc = new RegistryConfig();
rc.setAddress("XXXXXXXX:2181");
rc.setProtocol("zookeeper");
return rc;
}
/*相当于暴露服务的协议以及端口:<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>*/
@Bean
public ProtocolConfig myProtocolConfig(){
ProtocolConfig pc = new ProtocolConfig();
pc.setName("dubbo");
pc.setPort(20888);
return pc;
}
/*在实现类上加注解来代替:暴露的服务<dubbo:service interface="top.free.dubboservice.TestDubboService" ref="serviceImpl" timeout="60000"></dubbo:service>*/
}
package top.free.config; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.RegistryConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class DubboConsumerConfig { /*相当于consumer.xml中的:<dubbo:application name="consumer"/>*/ @Bean public ApplicationConfig myapplicationConfig(){ ApplicationConfig ac = new ApplicationConfig(); ac.setName("consumer3"); return ac; } /*相当于:<dubbo:registry address="39.108.125.227:2181" protocol="zookeeper"/>*/ @Bean public RegistryConfig myregistryConfig(){ RegistryConfig rc = new RegistryConfig(); rc.setAddress("39.108.125.227:2181"); rc.setProtocol("zookeeper"); return rc; } /*使用注解方式来代替:<dubbo:reference interface="top.free.dubboservice.TestDubboService" id="serviceImpl"></dubbo:reference>*/ }
#别名
dubbo.application.name=prodiver2
#注册中心zookeeper地址
dubbo.registry.address=XXXXXXXX:2181
dubbo.registry.protocol=zookeeper
#暴露的服务端口与协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
server.port=8080
#消费者别名
dubbo.application.name=consumer
#注册中心zookeeper地址与协议
dubbo.registry.address=zookeeper://39.108.125.227:2181
dubbo.registry.protocol=zookeeper
server.port=8081
原文:https://www.cnblogs.com/YpfBolg/p/10886740.html