spring mvc dubbo怎么配置
时间: 2024-04-29 19:22:42 浏览: 164
spring mvc 的配置
1. 配置dubbo
首先,在项目中添加dubbo相关的依赖,如下:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
```
在dubbo的配置文件中配置dubbo的注册中心和自己的服务,如下:
```xml
<dubbo:application name="spring-mvc-dubbo"/>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 配置服务 -->
<dubbo:service interface="com.test.service.UserService" ref="userService"/>
<!-- 配置消费者 -->
<dubbo:reference id="userService" interface="com.test.service.UserService"/>
```
2. 配置Spring MVC
在Spring MVC的配置文件中配置dubbo的扫描包和相关的注解,如下:
```xml
<!-- 配置dubbo扫描包 -->
<dubbo:annotation package="com.test.service"/>
<!-- 配置Spring MVC扫描包 -->
<context:component-scan base-package="com.test.controller"/>
<!-- 配置Spring MVC视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置Spring MVC静态资源访问 -->
<mvc:resources mapping="/static/**" location="/static/"/>
```
3. 编写Controller
在Controller中注入dubbo的服务,并使用相关注解进行调用,如下:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
@ResponseBody
public User getUserById(@PathVariable("id") Long id) {
return userService.getUserById(id);
}
}
```
以上就是Spring MVC集成dubbo的基本配置步骤。
阅读全文