1. boot-dubbo-api:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jcx.boot.dubbo.demo</groupId> <artifactId>boot-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> </dependencies> </project>
dao:
package com.jcx.boot.dubbo.demo.dao; import lombok.*; import java.io.Serializable; @Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString public class User implements Serializable { private Integer id; private String name; private Integer age; private String address; }
Service:
package com.jcx.boot.dubbo.demo.service; import com.jcx.boot.dubbo.demo.dao.User; import java.util.List; public interface IUserService { List<User> getUser(Integer id); }
package com.jcx.boot.dubbo.demo.service; import com.jcx.boot.dubbo.demo.dao.User; import java.util.List; public interface IOrderService { List<User> initUser(Integer id); }
2. boot-dubbo-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jcx.boot.dubbo.demo</groupId> <artifactId>boot-dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <name>boot-dubbo-provider</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.jcx.boot.dubbo.demo</groupId> <artifactId>boot-dubbo-api</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
service.impl:
package com.jcx.boot.dubbo.demo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.jcx.boot.dubbo.demo.dao.User; import com.jcx.boot.dubbo.demo.service.IUserService; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; @Service @Component public class UserServiceImpl implements IUserService { public List<User> getUser(Integer id) { User user1 = new User(1, "张三", 18, "西安"); User user2 = new User(2, "李四", 20, "北京"); return Arrays.asList(user1, user2); } }
ProviderApplication:
package com.jcx.boot.dubbo.demo; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
application.properties:
dubbo.application.name=boot-dubbo-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
3. dubbo-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jcx.boot.dubbo.demo</groupId> <artifactId>boot-dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <name>boot-dubbo-consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </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.jcx.boot.dubbo.demo</groupId> <artifactId>boot-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
service.impl:
package com.jcx.boot.dubbo.demo.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.jcx.boot.dubbo.demo.dao.User; import com.jcx.boot.dubbo.demo.service.IOrderService; import com.jcx.boot.dubbo.demo.service.IUserService; import org.springframework.stereotype.Service; import java.util.List; @Service public class OrderServiceImpl implements IOrderService { @Reference IUserService userService; @Override public List<User> initUser(Integer id) { System.out.println("用户id:" + id); List<User> users = userService.getUser(id); for (User user : users) { System.out.println(user); } return users; } }
controller:
package com.jcx.boot.dubbo.demo.controller; import com.jcx.boot.dubbo.demo.dao.User; import com.jcx.boot.dubbo.demo.service.IOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class OrderController { @Autowired IOrderService orderService; @ResponseBody @RequestMapping("/init") public List<User> initOrder(@RequestParam("id") Integer id) { List<User> users = orderService.initUser(id); return users; } }
ConsumerApplication:
package com.jcx.boot.dubbo.demo; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
application.properties:
server.port=8081
dubbo.application.name=boot-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
浏览器访问:localhost:8081/init?id=1
页面出现json结果:[{"id":1,"name":"张三","age":18,"address":"西安"},{"id":2,"name":"李四","age":20,"address":"北京"}]