-
先搭建两个微服务工程provider/consumer
1.pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.application.properties--provider
spring.application.name
=
dubbo-spring-boot-provider/consumer
2.application.properties--consumer
spring.application.name
=
dubbo-spring-boot-consumer
server.port
=
8084
3。创建启动类ProviderApplication/ConsumerApplication
@SpringBootApplication
public class ProviderApplication{
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
4.创建访问接口
@Controller
@RequestMapping
public class HelloController {
@RequestMapping("/getHello")
@ResponseBody
public String getHello(){
return "hello my springboot";
}
}
5.分别main方法启动启动类
-
provider项目配置修改
1.引入dubbo依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.启动类添加注解
@EnableDubboConfiguration
3.application.properties
spring.dubbo.registry=N/A
spring.dubbo.server=true
4.添加业务处理接口
public interface DubboService {
public String getName(String name);
}
import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.DubboService;
import org.springframework.stereotype.Component;
@Component //spring的bean配置
@Service(interfaceClass = DubboService.class) //相当于dubbo:service interface暴露配置
public class DubboServiceImpl implements DubboService{
@Override
public String getName(String name) {
return "姓名:"+name;
}
}
5.main方法启动启动类
-
consumer项目配置修改
1.引入dubbo依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.启动类添加注解
@EnableDubboConfiguration
3.添加业务处理接口
public interface DubboService {
public String getName(String name);
}
4.修改consumer访问接口
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.service.DubboService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping
public class TestController {
@Reference(url = "dubbo://localhost:20880")
DubboService dubboService;
@ResponseBody
@RequestMapping(value = "/abc",method = RequestMethod.GET)
public String abc(String name){
// return "nihao";
return dubboService.getName(name);
}
}
5.main方法启动启动类