1、服务接口单独定义一个bundle(jar)
新建一个service maven工程
Order:
public class Order implements Serializable { public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } private String orderId; private String orderName; }
IOrder 服务接口
public interface IOrder { public Order findOrder(String orderId); }
2、再新建一个maven 工程,结构如下:
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-produce</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-produce</name> <description>demo-produce</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 实现web功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>demo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <!-- zk的依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.1</version> <type>pom</type> </dependency> </dependencies> <build> <finalName>demo-produce</finalName> <!-- 打包项目 mvn clean package --> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.demo.DemoProduceApplication</mainClass><!--这个要改成自己的入口类--> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </plugin> </plugins> </build> </project> IOrder实现类:OrderService (注意:这里的@Service 是dubbo里的注解,并不是spring里的注解)
import com.alibaba.dubbo.config.annotation.Service; import com.example.demo.model.Order; import com.example.demo.service.IOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Service public class OrderService implements IOrder { Logger logger = LoggerFactory.getLogger(OrderService.class); @Override public Order findOrder(String orderId) { Order order = new Order(); order.setOrderId(orderId); order.setOrderName("redmi note 8 "); logger.info("----------------orderService11111 findOrder param {} ",orderId); return order; } } application.properties 内容:
# 项目contextPath server.context-path=/demo-produce # session最大超时时间(分钟),默认为30 server.session-timeout=60 # tomcat最大线程数,默认为200 server.tomcat.max-threads=100 # tomcat的URI编码 server.tomcat.uri-encoding=UTF-8 debug=true logging.level.root=debug server.port=8082 spring.application.name=demo-produce #Dubbo provider configuration dubbo.application.name= cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc dubbo.registry.protocol=zookeeper dubbo.registry.address=zookeeper://XXX.XXX.XXX.XXX:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #指定注册到zk上超时时间,ms dubbo.registry.timeout=10000 #扫描注解包通过该设置将服务注册到zookeeper dubbo.scan.base-packages=com.example.demo.produce
启动 demo-produce APP入口
@SpringBootApplication @EnableDubbo public class DemoProduceApplication { static Logger logger = LoggerFactory.getLogger(DemoProduceApplication.class); public static void main(String[] args) { SpringApplication.run(DemoProduceApplication.class, args); logger.debug("DemoProduceApplication 启动完毕!"); } }
新建一个消费者maven
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-produce</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-produce</name> <description>demo-produce</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 实现web功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>demo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <!-- zk的依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.1</version> <type>pom</type> </dependency> </dependencies> <build> <finalName>demo-produce</finalName> <!-- 打包项目 mvn clean package --> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.demo.DemoProduceApplication</mainClass><!--这个要改成自己的入口类--> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </plugin> </plugins> </build> </project>
新建 OrderController.java (注意:这里服务消费者引用dubbo服务提供者里的服务,要用 @Reference注解)
import com.alibaba.dubbo.config.annotation.Reference; import com.example.demo.model.Order; import com.example.demo.service.IOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/order") public class OrderController { Logger logger = LoggerFactory.getLogger(OrderController.class); @Reference private IOrder order; @ResponseBody @RequestMapping("/testOrder") public Order findOrderById(@RequestParam("id") String userId) { logger.info("controller {}","OrderController"); return order.findOrder(userId); }; }
application.properties
server.context-path=/demo-consume server.port=8000 #应用名称 spring.application.name=demo-consume #注册中心地址 dubbo.registry.address=zookeeper://XXX.XXX.XXX.XXX:2181 #dubbo.registry.address=zookeeper://127.0.0.1:2181 #连接zk的超时时间,ms dubbo.registry.timeout=10000 dubbo.scan.base-packages=com.example.demo dubbo.consumer.check=false
服务消费者 启动入口
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class DemoConsumeApplication { static Logger logger = LoggerFactory.getLogger(DemoConsumeApplication.class); public static void main(String[] args) { SpringApplication.run(DemoConsumeApplication.class, args); logger.debug("DemoConsumeApplication 启动完毕!"); } }
访问服务:http://127.0.0.1:8000/order/testOrder?id=1
通过阿里提供的springboot版 dubbo-admin监控包 查看
来源:oschina
链接:https://my.oschina.net/u/3436104/blog/4864022