此篇和上篇只是spring-boot和spring-cloud的版本不同,所带来的配置及代码变化,项目配置及代码参考如下。
创建maven项目servicefeign_f;
项目目录结构:
1. pom.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud.lcl.fetgn</groupId>
<artifactId>servicefeign_f</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>servicefeign_f</name>
<url>http://www.myorganization.org</url>
<build>
<finalName>servicefeign_f</finalName>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2. 代码示例
项目主启动类:
package com.springcloud.lcl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author lcl
* @version 0.1
* @date 2018/11/8
**/
@SpringBootApplication
@EnableFeignClients
public class ServiceFeignFinchleyApplication {
public static void main(String[] args){
SpringApplication.run(ServiceFeignFinchleyApplication.class,args);
}
}
服务接口 IFeignService代码:
package com.springcloud.lcl.feign.service;
import com.springcloud.lcl.feign.service.impl.FeignServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-hi",fallback = FeignServiceImpl.class)
public interface IFeignService {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String getHiService(@RequestParam(value = "name") String name);
}
服务接口实现类FeignServiceImpl:
package com.springcloud.lcl.feign.service.impl;
import com.springcloud.lcl.feign.service.IFeignService;
import org.springframework.stereotype.Component;
/**
* @author lcl
* @version 0.1
* @date 2018/11/8
**/
@Component
public class FeignServiceImpl implements IFeignService {
@Override
public String getHiService(String name) {
return "Hii "+name+": Sorry,Error...";
}
}
api接口定义类:FeignController
package com.springcloud.lcl.feign;
import com.springcloud.lcl.feign.service.IFeignService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lcl
* @version 0.1
* @date 2018/11/8
**/
@RestController
public class FeignController {
@Autowired
private IFeignService feignService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hiService(@RequestParam String name){
return feignService.getHiService(name);
}
}
3. application.properties 配置:
spring.application.name=service-feign-fin
server.port=8790
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
4. 启动及验证同上篇。