1、运行dubbo的三种方式
2、与springboot整合
2.1、服务提供者provider
2.2、服务消费者consumer
3、SpringBoot与dubbo整合的三种方式
1、运行dubbo的三种方式 <--返回目录
1)使用tomcat启动dubbo服务(打成war包)
2)使用main主线程启动dubbo服务
public class MainApp { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml"); ioc.start(); System.in.read(); } }
3)使用dubbo内置main打包jar启动
2、与springboot整合 <--返回目录
参考上一篇博客:搭建注册中心和管理后台
启动zookeeper;启动dubbo-admin(通过localhost:7001访问)
2.1、服务提供者provider <--返回目录
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oy</groupId> <artifactId>boot-user-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-user-service-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>com.oy</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.oy.MainApp</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
application.properties
dubbo.application.name=boot-user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#dubbo.monitor.protocol=registry
MainApp
package com.oy; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo //开启基于注解的dubbo功能 @SpringBootApplication public class MainApp { public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } }
UserServiceImpl
package com.oy.user.service; import com.alibaba.dubbo.config.annotation.Service; import com.oy.bean.UserAddress; import com.oy.service.UserService; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; @Service @Component public class UserServiceImpl implements UserService { @Override public List<UserAddress> getUserAddressList(String s) { UserAddress address1 = new UserAddress(1, "测试地址1", "1", "李老师", "135xxx", "Y"); UserAddress address2 = new UserAddress(2, "测试地址2", "1", "王老师", "139xxx", "N"); return Arrays.asList(address1,address2); } }
2.2、服务消费者consumer <--返回目录
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oy</groupId> <artifactId>boot-order-service-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-order-service-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>com.oy</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <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</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.oy.bootorderserviceconsumer.BootOrderServiceConsumerApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
application.properties
server.port=8081
dubbo.application.name=boot-order-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#dubbo.monitor.protocol=registry
MainApp
package com.oy; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class MainApp { public static void main(String[] args) { SpringApplication.run(MainApp.class, args); } }
OrderServiceImpl
package com.oy.order.service; import com.alibaba.dubbo.config.annotation.Reference; import com.oy.bean.UserAddress; import com.oy.service.OrderService; import com.oy.service.UserService; import org.springframework.stereotype.Component; import java.util.List; @Component public class OrderServiceImpl implements OrderService { @Reference private UserService userService; @Override public List<UserAddress> initOrder(String userId) { System.out.println("用户id:" + userId); //1、查询用户的收货地址 List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList; } }
OrderController
package com.oy.order.controller; import com.oy.bean.UserAddress; import com.oy.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class OrderController { @Autowired private OrderService orderService; @GetMapping("/initOrder") public List<UserAddress> initOrder(String userId) { return orderService.initOrder(userId); } }
启动provider和consumer。通过dubbo-admin查看启动情况。
访问 localhost:8081/initOrder
3、SpringBoot与dubbo整合的三种方式 <--返回目录
1)、导入dubbo-starter,在application.properties配置属性,使用@Service【暴露服务】使用@Reference【引用服务】
2)、保留dubbo xml配置文件;
导入dubbo-starter,使用@ImportResource导入dubbo的配置文件即可
3)、使用注解API的方式:
将每一个组件(指dubbo:xxx这种标签)手动创建到容器中,让dubbo来扫描其他的组件
配置参考 MyDubboConfig
package com.oy.gmall.config; import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.MethodConfig; import com.alibaba.dubbo.config.MonitorConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.ProviderConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.ServiceConfig; import com.atguigu.gmall.service.UserService; @Configuration public class MyDubboConfig { @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("boot-user-service-provider"); return applicationConfig; } //<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry> @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setProtocol("zookeeper"); registryConfig.setAddress("127.0.0.1:2181"); return registryConfig; } //<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol> @Bean public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20882); return protocolConfig; } /** *<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0"> <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> </dubbo:service> */ @Bean public ServiceConfig<UserService> userServiceConfig(UserService userService){ ServiceConfig<UserService> serviceConfig = new ServiceConfig<>(); serviceConfig.setInterface(UserService.class); serviceConfig.setRef(userService); serviceConfig.setVersion("1.0.0"); //配置每一个method的信息 MethodConfig methodConfig = new MethodConfig(); methodConfig.setName("getUserAddressList"); methodConfig.setTimeout(1000); //将method的设置关联到service配置中 List<MethodConfig> methods = new ArrayList<>(); methods.add(methodConfig); serviceConfig.setMethods(methods); //ProviderConfig //MonitorConfig return serviceConfig; } }
---