springboot 集成dubbo项目使用

springboot 集成dubbo项目使用

dubbo的springboot项目使用官方dubbo-spring-boot-starter 即可,前两天刚发布0.2.0版本,支持rest风格。
关于dubbo如何使用请查看官方文档:
Dubbo用户手册(中文)
Dubbo开发手册(中文)
Dubbo管理手册(中文)
下面通过一个demo来演示如何利用dubbo-spring-boot-starter 0.2.0版本暴露两个服务,一个服务一dubbo协议暴露,另一个服务以rest服务暴露。

API

在api项目中新建两个service 接口,和一个User 类,如下图;
HelloService 代码:


public interface HelloService {
    String hello(String hello);
}

UserService 代码

@Path("/user")
public interface UserService {
    @Path("/register/{id:\\d+}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    User getUser(@PathParam("id") Long id);
}

User 类

public class User {
    private Long id;
    private String name;

    public User() {
    }

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

provider 服务提供者

application.yml 中只能定义单个协议,要定义多个协议需要通过api 来实现

pring.application.name=dubbo-provider-demo
server.port=9091
#management.server.port = 9091

demo.service.version=1.0
dubbo.application.id=dubbo-provider-demo
dubbo.application.name=dubbo-provider-demo
dubbo.scan.basePackages  = com.dubbo.springboot.provider

dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.id=registry

## Legacy QOS Config
dubbo.qos.port = 22222

# Dubbo Endpoint (default status is disable)
endpoints.dubbo.enabled = true

# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
management.health.dubbo.status.defaults = memory

# Enables Dubbo All Endpoints
#management.endpoint.dubbo.enabled = true
#management.endpoint.dubbo-shutdown.enabled = true
#management.endpoint.dubbo-configs.enabled = true
#management.endpoint.dubbo-services.enabled = true
#management.endpoint.dubbo-references.enabled = true
#management.endpoint.dubbo-properties.enabled = true
#
## Exposes all web endpoints
#management.endpoints.web.exposure.include = *

Config 注入多个协议

@Configuration
public class Config {

    @Bean("dubbo")
    public ProtocolConfig dubboProtocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(12345);
        protocolConfig.setId("dubbo");
        protocolConfig.setServer(null);
        return protocolConfig;
    }

    @Bean("rest")
    public ProtocolConfig restProtocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("rest");
        protocolConfig.setPort(9002);
        protocolConfig.setId("rest");
        protocolConfig.setServer("tomcat");
        return protocolConfig;
    }

HelloServiceImpl 以dubbo协议暴露服务

@Service(
        version = "${demo.service.version}",
        protocol ="dubbo"
)
public class HelloServiceImpl implements HelloService {
    @Override

    public String hello(String hello) {
        System.out.println(" hello - ");
        return "hello: "+hello;
    }
}

UserServiceImpl 以rest 协议暴露服务

@Service(
        version = "${demo.service.version}",
        protocol ="rest"
)
public class UserServiceImpl implements UserService {

    @Override
    public User getUser(Long id) {
        return new User(id,"zs"+id);
    }
}

ProviderApplication 应用启动类

@SpringBootApplication()
public class ProviderApplication {

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

consumer 服务消费者

application.yml

spring.application.name=dubbo-consumer-demo
server.port=8080
#management.server.port = 8081

demo.service.version=1.0
dubbo.application.id=dubbo-consumer-demo
dubbo.application.name=dubbo-consumer-demo

dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.id=registry
#dubbo.registry.protocol=rest
## Legacy QOS Config
dubbo.qos.port = 22223

# Dubbo Endpoint (default status is disable)
endpoints.dubbo.enabled = true

# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
management.health.dubbo.status.defaults = memory

# Enables Dubbo All Endpoints
#management.endpoint.dubbo.enabled = true
#management.endpoint.dubbo-shutdown.enabled = true
#management.endpoint.dubbo-configs.enabled = true
#management.endpoint.dubbo-services.enabled = true
#management.endpoint.dubbo-references.enabled = true
#management.endpoint.dubbo-properties.enabled = true
#
## Exposes all web endpoints
#management.endpoints.web.exposure.include = *

Controller 控制器 调用服务

@RestController
public class Controller {
    @Reference(
            version = "${demo.service.version}"
    )
    HelloService helloService;
    @Reference(
            version = "${demo.service.version}"
    )
    UserService userService;

    @RequestMapping("/")
    public String hello( String world){
        return helloService.hello(world);
    }

    @RequestMapping("/{id:\\d+}")
    public User getUser(@PathVariable("id") Long id){
        return userService.getUser(id);
    }
}

ConsumerApplication 应用启动类

@SpringBootApplication
public class ConsumerApplication {

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

三个项目公共的pom.xml

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>0.2.0version>
        dependency>

        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>2.12.0version>
        dependency>
        
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-jaxrsartifactId>
            <version>3.5.1.Finalversion>
        dependency>
        <dependency>
            <groupId>javax.json.bindgroupId>
            <artifactId>javax.json.bind-apiartifactId>
            <version>1.0version>
        dependency>
        <dependency>
            <groupId>org.eclipsegroupId>
            <artifactId>yassonartifactId>
            <version>1.0.1version>
        dependency>
        <dependency>
            <groupId>org.glassfishgroupId>
            <artifactId>javax.jsonartifactId>
            <version>1.1.2version>
        dependency>
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-jackson-providerartifactId>
            <version>2.3.5.Finalversion>
        dependency>
        <dependency>
            <groupId>org.jboss.resteasygroupId>
            <artifactId>resteasy-clientartifactId>
            <version>3.5.1.Finalversion>
        dependency>

启动provider
启动consumer
访问consumer http://localhost:8080/11
访问consumer http://localhost:8080/?world=HelloWorld
访问provider rest接口 http://localhost:9092/user/register/12

项目地址

你可能感兴趣的:(spring_boot,java)