一、创建发布者项目
dubbo-boot-service-provider
选定 springboot的模板创建
填入参数
这边暂时不选任何
接着下一步‘完成
1. 引用依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.6.2 com.hhm dubbo-boot-service-provider0.0.1-SNAPSHOT dubbo-boot-service-provider dubbo的发布者 jar 1.8 2.6.2 2.7.8 true org.springframework.boot spring-boot-dependencies${spring-boot.version} pom import org.apache.dubbo dubbo-dependencies-bom${dubbo.version} pom import org.apache.dubbo dubbo${dubbo.version} org.springframework springjavax.servlet servlet-apilog4j log4jorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.example dubbo-demo-service1.0-SNAPSHOT org.apache.dubbo dubbo-spring-boot-starter2.7.8 org.apache.curator curator-frameworkorg.apache.curator curator-recipesorg.apache.zookeeper zookeeperapache.snapshots.https Apache Development Snapshot Repository https://repository.apache.org/content/repositories/snapshots false true org.apache.maven.plugins maven-surefire-plugin2.22.2 true
2. 配置 application.properties ,这边配置跟 spring的配置一样
dubbo.application.name=dubbo-boot-service-provider dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1 dubbo.registry.port=2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20881 dubbo.monitor.protocol=registry server.port=8081
3. 创建实现及调用
package com.hhm.gmall.service.impl; import com.hhm.gmall.bean.UserAddress; import com.hhm.gmall.service.UserService; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Value; import java.util.Arrays; import java.util.List; @DubboService(version = "1.0.0") public class UserServiceImpl implements UserService { @Value("${dubbo.application.name}") private String serviceName; @Override public ListgetUserAddressList(String userId) { System.out.println("serviceName=="+serviceName); UserAddress userAddress1 = new UserAddress(1, "厦门市思明区软件园二期科技大楼3层", "1", "李老师", "13754698745", "Y"); UserAddress userAddress2 = new UserAddress(2, "广州市思明区软件园二期科技大楼3层", "1", "姜老师", "13754567425", "N"); return Arrays.asList(userAddress1,userAddress2); } }
package com.hhm.gmall; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; //@SpringBootApplication @EnableDubbo @EnableAutoConfiguration public class DubboBootServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboBootServiceProviderApplication.class, args); } }
@EnableAutoConfiguration 这个包含了 @SpringBootApplication
执行启动,调用监控发现服务已经暴露了
可以点击测试
控制台也有自己打印的,也侧面说明调用到
二、创建消费者项目 dubbo-boot-service-consumer
1. 配置 pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.6.2 com.hhm dubbo-boot-service-consumer0.0.1-SNAPSHOT dubbo-boot-service-consumer dubbo的订阅者 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.example dubbo-demo-service1.0-SNAPSHOT org.apache.dubbo dubbo-spring-boot-starter2.7.8 org.apache.curator curator-framework4.0.1 org.apache.curator curator-recipes4.0.1 org.springframework.boot spring-boot-maven-plugin
2. 配置 application.properties
dubbo.application.name=dubbo-boot-service-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.monitor.protocol=registry server.port=8084
3. 实现方法及控制器
package com.hhm.gmall.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.hhm.gmall.bean.UserAddress; import com.hhm.gmall.service.OrderService; import com.hhm.gmall.service.UserService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.stereotype.Service; import java.util.List; @Service public class OrderServiceImpl implements OrderService { //1@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20881") @DubboReference(version = "1.0.0") UserService userService; @Override public ListinitOrder(String userId) { System.out.println("用户id==" + userId); List addressList = userService.getUserAddressList(userId); for (UserAddress userAddress : addressList) { System.out.println(userAddress); } return addressList; } }
package com.hhm.gmall.controller; import com.hhm.gmall.bean.UserAddress; import com.hhm.gmall.service.OrderService; 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 OrderService orderService; @ResponseBody @RequestMapping("/initOrder") public ListinitOrder(@RequestParam("uid") String userId) { return orderService.initOrder(userId); } }
package com.hhm.gmall; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo public class DubboBootServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboBootServiceConsumerApplication.class, args); } }
=====================================================
如果需要指定单独方法的配置
@DubboReference(version = "1.0.0",methods = {@Method(name="getUserAddressList",timeout = 4000)}) UserService userService;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)