1.启动Eureka
2.启动eureka-client,测试
3.打开cliient-test1项目,清理掉RestTemplate的代码
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.启动项目,测试