springboot整合dubbo

安装Zookeeper

安装jdk

  1. 下载jdk-8u171-linux-x64.rpm

  2. 安装 rpm -ivh jdk-8u171-linux-x64.rpm,会自动安装到usr目录中的java中

  3. 通过pwd命令,查看jdk安装路径:/usr/java/jdk1.8.0_171-amd64

  4. 配置环境变量: vi /etc/profile,在文件最后追加:

    export JAVA_HOME=/usr/java/jdk1.8.0_171-amd64
    export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. 刷新环境变量:source /etc/profile


安装zookeeper

docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v $PWD/data:/data $PWD/conf:/conf --name zookeeper --restart always zookeeper

语法就不多说了,注意数据卷挂载就行

springboot整合dubbo_第1张图片

在idea中可以下载一个zookeeper的插件


查看注册的服务(在容器内)

./zkCli.sh 
ls /dubbo

springboot整合案例

服务提供方

依赖

<dependency>
    <groupId>org.apache.dubbogroupId>
    <artifactId>dubbo-spring-boot-starterartifactId>
    <version>2.7.7version>
dependency>

<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-recipesartifactId>
    <version>2.13.0version>
dependency>

<dependency>
    <groupId>com.101tecgroupId>
    <artifactId>zkclientartifactId>
    <version>0.11version>
dependency>

配置

server:
  port: 8085
dubbo:
  application:
    name: provider-service
  registry:
    address: zookeeper://ip:2181
    timeout: 100000
  scan:
    base-packages: top.codekiller.test.dubbo.Service
  config-center:
    timeout: 1000000

启动器

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}


服务

@Component
@DubboService
public class TicketServiceImpl implements ITicketService {
    @Override
    public String getTicket() {
        return "《战狼,冲击波!》";
    }
}

服务消费方

依赖

<dependency>
    <groupId>org.apache.dubbogroupId>
    <artifactId>dubbo-spring-boot-starterartifactId>
    <version>2.7.7version>
dependency>

<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-recipesartifactId>
    <version>2.13.0version>
dependency>

<dependency>
    <groupId>com.101tecgroupId>
    <artifactId>zkclientartifactId>
    <version>0.11version>
dependency>

配置

server:
  port: 8086
dubbo:
  application:
    name: consumer-service
  registry:
    address: zookeeper://ip:2181
    timeout: 100000

服务api

springboot整合dubbo_第2张图片

一定要注意,api的路径要相同!!!!看一下zookeeper中注册的服务应该就能明白!我被坑惨了!


服务

@Service
public class UserServiceImpl implements IUserService {

    @DubboReference
    ITicketService ticketService;

    @Override
    public void hello(){
        System.out.println("买到票了 "+ticketService.getTicket());
    }
}

测试

@SpringBootTest
class DubboConsumerApplicationTests {

    @Autowired
    private IUserService userService;

    @Test
    void contextLoads() {
        userService.hello();
    }

}

springboot整合dubbo_第3张图片

你可能感兴趣的:(java,spring,后台工具,zookeeper,java,dubbo,springboot,spring)