侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Spring boot 配置dubbo

2023-11-09 星期四 / 0 评论 / 0 点赞 / 41 阅读 / 5289 字

使用包有<!-- dubbo--><dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId>

使用包有

<!-- dubbo--><dependency>    <groupId>com.alibaba</groupId>    <artifactId>dubbo</artifactId>    <exclusions>        <exclusion>            <groupId>org.springframework</groupId>            <artifactId>spring</artifactId>        </exclusion>    </exclusions></dependency><!-- dubbo--><!-- zookeeper--><dependency>    <groupId>com.101tec</groupId>    <artifactId>zkclient</artifactId></dependency><!-- zookeeper-->

第一种使用spring.xml配置

  • 在生产者与消费者项目中添加Configuration配置类(也可以放到公用项目中)
package com.uwo.dubbo.configuration;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;import org.springframework.context.annotation.PropertySource;/** * Created by yanhao on 2017/5/23. */@Configuration@PropertySource("classpath:dubbo.properties")@ImportResource({ "classpath:*.xml" })public class UwoConfiguration {}
  • 生产者xml配置方式
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd        ">    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="${dubbo.application.name}" />    <!-- 注册中心暴露服务地址 -->    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />    <!-- 暴露服务 -->    <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />    <dubbo:service interface="com.uwo.dubbo.service.UwoService" ref="uwoService" retries="0" timeout="6000" /></beans>
  • 生产者属性配置dubbo.properties
#应用名称dubbo.application.name=uwo-provider#注册中心类型dubbo.registry.protocol=zookeeper#注册中心地址dubbo.registry.address=10.211.55.8:2181#暴露服务方式dubbo.protocol.name=dubbo#暴露服务端口dubbo.protocol.port=20880
  • Service类
package com.uwo.dubbo.service.imp;import com.uwo.dubbo.pojo.Uwo;import com.uwo.dubbo.service.UwoService;import org.springframework.stereotype.Service;/** * Created by yanhao on 2017/5/23. */@Service("uwoService")public class UwoServiceImpl implements UwoService{    public Uwo validate(String username) {        Uwo uwo = new Uwo();        uwo.setUsername(username + "-hu");        uwo.setPassword("uwo123456");        return uwo;    }}
  • 消费者xml配置方式
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"       xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="${dubbo.application.name}" />    <!-- 注册中心暴露服务地址 -->    <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />    <dubbo:reference id="uwoService" interface="com.uwo.dubbo.service.UwoService" /></beans>
  • 消费者属性配置dubbo.properties
#应用名称dubbo.application.name=uwo-consumer#注册中心类型dubbo.registry.protocol=zookeeper#注册中心地址dubbo.registry.address=10.211.55.8:2181
  • Controller类
package com.uwo.dubbo.controller;import com.uwo.dubbo.pojo.Uwo;import com.uwo.dubbo.service.UwoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by yanhao on 2017/5/23. */@Controllerpublic class UwoController {    @Autowired    private UwoService uwoService;    @RequestMapping("/")    @ResponseBody    public String auth(){        Uwo uwo = uwoService.validate("uwo");        return uwo.getUsername();    }}

第二种使用@Configuration与@Bean配置

参考地址

广告 广告

评论区