springCloud-5.使用Feign实现应用之间的通信

1.启动Eureka

4f9f30d4d61d6f53301d4c8a161de08c18b.jpg

2.启动eureka-client,测试

0af9e97295fec44c859ff57047ce66028cf.jpg

ce8381a6fb2aa0dc2586933a5f7293e9be8.jpg

3.打开cliient-test1项目,清理掉RestTemplate的代码

3c8dd822005b3844185db0387fbd05d6bb8.jpg

4.pom.xml添加Feign

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
     <version>1.4.4.RELEASE</version>
</dependency>

5.启动文件增加Feign注解

package com.client.test.clienttest1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientTest1Application {

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

6.创建访问服务类EurekaClient

package com.client.test.clienttest1.client;

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

/**
 * 描述:
 *
 * @outhor hekang
 * @create 2018-07-31 上午10:32
 */
@FeignClient(name = "EUREKA-CLIENT")
public interface EurekaClient {
    @RequestMapping("/index")
    String getIndex();

}

说明:FeignClient里面的name是服务名称

        RequestMapping里面写需要访问的方法

7.在ClientController类里面调用EurekaClient

package com.client.test.clienttest1;

import com.client.test.clienttest1.client.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述:
 *
 * @outhor hekang
 * @create 2018-07-30 下午3:31
 */
@RestController
public class ClientController {
    @Autowired
    private EurekaClient eurekaClient;

    @RequestMapping("toIndex")
    public String toIndex(){
        return eurekaClient.getIndex();
    }
}

8.启动项目,测试

a1db4593616d87f19a171338e87fcbbcbb4.jpg

转载于:https://my.oschina.net/u/1046143/blog/1920146

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值