• 与springboot整合的三种方式


    前期准备

    创建两个springboot工程,分别为服务提供者和服务消费者,再创建一个maven project 的公共API

    公共API

    bean

    package com.atguigu.gmall.bean;
    
    import java.io.Serializable;
    
    public class UserAddress implements Serializable {
    
        private Integer id;
        private String userAddress;
        private String userId;
        private String consignee;
        private String phoneNum;
        private String isDefault;
    
        public UserAddress() {
            super();
        }
    
        public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) {
            this.id = id;
            this.userAddress = userAddress;
            this.userId = userId;
            this.consignee = consignee;
            this.phoneNum = phoneNum;
            this.isDefault = isDefault;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserAddress() {
            return userAddress;
        }
    
        public void setUserAddress(String userAddress) {
            this.userAddress = userAddress;
        }
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public String getConsignee() {
            return consignee;
        }
    
        public void setConsignee(String consignee) {
            this.consignee = consignee;
        }
    
        public String getPhoneNum() {
            return phoneNum;
        }
    
        public void setPhoneNum(String phoneNum) {
            this.phoneNum = phoneNum;
        }
    
        public String getIsDefault() {
            return isDefault;
        }
    
        public void setIsDefault(String isDefault) {
            this.isDefault = isDefault;
        }
    }
    View Code

    service

    package com.atguigu.gmall.service;
    
    import com.atguigu.gmall.bean.UserAddress;
    
    import java.util.List;
    
    public interface OrderService {
        public List<UserAddress> initOrder(String userId);
    }
    View Code
    package com.atguigu.gmall.service;
    
    
    import com.atguigu.gmall.bean.UserAddress;
    
    import java.util.List;
    
    public interface UserService {
        public List<UserAddress> getUserAddressList(String userId);
    }
    View Code

    服务提供者

    service 实现

    package com.atguigu.gmall.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.atguigu.gmall.bean.UserAddress;
    import com.atguigu.gmall.service.UserService;
    import org.springframework.stereotype.Component;
    
    import java.util.Arrays;
    import java.util.List;
    @Service
    @Component
    public class UserServiceImpl implements UserService {
        public List<UserAddress> getUserAddressList(String userId) {
            UserAddress address1 = new UserAddress(1,"beijingshichangping","1", "lilaoshi","0536-0877651","yes");
            UserAddress address2 = new UserAddress(2, "shenzhenshi", "1", "wanglaoshi", "0536-0671118", "no");
            return Arrays.asList(address1, address2);
        }
    }
    View Code

    application.properties

    dubbo.application.name=user-service-provider
    dubbo.registry.address=127.0.0.1:2181
    dubbo.registry.protocol=zookeeper
    
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=20880
    
    dubbo.monitor.protocol=registry

    pom 依赖文件

    <?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.3.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.atguigu.gmall</groupId>
        <artifactId>boot-user-service-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>boot-user-service-provider</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>gmall-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.7</version>
            </dependency>
    <!--        <dependency>-->
    <!--            <groupId>com.alibaba</groupId>-->
    <!--            <artifactId>dubbo</artifactId>-->
    <!--            <version>2.6.9</version>-->
    <!--        </dependency>-->
            <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.8.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    View Code

    服务消费者

     service 实现

    package com.atguigu.gmall.impl;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.atguigu.gmall.bean.UserAddress;
    import com.atguigu.gmall.service.OrderService;
    import com.atguigu.gmall.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class OrderServiceImpl implements OrderService {
        @Reference
        UserService userService;
        public List<UserAddress> initOrder(String userId) {
            List<UserAddress> addressList = userService.getUserAddressList(userId);
            return addressList;
        }
    }
    View Code

    controller

    package com.atguigu.gmall.controller;
    
    import com.atguigu.gmall.bean.UserAddress;
    import com.atguigu.gmall.service.OrderService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    public class OrderController {
    
        @Autowired
        private OrderService orderService;
    
        @ResponseBody
        @RequestMapping("/initOrder")
        public List<UserAddress> initOrder(@RequestParam String userId){
            return orderService.initOrder(userId);
        }
    }
    View Code

    application.properties

    server.port=8081
    dubbo.application.name=user-service-consumer
    dubbo.registry.address=127.0.0.1:2181
    dubbo.registry.protocol=zookeeper

    pom 配置文件

    <?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.3.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.atguigu.gmall</groupId>
        <artifactId>boot-order-service-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>boot-order-service-consumer</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>gmall-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.7</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.8.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    View Code

    三种整合方式

    (1)导入dubbo-starter,在application.properties中配置属性,使用@Service【暴露服务】,使用@Reference 【引用服务】,只能配置接口层级

      实现方式就是前期准备中的demo.

    (2)保留dubbo xml 配置,通过@ImportResource 引入配置,可以配置方法级别

          保留 provider.xml ,使用@ImportResource 导入。示例:

      

    package com.atguigu.gmall;
    
    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    //@EnableDubbo
    @ImportResource(locations = "classpath:provider")
    @SpringBootApplication
    public class BootUserServiceProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BootUserServiceProviderApplication.class, args);
        }
    
    }

    然后注释application.properties 中的属性配置。

      

    (3)使用注解API的方式

      API 配置类示例

    package com.atguigu.gmall.config;
    
    import com.atguigu.gmall.service.UserService;
    import org.apache.dubbo.config.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    public class MyDubboConfig {
    
        @Bean
        public ApplicationConfig applicationConfig(){
            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName("boot-user-service-provider");
            return applicationConfig;
        }
    
        @Bean
        public RegistryConfig registryConfig(){
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setProtocol("zookeeper");
            registryConfig.setAddress("127.0.0.1:2181");
            return registryConfig;
        }
    
        @Bean
        public ProtocolConfig protocolConfig(){
            ProtocolConfig protocolConfig = new ProtocolConfig();
            protocolConfig.setName("dubbo");
            protocolConfig.setPort(20880);
            return protocolConfig;
        }
    
        @Bean
        public ServiceConfig<UserService> userServiceServiceConfig(UserService userService){
            ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<>();
            userServiceServiceConfig.setInterface(UserService.class);
            userServiceServiceConfig.setRef(userService);
            userServiceServiceConfig.setVersion("1.0.0");
    
            MethodConfig methodConfig = new MethodConfig();
            methodConfig.setName("getUserAddressList");
            methodConfig.setTimeout(1000);
            List<MethodConfig> list = new ArrayList<>();
            list.add(methodConfig);
            userServiceServiceConfig.setMethods(list);
    
            return userServiceServiceConfig;
        }
    }
    View Code

      启动类

      

    package com.atguigu.gmall;
    
    import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    //@EnableDubbo
    //@ImportResource(locations = "classpath:provider")
    @DubboComponentScan("com.atguigu.gmall")
    @SpringBootApplication
    public class BootUserServiceProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BootUserServiceProviderApplication.class, args);
        }
    
    }
    View Code
  • 相关阅读:
    [CareerCup] 1.4 Replace Spaces 替换空格
    [CareerCup] 1.3 Permutation String 字符串的排列
    Android @1x,@2x,@3x 资源文件自动分包工具
    Android 使用xml实现边框阴影,背景渐变效果(附有RGB颜色查询对照表)
    Android中快速实现自定义字体!
    Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.
    Android getRunningTasks和getRunningAppProcesses失效
    Android 实现朋友圈有图片和视频
    Android 仿微信朋友圈拍小视频上传到服务器
    Android setUserVisibleHint-- fragment真正的onResume和onPause方法
  • 原文地址:https://www.cnblogs.com/dongch/p/13881421.html
Copyright © 2020-2023  润新知