SpringCloud中服务间传递参数

环境:

Eclipse,Maven,Redis

框架:

SpringCloud,Feign

需求:

项目有Common公共模块和Weather天气模块,Common模块调用Weather模块中查询天气的功能,但是目前使用前台传入经纬度,经过百度逆地址编码获取所在城市,然后将城市作为参数传入天气接口获取天气情况,并返回天气JSON串,如:

{"city":"南京市","tmp":"28","cond_txt":"阴","wind_spd":"7"}

暂时有两个想法:

1.调用Weather模块方法时,将经纬度信息传递过去

2.获取到地址信息后将经纬度存入Redis,然后天气接口从Redis取,但是如何保证存入的数据是取出时想要的那条也是个问题

第一个方法:

从请求开始—>

前台js

                    $.ajax({
	    				url : "getWeatherInfo",
	    				data : {
	    					lng : r.point.lng,
	    					lat : r.point.lat
	    				},
	    				method : "get",
	    				dataType : "json",
	    				success : function(res){
	    					document.getElementById("city").innerText = res.city;
	    					document.getElementById("cond_txt").innerText = res.cond_txt;
	    					document.getElementById("tmp").innerText = res.tmp;
	    				},error : function(){
	    					alert("地址上传失败");
	    				}
	    			});

Common模块为8004端口,此时在浏览器请求

http://localhost:8004/getWeatherInfo?lng=118.77807441&lat=32.0572355

调用方法如下

    @Autowired
    WeatherFeign weatherFeign;

    @ResponseBody
	@RequestMapping(value = "/getWeatherInfo",method = RequestMethod.GET)
	public String locationTransform(@RequestParam("lng") String lng,@RequestParam("lat") String lat) throws IOException {
		System.out.println("lng:"+lng+"lat:"+lat);
		return weatherFeign.locationTransform(lng,lat);
	}

其中使用了接口WeatherFeign,如下

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "damionew-weather")
public interface WeatherFeign {
	
	@RequestMapping(value = "/weatherGetWeatherInfo",method=RequestMethod.GET)
	public String locationTransform(@RequestParam("lng") String lng,@RequestParam("lat") String lat);
}

Weather模块下/weatherGetWeatherInfo请求处理方法


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiOperation;

/**
 * 通过前台获取经纬度转换为地址信息
 * 通过地址信息向天气接口查询天气
 * @author yinyunqi
 *
 */
@Controller
public class WeatherController {
	
	@ApiOperation(value = "获取天气信息",notes ="通过和风天气接口获取并处理天气信息")
	@ResponseBody
	@RequestMapping(value = "/weatherGetWeatherInfo",method = RequestMethod.GET)
	public String locationTransform(@RequestParam("lng") String lng
,@RequestParam("lat") String lat) throws IOException {
		// 经度
//		String lng = request.getParameter("lng");
		// 纬度
//		String lat = request.getParameter("lat");
//		String lng = "118.77807441";
//		String lat = "32.0572355";
		System.out.println("lng:"+lng+";lat:"+lat);
		XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
		return weatherResult.toJSONString();
	}

控制台打印出经纬信息说明传递成功,但此时遭遇另一个问题:

我想请求8004Common模块,但因为项目是通过8003安全验证模块登录的,所以导致所有的ajax和Css、js请求等都往8003发

顺手加了一个Zuul路由模块 史上最简单系列

然后尝试发现使用url使用绝对地址

                        $.ajax({
	    				url : "http://localhost:8006/common/getWeatherInfo",
	    				data : {
	    					lng : r.point.lng,
	    					lat : r.point.lat
	    				},
	    				method : "get",
	    				dataType : "json",
	    				success : function(res){
	    					document.getElementById("city").innerText = res.city;
	    					document.getElementById("cond_txt").innerText = res.cond_txt;
	    					document.getElementById("tmp").innerText = res.tmp;
	    				},error : function(){
	    					alert("地址上传失败");
	    				}
	    			});

鹏总教导,URL暴露不安全,最好还是后台处理

于是

在8003安全模块后台处理该请求,然后通过Feign接口调用Weather模块直接处理,不再通过Common公共模块

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值