目录:
springcloud费话之Eureka服务访问(restTemplate)
springcloud费话之Eureka接口调用(feign)
springcloud费话之断路器(hystrix in feign)
springcloud的配置中心,即config-server的端口只能之8888的问题比较恶心
原因在于spring代码中写死了每一个获取配置的客户端,都是向8888请求,因此问题出在客户端上
但是服务端也要进行一定程度的修改
思路:
修改config服务端端口,改为非8888端口
修改config客户端获取配置的方式,从固定的ip和端口,修改为通过eureka注册中心,通过注册的名称来获取
将config的服务端添加进eureka注册中心
实际代码如下:
修改config-server的配置如下:
1server: 2 port: 9999 3 tomcat: 4 max-threads: 10000 5 max-connections: 20000 6 7eureka: 8 client: 9 serviceUrl: 10 defaultZone: http://localhost:10086/eureka/ 11 12spring: 13 application: 14 name: config-server 15 profiles: 16 active: subversion 17 cloud: 18 config: 19 server: 20 svn: 21 uri: https://xxxxxx/svn/liuyuhang_FM/configCenter/ 22 username: liuyuhang 23 password: xxxxxx 24 search-paths: null 25 default-label: testConfig 26 basedir: /data
修改config-client配置如下:
1server: 2 port: 8889 3 tomcat: 4 max-threads: 10000 5 max-connections: 20000 6spring: 7 application: 8 name: config-client 9 cloud: 10 config: 11 discovery: 12 enabled: true 13 service-id: config-server 14 15 profiles: 16 active: dev 17 18eureka: 19 client: 20 serviceUrl: 21 defaultZone: http://localhost:10086/eureka/
注意spring的application的name一定要正确
注意config-client中的discovery中,要enable为true,表示可以被发现
然后指定service-id,来代替之前的uri配置即可
因为config-server中添加了eureka作为客户端,pom需要引入eureka的内容,节选如下:
1 <!-- eureka server的jar, 作为client也需要 --> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 5 </dependency> 6 <!-- eureka client的jar --> 7 <dependency> 8 <groupId>org.springframework.cloud</groupId> 9 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
添加了以后,config-server还需要对启动入口添加eureka的client的注解,节选代码如下:
1@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 2@EnableEurekaClient 3@EnableConfigServer 4public class ConfigApplication extends SpringBootServletInitializer { 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigApplication.class, args); 7 } 8}
以上~