springboot集成dubbo框架

操作顺序:1、开启zookeeper 2、启动服务提供者 3、启动服务消费者

依赖:

 
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
        

        
        
            com.101tec
            zkclient
            0.10
        

        
        
            com.springboot
            14-springboot-dubbo-interface
            1.0.0
        

一、接口工程

springboot集成dubbo框架_第1张图片

二、服务提供者

1、application.properties

#设置tomcat端口号
server.port=8081

#设置上下文根
server.servlet.context-path=/

#声明项目
spring.application.name=14-springboot-dubbo-provider

#当前项目是一个服务提供者
spring.dubbo.server=true

#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

2、class Application

package com.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication			//开启spring注解驱动
@EnableDubboConfiguration			//开启dubbo注解驱动
public class Application {

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

}

3、WeatherServiceImpl

package com.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.beans.Weather;
import com.service.IWeatherService;
import org.springframework.stereotype.Component;

/**
 * @author dc
 * @date 2020/5/16 - 18:17
 */
@Component
@Service(interfaceClass = IWeatherService.class)
public class WeatherServiceImpl implements IWeatherService {
    @Override
    public Weather queryWeather(String code) {
        Weather weather = new Weather();
        if ("1".equals(code)){
            weather.setName("北京");
            weather.setSd("60%");
            weather.setTemp(15);
            weather.setDesc("天气凉爽!");
        }else if ("2".equals(code)){
            weather.setName("上海");
            weather.setSd("70%");
            weather.setTemp(25);
            weather.setDesc("天气温暖!");
        }else if ("3".equals(code)){
            weather.setName("广州");
            weather.setSd("80%");
            weather.setTemp(35);
            weather.setDesc("天气较热!");
        }else {
            weather.setName("深圳");
            weather.setSd("85%");
            weather.setTemp(38);
            weather.setDesc("天气炎热!");
        }
        return weather;
    }
}

4、MovieServiceImpl

package com.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.beans.Movie;
import com.service.IMovieService;
import org.springframework.stereotype.Component;

/**
 * @author dc
 * @date 2020/5/16 - 18:22
 */
@Component
@Service(interfaceClass = IMovieService.class)
public class MovieServiceImpl implements IMovieService {
    @Override
    public Movie queryMovie(String code) {
        Movie movie = new Movie();
        if ("1".equals(code)){
            movie.setName("西虹市首富");
            movie.setActor("沈腾");
            movie.setMinutes(100);
        }else if ("2".equals(code)){
            movie.setName("美人鱼");
            movie.setActor("邓超");
            movie.setMinutes(105);
        }else if ("3".equals(code)){
            movie.setName("夏洛特烦恼");
            movie.setActor("尹正");
            movie.setMinutes(110);
        }else {
            movie.setName("A计划");
            movie.setActor("成龙");
            movie.setMinutes(115);
        }
        return movie;
    }
}

三、服务消费者

springboot集成dubbo框架_第2张图片

1、application.properties

#设置tomcat端口号
server.port=8088

#设置上下文根
server.servlet.context-path=/

#声明项目
spring.application.name=14-springboot-dubbo-consumer

#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

2、class Application

package com.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication				//开启spring注解驱动
@EnableDubboConfiguration		//开启dubbo注解驱动
public class Application {

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

}

3、MyController

package com.springboot.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.beans.Movie;
import com.beans.Weather;
import com.service.IMovieService;
import com.service.IWeatherService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dc
 * @date 2020/5/16 - 18:29
 */
@RestController
public class MyController {

    @Reference(interfaceClass = IWeatherService.class, check = false)
    private IWeatherService weatherService;
    @Reference(interfaceClass = IMovieService.class, check = false)
    private IMovieService movieService;

    @RequestMapping("/weather/{code}")
    public Object weatherController(@PathVariable("code") String code){
        Weather weather = weatherService.queryWeather(code);
        return weather;
    }

    @RequestMapping("/movie/{code}")
    public Object movieController(@PathVariable("code") String code){
        Movie movie = movieService.queryMovie(code);
        return movie;
    }
}

你可能感兴趣的:(springboot集成dubbo框架)