2019-11-17 yls
Create a service interface module
Interface Engineering provides only the interface does not provide implementation, use the back of providers and consumers
just need to write the specific implementation class module of the interface, to avoid duplication of writing in each module Interface
Introducing dependencies in the interface
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
Create an entity class must implement the Serializable interface, or can not transfer agreement between dubbo
@Data @AllArgsConstructor public class User implements Serializable { private String name; }
Creating interfaces
public interface UserService { List<User> getAll(); List<User> getAll2(); }
Create a service provider
The introduction of dependence
<!--引入创建的接口服务--> <dependency> <groupId>com.yls</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--由于服务提供者不是web项目 ,只需引入spring-boot-starter,不用引入spring-boot-starter-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
Modify the configuration file
#服务名称 dubbo.application.name=provider1 #注册中心地址 dubbo.registry.address=39.97.234.52:2181,39.97.234.52:2182,39.97.234.52:2183 #注册中心类型 dubbo.registry.protocol=zookeeper #版本号 dubbo.application.version=3 # Dubbo Protocol #协议名称 dubbo.protocol.name=dubbo #服务暴露端口 dubbo.protocol.port=20880
Implements the service interface
//暴露服务 //这里的@Service是Dubbo提供的,不是spring中的 //version必填 @Service(version = "${dubbo.application.version}") @Component public class UserImpl implements UserService { @Override public List<User> getAll() { User user1 = new User("张三"); User user2 = new User("lisi"); List<User> list = Arrays.asList(user1, user2); return list; } }
Start Service
//@EnableDubbo等价于在配置文件中配置dubbo.scan.base-packages //扫描实现类所在的包,注册Bean @EnableDubbo @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class,args); } }
Creating consumer
The introduction of dependence
<!--消费者是web项目--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!--引入创建的接口服务--> <dependency> <groupId>com.yls</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Modify the configuration file
#注册中心 dubbo.registry.address=39.97.234.52:2181,39.97.234.52:2182,39.97.234.52:2183 dubbo.registry.protocol=zookeeper #dubbo应用名称 dubbo.application.name=consumer1
Implement consumer interfaces
//服务消费者的Service是spring的 @Service public class OrderImpl implements OrderService { //使用dubbo提供的@Reference访问远程服务 //version对应服务提供者的version @Reference(version = "3") private UserService userService; @Override public List<User> init() { List<User> list = userService.getAll(); list.forEach(item -> System.out.println(item.getName())); return list; } }
Create a controller
@Controller public class OrderController { @Autowired private OrderService orderService; @ResponseBody @RequestMapping("/init") public List<User> init() { return orderService.init(); } }
Start service consumers
@EnableDubbo @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
dubbo related supplement
dubbo use the local cache mode, if the registration centers all down, dubbo can work, even if there is no registry, dubbo also through direct communication dubbo
//使用dubbo提供的@Reference访问远程服务 //version对应服务提供者的version //url:如果没有注册中心,可以通过url配置服务提供者的dubbo协议端口,进行dubbo直连 @Reference(version = "3",url ="127.0.0.1:20880" ) private UserService userService;