dubbo web工程示例_Dubbo专栏:SpringBoot整合Dubbo服务降级与豪猪断路器二

工程目录

8622129f3a48123baa2b3c6a1f21a0d8.png

添加pom仓库坐标

xml version="1.0" encoding="UTF-8"?>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">            org.springframework.boot        spring-boot-starter-parent        2.0.4.RELEASE                4.0.0    springboot-dubbo-service-consumer    jar    boot-order-service-consumer    Demo project for Spring Boot            UTF-8        UTF-8        1.8                            org.springframework.boot            spring-boot-starter-web                            com.alibaba.boot            dubbo-spring-boot-starter            0.2.0                            com.example            iterface-common            1.0-SNAPSHOT                            org.springframework.boot            spring-boot-starter-test            test                            org.springframework.cloud                            spring-cloud-starter-netflix-hystrix                                                    org.springframework.boot                spring-boot-maven-plugin                                                                org.springframework.cloud                spring-cloud-dependencies                Finchley.SR1                pom                import                        

添加配置文件application.yml

server.port=8081dubbo.application.name=springboot-dubbo-service-consumerdubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.monitor.protocol=registry

添加控制层controller

package com.yang.dubbo.controller;import java.util.List;import com.dubbo.common.bean.UserAddress;import com.dubbo.common.service.OrderService;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.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class OrderController {@Autowired   OrderService orderService;      @ResponseBody   @RequestMapping("/initOrder")public ListinitOrder(@RequestParam("uid")String userId) {return orderService.initOrder(userId);   }
}

service层

package com.yang.dubbo.service.impl;import java.util.Arrays;import java.util.List;import com.dubbo.common.bean.UserAddress;import com.dubbo.common.service.OrderService;import com.dubbo.common.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.alibaba.dubbo.config.annotation.Reference;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;/** * 1、将服务提供者注册到注册中心(暴露服务) *        1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator) *        2)、配置服务提供者 *  * 2、让服务消费者去注册中心订阅服务提供者的服务地址 * @author lfy * */@Servicepublic class OrderServiceImpl implements OrderService {//@Autowired   @Reference(loadbalance="random",timeout=1000) //dubbo直连         UserService userService;      @HystrixCommand(fallbackMethod="hello")@Override   public ListinitOrder(String userId) {// TODO Auto-generated method stub      System.out.println("用户id:"+userId);      //1、查询用户的收货地址      List addressList = userService.getUserAddressList(userId);      return addressList;   }public Listhello(String userId) {// TODO Auto-generated method stub         return Arrays.asList(new UserAddress(10, "测试地址", "1", "测试", "测试", "Y"));   }
}

消费方的启动类

package com.yang.dubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;@EnableDubbo@EnableHystrix@SpringBootApplicationpublic class BootOrderServiceConsumerApplication {public static void main(String[] args) {
SpringApplication.run(BootOrderServiceConsumerApplication.class, args); }
}

最后我们访问下

c6e69351243f6494897574ecf6376b00.png

8a1069476ec02f4babd4e6665b0463ef.png

d857f26b67b043fe09ad556b8b0bfecc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值