Micro-mvc与springcloud整合

原创
2018/01/18 10:24
阅读数 810

Micro-mvc不但可以与springmvc整合还可以与springcloud整合

Springcloud的controller层改为接口注解仍使用springcloud和springboot相关注解实现服务注册路由等配置,但controller接口上需要添加@InjectGroovy注解设置接口实现的关联groovy。

package com.nh.micro.springcloud.demo.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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 org.springframework.web.bind.annotation.RestController;

import com.nh.micro.service.InjectGroovy;

@InjectGroovy(name="ComputeGroovy")
@RestController
public interface ComputeController {

    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    @ResponseBody
    public Integer add(@RequestParam(value="a") Integer a, @RequestParam(value="b") Integer b);

}

Spring配置中使用GroovyBeanScannerConfigurer扫描controller接口

	<bean class="com.nh.micro.service.GroovyBeanScannerConfigurer">
		<property name="scanPath" value="com.nh.micro.springcloud.demo.web"></property>
	</bean>

ComputeController接口实现ComputeGroovy.groovy

package groovy

import javax.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestParam;
import org.apache.log4j.Logger;

class ComputeGroovy {
private final Logger logger = Logger.getLogger(getClass());

//@Resource(name="discoveryClient")
//public DiscoveryClient client;
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        //GroovyExecUtil.execGroovyRetObj("TestGroovy", "test");

        Integer r = a + b;
		System.out.println(r);
		//ServiceInstance instance = client.getLocalServiceInstance();
        //logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }
}

启动DemoServiceApplication,访问http://localhost:2222/add?a=1&b=2

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部