spring cloud feign 服务调用


spring cloud feign 服务调用

 

 

***********************

引入jar包

 

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

 

***********************

相关注解

 

FeignClient

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {

    @AliasFor("name")
    String value() default "";

    @AliasFor("value")
    String name() default "";

    Class<?>[] configuration() default {};

    Class<?> fallback() default void.class;
    Class<?> fallbackFactory() default void.class;

    String url() default "";
    String path() default "";
    String qualifier() default "";
    String contextId() default "";

    boolean decode404() default false;
    boolean primary() default true;
}

 

 

***********************

服务提供端

 

*****************

配置文件

 

application.yml

spring:
  cloud:
    consul:
      host: 172.18.0.20
      port: 8500
  application:
    name: hello-provider

 

*****************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }

    @RequestMapping("/hello2")
    public String hello(@RequestParam("name") String name){
        System.out.println(name);

        return "hello world2";
    }
}

 

 

***********************

服务消费端

 

*****************

配置文件

 

application.yml

spring:
  application:
    name: feign-consumer
  cloud:
    consul:
      host: 172.18.0.20
      port: 8500

 

*****************

service 层

 

HelloService

@FeignClient(name = "hello-provider")
public interface HelloService {

    @RequestMapping("/hello")
    String hello();

    @RequestMapping("/hello2")
    String hello2(@RequestParam("name")String name);
}

 

*****************

controller 层

 

HelloController

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @RequestMapping("/get")
    public String hello(){
        return helloService.hello();
    }

    @RequestMapping("/get2")
    public String hello2(@RequestParam("name")String name){
        return helloService.hello2(name);
    }
}

 

*****************

启动类

 

DemoApplication:加注解@EnableFeignClients

@EnableFeignClients
@SpringBootApplication
public class DemoApplication {

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

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值