dubbo integrated springboot simple example

What Dubbo that?

dubbo is a high performance, lightweight open source java RPC framework.

Its three core:

Telecommunications, remote method invocation interface for

Intelligent load balancing and fault tolerance

Automatic registration and discovery services

concept

1571398361922

Service Provider (Provider): the service provider to provide services, a service provider in Qidong, the register itself as a registry service.

Service consumers (Consumer) : call the remote service consumer services, consumer services at startup, they need to subscribe to the service registry, service consumers, provide an address from the list, depending on the software load balancing algorithm, choose a Taiwan providers call, if the call fails, select another call.

Registry (Register): registry service provider address list returned to the consumer, if there is a change, the registry will be based on long push to change the data connection to the consumer.

Monitoring Center (Monitor): service consumers and providers, in memory only the cumulative number of calls and call time, the definition of statistical data transmitted once per minute to the monitoring center.

Installation dubbo monitoring

dubbo service itself is not a software package itself as a jar. Your role is to help java program to connect to the zookeeper, zookeeper achieve consumer use, to provide services.

The required tools and FIG decompression

1571399947862

1571400436979

1571400887606

The contents of the configuration file

1571401140445

Note: When you use the registry as a zookeeper, zookeeper to advance the start !

run

First run zookepper

1571401938011

Before running zookepper file, copy the configuration file and rename it. Otherwise, the start time will flash back phenomenon

1571402259886

Specific reason is the time to start looking for zoo.cfg file instead zoo_simple.cfg. File not found at the time will flash back.

start up

1571402535774

According to their own environment, select the appropriate method of operating.

After a successful run

1571402621208

Run dubbo

After zookeeper operates successfully dubbo.

1571402763399

In this case the command cmd run at jar package

1571403057192

Sign of success

1571403144915

Verify Login

1571403279962

Home login screen

1571403321450

State registry

1571403447327

Dubbo integration springboot (This is the text ah ah ah ah)

Project: as shown below

1571461003138

Among them, the entity classes and service interfaces, by consumers and service providers rely on.

Entity class service and interface architecture

1571461153816

pom.xml Code

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.bc</groupId>
  <artifactId>spring-user-interface</artifactId>
  <version>1.0</version>
  
  <properties>
        <lombok.version>1.18.8</lombok.version>
  </properties>
  <dependencies>
    <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

Domain entity class codes in packet

package com.bc.domain;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{
    private static final long serialVersionUID = 1L;
    private Integer id ;
    private String name ;
    private String address ;
}

Interface code in the service package

OrderService

package com.bc.service;

import java.util.List;

import com.bc.domain.User;

public interface OrderService {
    public List<User> initOrder(Integer id) ;
}

UserService

package com.bc.service;

import java.util.List;

import com.bc.domain.User;

public interface UserService {
    public List<User> getUserList();
}
Service providers related structures and code

1571461478088

pom.xml Code

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bc</groupId>
    <artifactId>springboot-service-provider</artifactId>
    <version>1.0</version>
    <name>springboot-service-provider</name>
    <description>springboot与dubbo的集成服务提供者(白茶弟弟)</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <dubbo.version>2.6.5</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.bc</groupId>
            <artifactId>spring-user-interface</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
          <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>   
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
         </dependency>
         <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
         </dependency>
         <!-- curator-framework -->
         <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
         </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

UserServiceImpl Code

package com.bc.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Service;
import com.bc.domain.User;
import com.bc.service.UserService;
//注意此时的service为dubbo的注解
@Service
public class UserServiceImpl implements UserService{
    
    public static List<User> user=new ArrayList<>();
    static {
        user.add(new User(1,"白菜弟弟","安徽"));
    }
    @Override
    public List<User> getUserList() {
        // TODO Auto-generated method stub
        return user;
    }
    
}

Start Class Code

package com.bc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo //注意需要这个注解
public class SpringbootServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootServiceProviderApplication.class, args);
        System.out.println("服务者启动成功");
    }
}

Profiles Code application.Properties


#Dubbo config
dubbo.application.name=springboot-service-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

So far the service provided is complete, run the startup class View

1571461867495

Browser View

1571461921644

Consumer-related part and code

1571462023873

Code

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bc</groupId>
    <artifactId>springboot-service-consumer</artifactId>
    <version>1.0</version>
    <name>springboot-service-consumer</name>
    <description>springboot与dubbo的集成服务消费者(白茶弟弟)</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <dubbo.version>2.6.5</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.bc</groupId>
            <artifactId>spring-user-interface</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
         <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>   
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
         </dependency>
         <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
         </dependency>
         <!-- curator-framework -->
         <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
         </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Profiles Code application.Properties

#Dubbo 
dubbo.application.name=springboot-service-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181

server.port=8888

OrderServiceImpl implementation class

package com.bc.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bc.domain.User;

@Service//此时的这个是spring下的
public class OrderServiceImpl implements OrderService {
    
    @Reference  //这个是dubbo下的
    private UserService userService ;
    @Override
    public List<User> initOrder(Integer id) {
        // TODO Auto-generated method stub
        return userService.getUserList();
    }
    
}

Controller

package com.bc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bc.domain.User;
import com.bc.service.OrderService;


@RestController
public class CusController {
    
    @Autowired
    private OrderService orderSrvice ;
    
    @RequestMapping("getUser")
    public List<User> getUser(){
        return this.orderSrvice.initOrder(1);
    }
}

Startup class

package com.bc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo
public class SpringbootServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootServiceConsumerApplication.class, args);
        System.out.println("消费者启动成功");
    }
}

These are the code section

Start Consumers

1571462355993

Browser View

1571462408452

1571462468130

So far it has completed.

Guess you like

Origin www.cnblogs.com/ZT-Song/p/11708025.html