1. 程式人生 > >Spring Cloud入門:服務消費(Spring Cloud Feign)

Spring Cloud入門:服務消費(Spring Cloud Feign)

文章例項使用的Spring Cloud版本為Finchley.SR1,Spring Boot版本為2.0.4

1 Spring Cloud Feign

Spring Cloud Feign是一套基於Netflix Feign實現的宣告式服務呼叫客戶端。使用Spring Cloud Feign,我們只需建立一個介面並用註解的方式來配置它,即可完成對服務提供方的介面繫結,簡化了在使用Spring Cloud Ribbon時自行封裝服務呼叫客戶端的開發量。

2 例項

2.1 建立Spring Boot應用,引入相關依賴

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<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-netflix-hystrix</artifactId>
		</dependency>

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

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2.2 application.properties指定註冊中心

spring.application.name=feign-consumer
server.port=3002

#專案資訊
info.name=${spring.application.name}
info.server.ip-address=${spring.cloud.client.ip-address}
info.server.port=${server.port}

#例項預設通過使用域名形式註冊到註冊中心:false
eureka.instance.prefer-ip-address=true

#例項名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

#註冊中心地址
eureka.client.serviceUrl.defaultZone=http://peer2:1002/eureka/,http://peer2:1003/eureka/

2.3 修改應用主類

@SpringCloudApplication
@EnableFeignClients
public class FeignConsumerApplication {

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

2.4 新建介面,繫結要呼叫的服務

@FeignClient("EUREKA-CLIENT")
public interface HelloFeignClient {

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

}

2.5 服務呼叫

@RestController
public class ConsumerController {

    @Autowired
    private HelloFeignClient helloFeignClient;

    @GetMapping("/feign-consumer/hello")
    public String dc() {
        return helloFeignClient.hello();
    }
}

2.6 啟動應用

訪問http://localhost:3002/feign-consumer/hello 可以看到成功呼叫到了EUREKA-CLIENT的服務。