接上篇dubbo入门-dubbo和spring整合,在上一个工程的基础上,进行修改,让dubbo和springboot实现整合
1.服务生产者模块:dubbo-demo-provider
- 在DemoServiceImpl上添加Service注解
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.demo.DemoService;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return " I love you! " + name;
}
}
- 将启动类修改为springboot的方式
package com.alibaba.dubbo.demo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Provider {
public static void main(String[] args) throws Exception {
SpringApplication.run(Provider.class, args);
}
}
- pom文件添加springboot相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.imooc</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建application.properties
##端口号
server.port=8880
## Dubbo 服务提供者配置
spring.dubbo.application.name=dubbo_provider
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.alibaba.dubbo.demo.provider
2.服务消费者模块:dubbo-demo-consumer
- 创建HelloController
package com.alibaba.dubbo.demo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference(version = "1.0.0")
DemoService demoService;
@GetMapping("sayHello")
public String sayHello(String name){
return demoService.sayHello(name);
}
}
- 创建入口类
package com.alibaba.dubbo.demo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Consumer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Consumer.class,args);
}
}
- 创建application.properties
## 端口号
server.port=8881
##Dubbo 服务消费者配置
spring.dubbo.application.name=dubbo_consumer
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.scan=com.alibaba.dubbo.demo.consumer
网友评论