版本详细
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.3</version>
</dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<properties>
<!-- Spring Cloud Hoxton.SR8 依赖 -->
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<!-- spring cloud alibaba 依赖 -->
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>
</properties>
1.创建dubboApi
package com.imooc.ecommerce.service;
public interface DubboApiService {
public String dubboTest(String arg);
}
2.创建provider
package com.imooc.ecommerce;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
创建Dubboapi实现类 这里要引入api模块
package com.imooc.ecommerce.service;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DubboServiceImpl implements DubboApiService {
@Override
public String dubboTest(String arg) {
System.out.println("test success");
return "test success";
}
}
<dependency>
<groupId>com.imooc.ecommerce</groupId>
<artifactId>e-commerce-dubbo3-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
3.创建consumer
package com.imooc.ecommerce;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
4 consumer调用
package com.imooc.ecommerce;
import com.imooc.ecommerce.service.DubboApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DubboConsumerApplicationTest {
@DubboReference(url = "dubbo://192.168.31.24:20880/com.imooc.ecommerce.service.DubboApiService")
private DubboApiService dubboApiService;
@Test
public void test() {
String sss = dubboApiService.dubboTest("sss");
System.out.println(sss);
}
}
在provider控制台找