2018-1-30
[email protected]
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
先创建一个服务注册中心,IDEA中先在父项目中new一个module:
然后next:
next,然后选择Cloud Discovery,选择Eureka Server,再next,finish。
然后再在src中的EurekaServerApplication中加上@EnableEurekaServer注解,表明它是一个注册中心
最后在resources目录下的application.properties中写入:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
之后可以启动注册中心,浏览器输入localhost:8761,可以看到暂时是没有注册的
新建一个module,创建一个服务提供者eureka-client,过程和上面创建注册中心可以是一样的。
然后在EurekaClientApplication中:
package com.springcloud.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
在resources目录下的application.yml(后缀改名为yml文件,这种写法感觉更好用)
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-A
这时可以新建controller层和service层,写几个服务方法:
比如 serviceClientController:
package com.springcloud.eurekaclient.Controller;
import com.springcloud.eurekaclient.Service.IFeignSevice;
import com.springcloud.eurekaclient.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
/** * Copyright Shanghai Hand Co. Ltd. * * @author [email protected] * @date 2018/1/29 14:24 * @version: 1.0 * @description */
@RestController
public class serviceClientController {
@Value("${server.port}")
private String port;
@Autowired
private IFeignSevice feignSevice;
@RequestMapping(value = "/",method = RequestMethod.GET)
public String getServiceClientPort(){
return "我是服务提供者的第一个方法" + feignSevice.getMsg(port);
}
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String getServiceClientPort2(String name){
return "我是服务提供者的第二个方法," + name + feignSevice.getMsg(port);
}
@RequestMapping(value = "/helloworld",method = RequestMethod.POST)
public String getService(@RequestBody User user){
return "我是服务提供者的第三个方法," + feignSevice.getMsg2(port,user);
}
}
service实现类:
package com.springcloud.eurekaclient.Service.FeignServiceImpl;
import com.springcloud.eurekaclient.Service.IFeignSevice;
import com.springcloud.eurekaclient.dto.User;
import org.springframework.stereotype.Service;
/** * Copyright Shanghai Hand Co. Ltd. * * @author [email protected] * @date 2018/1/29 19:39 * @version: 1.0 * @description */
@Service
public class FeignServiceImpl implements IFeignSevice {
@Override
public String getMsg(String port) {
return " 我的端口号是:"+port;
}
@Override
public String getMsg2(String port, User user) {
String msg = " 我的端口号是:"+port+" 姓名"+user.getUserName()+" 身高"+user.getHeight()+" 年龄"+user.getAge();
return msg;
}
}
然后启动项目,浏览器访问第一个方法,因为其他两个方法需要参数,然后我们会在后面使用feign来调用这两个方法。
可以看到,服务注册到注册中心了
然后访问浏览器地址:
新建模块的过程和前面的类似,只是pom文件引入的依赖不一样,如图:
然后 ServiceFeignApplication:
package com.springcloud.servicefeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
application.yml:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
然后在feign服务里面创建controller层和service层:
frignController:
package com.springcloud.servicefeign.Controller;
import com.springcloud.servicefeign.Service.IFeignService;
import com.springcloud.servicefeign.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/** * Copyright Shanghai Hand Co. Ltd. * * @author [email protected] * @date 2018/1/29 17:09 * @version: 1.0 * @description */
@RestController
public class frignController {
@Autowired
private IFeignService feignService;
@RequestMapping(value = "/",method = RequestMethod.GET)
public String getMsg(){
return feignService.getMsg();
}
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String getServiceClientPort(){
String name = "我是帅哥";
return feignService.getServiceClientPort(name);
}
@RequestMapping(value = "/hello")
public String getServiceClientPort2(){
User user = new User().setUserName("wlg").setHeight(174).setAge(18);
return feignService.getServiceClientPort2(user);
}
}
IFeignService:
package com.springcloud.servicefeign.Service;
import com.springcloud.servicefeign.dto.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/** * Copyright Shanghai Hand Co. Ltd. * * @author [email protected] * @date 2018/1/29 17:22 * @version: 1.0 * @description */
@FeignClient(value = "service-A")
public interface IFeignService {
@RequestMapping(value = "/")
String getMsg();
@RequestMapping(value = "/hi")
String getServiceClientPort(@RequestParam("name") String name);
@RequestMapping(value = "/helloworld")
String getServiceClientPort2(@RequestBody User user);
}
通过注解@FeignClient(value = “service-A”)找到服务提供者service-A,再通过@RequestMapping(value = “”)找到要调用的方法,
这里的例子举了调用GET方法和POST方法。feign调用的一方记得加上注解,比如说get请求的@RequestParam注解,@PathVariable注解等等,
post请求记得加上@RequestBody注解等等。被feign调用的一方由于是controller层,所以带参数的话这些注解按道理是有的,
但被调用的一方的controller的某个GET请求接口如果仅仅只是用来被feign调用,我发现在这边的参数不写注解也是行的。比如:
启动项目,看注册中心:
浏览器再分别访问localhost:8765的三个接口: