五.springboot集成dubbo(无admin)

  • 先搭建两个微服务工程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方法启动启动类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值