框架整合搭建实战练习记录2

上一篇文章里实现了一个基本的用户服务程序

框架整合搭建实战练习记录1


这里对程序进行改造


由原先的单用户服务情况下增加Eureka服务注册中心、Zuul智能路由、用户服务里增加用户余额的业务、order订单服务程序(order里增加Feign服务,用来调用用户余额服务)。


过程简要记录,具体可以看代码https://github.com/li39000yun/cloud3

一、增加注册服务中心eureka

1、新建一个model,eureka-server

2、修改application.properties为application.yml,使用yaml格式,设置port端口号8761

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、启动类上增加注解@EnableEurekaServer

package com.lyq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

二、修改用户服务demo

1、增加用户余额功能,每个用户绑定一个账户金额

2、对外开放接口,实现两个功能

a)根据用户id查询余额

b)根据用户id修改余额

package com.lyq.controller;

import com.lyq.model.AccTotal;
import com.lyq.service.AccTotalService;
import com.lyq.utils.AjaxJson;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账户余额对外接口
 * Created by 云强 on 2017/11/12.
 */
@RestController
public class AccTotalRestController {

    @Autowired
    AccTotalService accTotalService;

    /**
     * 查询用户余额
     * @param userId
     * @return
     */
    @RequestMapping("/accTotal/find/{userId}")
    public AccTotal findByUserId(@PathVariable("userId") Long userId) {
        return accTotalService.findByUserId(userId);
    }

    /**
     * 修改账户余额
     * 给指定账户添加余额,如需要减少,则传入负数
     * @param userId 用户id
     * @param money 修改余额
     * @return
     */
    @RequestMapping("/accTotal/update/{userId}")
    public AjaxJson updateAccTotal(@PathVariable("userId") Long userId,Double money) {
        AjaxJson j = new AjaxJson();
        try{
            // 获取余额
            AccTotal accTotal = accTotalService.findByUserId(userId);
            // 修改余额
            accTotal.setAccTotal(accTotal.getAccTotal()+money);
            // 更新余额
            accTotalService.updateTotal(accTotal);
            j.setObj(accTotal);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg(e.getMessage());
        }
        return j;
    }


}


二、增加订单服务order

1、新建一个model,order

2、实现订单基本的增删改查功能,订单包括用户id,和金额

3、启动类开启feign功能

package com.lyq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(OrderApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}
4、Feign接口实现相关类

接口调用类AccTotalService

package com.lyq.service;

import com.lyq.model.AccTotal;
import com.lyq.utils.AjaxJson;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 调用接口类
 * Created by 云强 on 2017/11/10.
 */
@FeignClient(value = "service-demo")
public interface AccTotalService {

    /**
     * 查询用户余额
     *
     * @param userId
     * @return
     */
    @RequestMapping("/accTotal/find/{userId}")
    public AccTotal findByUserId(@PathVariable("userId") Long userId);

    /**
     * 修改账户余额
     * 给指定账户添加余额,如需要减少,则传入负数
     *
     * @param userId 用户id
     * @param money  修改余额
     * @return
     */
    @RequestMapping("/accTotal/update/{userId}")
    public AjaxJson updateAccTotal(@PathVariable("userId") Long userId,@RequestParam(value = "money")  Double money);

}

控制层实现订单余额修改调用功能

package com.lyq.controller;

import com.lyq.model.OrderEntity;
import com.lyq.service.AccTotalService;
import com.lyq.service.OrderService;
import com.lyq.utils.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制层
 * Created by 云强 on 2017/11/8.
 */
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    AccTotalService accTotalService;

    @Value("${server.port}")
    String port;

    @RequestMapping("/hello")
    public String home(@RequestParam String name) {
        return "hi " + name + ",i am from port:" + port;
    }


    @RequestMapping("/order/get/{id}")
    public OrderEntity orderFindOne(@PathVariable("id") String id) {
        return orderService.findOrderById(id);
    }

    @RequestMapping("/order/getList")
    public List<OrderEntity> orderFindAll() {
        return orderService.findAll();
    }

    @RequestMapping("/submit/{id}")
    public AjaxJson sumbit(@PathVariable("id") String id) {
        AjaxJson j = new AjaxJson();
        try {
            OrderEntity orderEntity = orderService.findOrderById(id);
            return accTotalService.updateAccTotal(orderEntity.getUserId(), orderEntity.getMoney());
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg(e.getMessage());
        }
        return j;
    }

}

5、界面增加提交功能,当提交时调用用户账户服务,通过feign调用服务修改账户金额



三、增加zuul路由网关功能(作为客户端统一入口)

1、新建model:service-zuul

2、application.yml配置如下,增加用户及用户账户服务(demo)和订单服务(order)路由规则,端口号设置8769

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-demo
    api-b:
      path: /api-b/**
      serviceId: service-feign
    demo:
      path: /demo/**
      serviceId: service-demo
    order:
      path: /order/**
      serviceId: service-order
3、启动类开启zuul代理功能

package com.lyq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ServiceZuulApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceZuulApplication.class, args);
	}
}
四、测试

1、依次启动eureka-server、demo、order、service-zuul

2、访问http://localhost:8761/    可以看到用户服务、订单服务、zuul服务已经注册进去了

3、访问http://localhost:8769/demo/accTotal

自动跳转到http://localhost:8081/accTotal/list 打开了账户界面


4、访问http://localhost:8769/order打开订单服务列表界面


5、操作提交,再查看demo的用户余额,发现余额增加了,说明接口调用成功,成功实现两个服务之间的数据调用

保存成功返回值:


用户余额界面:



本次改造先到这里


猜你喜欢

转载自blog.csdn.net/failure_lee/article/details/78521060