Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

10.11. FAQ

10.11.1. Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

解決方法,禁用 CSRF

		
package cn.netkiller.eureka.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
		super.configure(http);
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		super.configure(auth);
	}

}
		
		
		

10.11.2. @EnableDiscoveryClient與@EnableEurekaClient 區別

		
相同點:@EnableDiscoveryClient、@EnableEurekaClient  這二個註解作用,都可以讓該服務註冊到註冊中心上去。
不同點:@EnableEurekaClient 只支持Eureka註冊中心,@EnableDiscoveryClient 支持Eureka、Zookeeper、Consul 這三個註冊中心。		
		
		

10.11.3. Feign請求超時

		
方法一,修改配置是讓Hystrix的超時時間改為5秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

方法二,修改配置,禁用Hystrix的超時時間
hystrix.command.default.execution.timeout.enabled: false

方法三,修改配置,用於索性禁用feign的hystrix。
feign.hystrix.enabled: false
		
		

10.11.4. 已停止的微服務節點註銷慢或不註銷

由於 Eureka Server 清理無效節點周期長預設為90秒,可能會遇到微服務註銷慢甚至不註銷的問題。

Eureka Server 配置,注意僅適合開發環境。

		
# 設為false, 關閉自我保護, 從而保證會註銷微服務
eureka.server.enable-self-preservation=false

# 清理間隔(單位毫秒,預設是60 * 1000)
eureka.server.eviction-interval-timer-in-ms=30000
		
		

Eureka Client

配置開啟健康檢查,續約更新時間和到期時間。

		
# 設為true,開啟健康檢查(需要spring-boot-starter-actuator 依賴)
eureka.client.healthcheck.enabled=ture

# 續約更新時間間隔(預設是30秒)
eureka.instance.lease-renewal-interval-in-seconds=20000

# 續約到期時間(預設90秒)
eureka.instance.lease-expiration-duration-in-seconds=30000
		
		

10.11.5. Feign 啟動出錯 PathVariable annotation was empty on param 0.

問題分析,@PathVariable 找不到對應的參數

		
package api.feign;

import java.util.List;
import java.util.Map;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("restful-api-service")
public interface Search {
	
	@RequestMapping("/search/article/list")
	public List<Map<String, Object>> list();
	
	@RequestMapping("/search/article/{articleId}")
	public Object read(@PathVariable String articleId);
}		
		
		

解決方案

		
	@RequestMapping("/search/article/{articleId}")
	public Object read(@PathVariable("articleId") String articleId);
		
		

10.11.6. Feign 提示 Consider defining a bean of type 'common.feign.Cms' in your configuration.

背景:我們需要共用 Feign 介面,故將 Feign 放到共用的 common-version.jar 包中,供其他項目使用。

啟動提示:Consider defining a bean of type 'common.feign.Cms' in your configuration.

註解加入包位置後解決

		
@EnableFeignClients("common.feign")		
		
		

例 10.1. Share feign interface.

			
package cn.netkiller.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("common.feign")
public class Application {

	public static void main(String[] args) {
		System.out.println("Feign Starting...");
		SpringApplication.run(Application.class, args);
	}
}
			
			
			

10.11.7. Load balancer does not have available server for client

		
com.netflix.client.ClientException: Load balancer does not have available server for client: restful		
		
		

10.11.8. Eureka Client (Dalston.SR1)

10.11.8.1. Maven

			
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller.spring.cloud</groupId>
	<artifactId>eureka.client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka.client</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

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

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
			
			

10.11.8.2. Application

			
package cn.netkiller.spring.cloud.eureka.client;

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

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
			
			

10.11.8.3. RestController

			
package cn.netkiller.spring.cloud.eureka.client;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
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;

@RestController
public class TestRestController {
	private static final Logger logger = LoggerFactory.getLogger(TestRestController.class);

	@RequestMapping("/")
	public String home() {
		logger.info("Hello!!!");
		return "Hello World";
	}

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/service-instances/{applicationName}")
	public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
		return this.discoveryClient.getInstances(applicationName);
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
		@SuppressWarnings("deprecation")
		ServiceInstance instance = discoveryClient.getLocalServiceInstance();
		Integer r = a + b;
		logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
		return r;
	}
	
	@RequestMapping("/greeting")
	public String greeting() {
		return "GREETING";
	}
}
			
			
			

10.11.8.4. application.properties

			
spring.application.name=test-service
server.port=8080
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/			
			
			

10.11.8.5. 測試

首先確認客戶端已經註冊到 http://localhost:8761/

			
$ curl http://localhost:8080/service-instances/test-service

[
	{
		"host": "Neo-Desktop",
		"port": 8080,
		"secure": false,
		"uri": "http://Neo-Desktop:8080",
		"serviceId": "TEST-SERVICE",
		"metadata": {},
		"instanceInfo": {
			"instanceId": "Neo-Desktop:test-service:8080",
			"app": "TEST-SERVICE",
			"appGroupName": null,
			"ipAddr": "172.25.10.150",
			"sid": "na",
			"homePageUrl": "http://Neo-Desktop:8080/",
			"statusPageUrl": "http://Neo-Desktop:8080/info",
			"healthCheckUrl": "http://Neo-Desktop:8080/health",
			"secureHealthCheckUrl": null,
			"vipAddress": "test-service",
			"secureVipAddress": "test-service",
			"countryId": 1,
			"dataCenterInfo": {
			"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
			"name": "MyOwn"
		},
		"hostName": "Neo-Desktop",
		"status": "UP",
		"leaseInfo": {
			"renewalIntervalInSecs": 30,
			"durationInSecs": 90,
			"registrationTimestamp": 1497922681680,
			"lastRenewalTimestamp": 1497922681680,
			"evictionTimestamp": 0,
			"serviceUpTimestamp": 1497922003783
		},
		"isCoordinatingDiscoveryServer": false,
		"metadata": {},
		"lastUpdatedTimestamp": 1497922681680,
		"lastDirtyTimestamp": 1497922681025,
		"actionType": "ADDED",
		"asgName": null,
		"overriddenStatus": "UNKNOWN"
		}
	}
]
			
			

add 介面測試

			
curl http://localhost:8080/add.json?a=5&b=3

8			
			
			

10.11.9. Config Server(1.3.1.RELEASE)

10.11.9.1. Server

10.11.9.1.1. Maven
			
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller</groupId>
	<artifactId>config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config</name>
	<url>http://maven.apache.org</url>

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath />
	</parent>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-config</artifactId>
				<version>1.3.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

			
				
10.11.9.1.2. Application

Application

			
package cn.netkiller.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class Application {

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

}
			
				
10.11.9.1.3. application.properties
			
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/netkiller/config.git
			
				
10.11.9.1.4. Git 倉庫

克隆倉庫

		
git clone https://github.com/netkiller/config.git
		
				

創建配置檔案 server-development.properties

		
vim server-development.properties

test.a=KKOOKK
message=Hello world
		
				

提交配置檔案

		
git commit -a
git push
		
				
10.11.9.1.5. 測試伺服器
			
neo@netkiller $ curl http://localhost:8888/server-development.json
{"message":"Hello world","test":{"a":"KKOOKK"}}
			
				

10.11.9.2. Client

10.11.9.2.1. Maven pom.xml
			
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

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

	<dependencyManagement>
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-config</artifactId>
	            <version>1.3.1.RELEASE</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
	    </dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>			
			
				
10.11.9.2.2. Application
			
package cn.netkiller.cloud.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {

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

@RefreshScope
@RestController
class MessageRestController {

	@Value("${message:Hello default}")
	private String message;

	@RequestMapping("/message")
	String getMessage() {
		return this.message;
	}
}			
			
				

注意 @RefreshScope 註解

10.11.9.2.3. bootstrap.properties
			
spring.application.name=server-development
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false			
			
				
10.11.9.2.4. 測試 client
						
neo@netkiller $ curl http://localhost:8080/message.json
Hello world