一、创建springboot_dubbo_provider项目
1 创建service层接口
public interface IDoSomeService { public String sayHi(); }
2 创建service层接口实现类
//利用Dubbo暴露出一个接口 @Service(interfaceClass=IDoSomeService.class) @Component public class IDoSomeServiceImpl implements IDoSomeService { @Override public String sayHi() { System.out.println("生产者IDoSomeService中sayHi方法"); return "springboot dubbo"; } }
3 编写application.properties文件
spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880
4 启动程序
@SpringBootApplication @EnableDubboConfiguration public class StartDubboProvider { public static void main(String[] args) { SpringApplication.run(StartDubboProvider.class,args); System.out.println("Dubbo接口暴露成功"); } }
5 控制台
二、创建springboot_dubbo_consumer项目
1 创建service层接口
@SpringBootApplication @EnableDubboConfiguration public class StartDubboConsumer { public static void main(String[] args) { SpringApplication.run(StartDubboConsumer.class,args); } }
2 创建application.properties文件
spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 server.port=8081
3 创建Controlle层
@RestController public class IDoSomeController { @Reference private IDoSomeService iDoSomeService; @RequestMapping("/dubbo") public String dubbo(){ String s = iDoSomeService.sayHi(); return s; } }
4 启动程序
@SpringBootApplication @EnableDubboConfiguration public class StartDubboConsumer { public static void main(String[] args) { SpringApplication.run(StartDubboConsumer.class,args); } }
5 运行页面
6 控制台效果