dubbo hello(无注册中心)
服务提供者
依赖
<dependency> <groupId>org.example</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency>
提供者实现类
重点是DubboService注解
@DubboService public class ProductServiceImpl implements ProductService { @Override public String getNameById(int id) { System.out.println("wo bei getNameById -----"); return id + "adfa"; } }
配置文件
服务名字靠spring.application.name来配置,是必须配置的,端口可以不用配置,默认是20880的
spring: application: name: product-provider # 下面的配置是不用注册中心的,重点是那个N/A dubbo: scan: base-packages: com.nf.xxx.service.impl protocol: name: dubbo # port: 20888 registry: address: N/A
启动类
@SpringBootApplication public class ProductServiceApplication { public static void main(String[] args) { SpringApplication.run(ProductServiceApplication.class, args); } }
服务消费者
依赖
<dependency> <groupId>org.example</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency>
消费者
重点是DubboReference注解的url设置
@Service public class OrderServiceImpl { // @Autowired @DubboReference(url = "dubbo://127.0.0.1:20880") private ProductService productService; public void genereateOrder() { String name = productService.getNameById(1); System.out.println("生成订单,产品名字是:" + name); } }
配置文件
spring: application: name: order-consumer
启动类
@SpringBootApplication public class OrderConsumerApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(OrderConsumerApplication.class, args); } @Autowired private OrderServiceImpl orderService; @Override public void run(String... args) throws Exception { orderService.genereateOrder(); } }
序列化
默认使用的是hessien2,要求类需要实现Serializable接口
public class User implements Serializable { private int id; private String name; //省略 }
接口就可以直接使用,其它地方不需要改变
public interface ProductService2 { User getUser(User user); }
zookeeper注册中心
采用了zookeeper作为注册中心,代码变化不大,就是提供者与消费者配置需要调整一下
依赖
提供者与消费者都需要与zookeeper通信,所以都需要添加下面的依赖
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.8</version> <type>pom</type> </dependency>
提供者配置
提供者只是更改一下配置,代码不需要任何改动
spring: application: name: product-provider dubbo: scan: base-packages: com.nf.xxx.service.impl registry: address: zookeeper://127.0.0.1:2181
消费者配置
配置中指定注册中心的地址
spring: application: name: order-consumer dubbo: registry: address: zookeeper://127.0.0.1:2181
消费者代码
只需要添加注解,不需要指定url之类的东西,直接到注册中心找提供者信息
@DubboReference() private ProductService2 productService2;
负载均衡
单机多提供者实例
在idea中直接复制启动配置,并在启动配置环境变量下配置如下内容即可
dubbo.protocol.name=dubbo;dubbo.protocol.port=20881
如果实例运行在不同的机器上是不需要上面的操作的,直接就启动多个实例并注册到注册中心去
负载均衡策略
消费者默认采用的是随机策略,可以在DubboReference注解中调整
@DubboReference(loadbalance = "roundrobin") private ProductService productService;
loadbalance值有如下几个
-
random
-
roundrobin
-
leastactive