Spring-Cloud-Alibaba与Dubbo的集成

前言

为什么要集成Dubbo

在我之前的文章提到过,spring-cloud-alibaba无缝的集成类feign组件,底层属于http调用,所以使用起来会非常灵活,
但是在性能上不及rpc,Dubbo正是一款高性能的rpc框架,有着良好的负载均衡与熔断降级特性,在微服务中也是一个非常流行的框架

集成方案

集成方案有多种,可以让zookeeper作为注册中心,然后把zookeeper集群集成到nacos中,或者直接让nacos作为注册中心,推荐后一种方式,因为阿里巴巴推出nacos的目的就是想代替zookeeper的方案

第一种:zookeeper作为注册中心

参考

zookeeper安装(推荐docker安装)

  • docker-compose.yaml
version: '3.1'

services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    networks:
      - zoo-net
    ports:
      - 2181:2181
    volumes:
      - zoo1-data:/data
      - zoo1-log:/datalog
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: "*"

  zoo2:
    image: zookeeper
    restart: always
    hostname: zoo2
    networks:
      - zoo-net
    ports:
      - 2182:2181
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: "*"

  zoo3:
    image: zookeeper
    restart: always
    hostname: zoo3
    networks:
      - zoo-net
    ports:
      - 2183:2181
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
      ZOO_4LW_COMMANDS_WHITELIST: "*"

volumes:
  zoo1-data:
    external: false
  zoo1-log:
    external: false  

networks:
  zoo-net:

第二种:直接以nacos作为注册中心

参考

nacos安装

点此跳转

公共接口项目

  • 就是一个简单的接口
public interface HelloService {
    public  String sayHello(String name);
}

服务提供者

  • 必要依赖
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
dependency>

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-dubboartifactId>
dependency>

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>


<dependency>
...
dependency>
  • application.yaml
dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://jd
  scan:
    base-packages: com.zhubome.democommonservice4.service
  cloud:
    subscribed-services:


spring:
  application:
    name: alibaba-dubbo-server
  cloud:
    nacos:
      discovery:
        server-addr: jd:8848
  • 实现类
import com.zhubome.sca.dubbointerface.service.HelloService;
import org.apache.dubbo.config.annotation.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
  • 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoCommonService4Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoCommonService4Application.class, args);
    }

}
  • 由于nacos默认注册内网ip,如有需要注册公网ip时,在运行jar包时把公网ip传入即可
java -jar -Dspring.cloud.nacos.discovery.ip=`hostname -i` -jar xxx.jar

前提是已做好hostname公网ip映射

服务消费者

  • 必要依赖
    与提供者同

  • application.yaml

spring:
  application:
    name: sca-consumer-service
  cloud:
    nacos:
      discovery:
        server-addr: jd:8848

server:
  port: 10107

dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://jd
  cloud:
    subscribed-services: alibaba-dubbo-server
  • 调用类
import com.zhubome.sca.dubbointerface.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Reference
    private HelloService helloService;

    @GetMapping("/dubbo")
    public String sayHello() {
        System.out.println(helloService.sayHello("SnailClimb"));
        return helloService.sayHello("world");
    }
}

前往我的博客原文

你可能感兴趣的:(微服务)