1. 程式人生 > >dobbo入門案例(一)

dobbo入門案例(一)

public class Stu implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private String sex;

 3.dao層

public interface StuDao {
    public List<Stu>  findall();
    public Stu findallbyid(Integer id);

4.sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xdl.tigong.dao.StuDao">
    <select id="findall" resultType="com.xdl.tigong.entity.Stu" >
        SELECT  * from stu
    </select>
    <select id="findallbyid" resultType="com.xdl.tigong.entity.Stu" parameterType="Integer">
        SELECT  * from stu where id=#{id}
    </select>
</mapper>

5.service

//service介面
public interface IStuService {

    public List<Stu> findall();
    public Stu findallbyid(Integer id);
}

//service介面實現
@Service
public class IStuServiceImpl implements IStuService {

    @Autowired
    private StuDao stuDao;

    @Override
    public List<Stu> findall() {
        return  stuDao.findall();
    }

    @Override
    public Stu findallbyid(Integer id) {
        return stuDao.findallbyid(id);
    }

 6.暴露服務的介面(重點)

   providers.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-tigong" />
    <!-- 使用zookeeper註冊中心暴露服務地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 用dubbo協議在20880埠暴露服務 -->
	<dubbo:protocol name="dubbo" port="29014" />

    <!-- 宣告需要暴露的服務介面 -->
    <dubbo:service interface="com.xdl.tigong.service.TestService" ref="testService"/>
    <!-- 具體的實現bean -->
    <bean id="testService" class="com.xdl.tigong.service.impl.TestServiceImpl"></bean>

    <dubbo:service interface="com.xdl.tigong.service.IStuService" ref="findService"/>
    <bean id="findService" class="com.xdl.tigong.service.impl.IStuServiceImpl"></bean>

</beans>

7.在啟動項

@SpringBootApplication
@ImportResource(value = {"classpath:providers.xml"})  //讀檔案
@MapperScan(value = "com.xdl.tigong.dao") 
public class TigongApplication {

	public static void main(String[] args) {
		SpringApplication.run(TigongApplication.class, args);
	}
}

生產者:

maven和提供者的一樣。

providers.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-xiaofei"/>
    <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://localhost:2181" />

    <!-- 生成遠端服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="testService" interface="com.xdl.tigong.service.TestService"/>

    <dubbo:reference id="findService" interface="com.xdl.tigong.service.IStuService" />
</beans>
@SpringBootApplication
@ImportResource(value = {"classpath:providers.xml"})
public class XiaofeiApplication {

	public static void main(String[] args) {
		SpringApplication.run(XiaofeiApplication.class, args);
	}
}

controller

@Controller
public class StuController {
    @Resource //不能用@Autowired 注意
    private IStuService iStuService;

    @RequestMapping(value = "/find" ,method = RequestMethod.GET,produces = "application/json;charset=utf-8")

    public @ResponseBody List<Stu> findall(){
       return iStuService.findall();
    }

完了就ok,開啟zookeeper。

在開啟服務,訪問8080埠。消費者預設埠8080,如果和本地衝突自己改哈。

在把監控開起來dubbo-admin-2.5.10.war(網上down一個放到自己的tomcat裡啟動,預設賬號密碼root)。

我是程式設計師s