springcloud 学习碰到的问题整合(1-8)

springcloud教程:http://blog.csdn.net/forezp/article/details/70148833


一路按教程做下来,碰到以下的问题:

1.在feign中使用熔断器hystrix,但是一直不起作用。因为在D版本的Spring Cloud中,它没有默认打开。

解决:在yml文件中加入

feign:
  hystrix:
    enabled: true
注意yml中不识别tab的空格。

做完以上工作,重启服务即可。


2.feign  hystrix dashboard 断路器  仪表盘无法打开

解决:a.pom添加一下依赖

	
        
		org.springframework.cloud
		spring-cloud-starter-hystrix
	
	
	
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
        
        
	
            org.springframework.boot
            spring-boot-starter-actuator
        
b.启动类添加一下注释

@EnableHystrix@EnableHystrixDashboard

3.zuul 

maven中直接添加zuul依赖无法成功,需要手动添加到maven库。



	org.springframework.cloud
	spring-cloud-starter-zuul
	1.4.3.RELEASE

		


	com.netflix.zuul
	zuul-core
	1.3.0

4.分布式配置中心(Spring Cloud Config)

1) server:

关于server部分,主要注意一下,uri和对应的git中的命名规则组合。

SpringcloudConfig/respo/config-client-dev.properties

想取到config-client-dev.properties,则uri对应的为:http://localhost:8888/config-client/dev

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/forezp/SpringcloudConfig/
          searchPaths: respo
          #uri: https://github.com/eddie-code/SpringCloudDemo
          #searchPaths: config-repo
    label: master
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2)client

关于client,主要注意bootstrap.yml的优先级高于application.yml,所以关于外部配置写在bootstrap.yml中。

server:
  port: 8881
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        serviceId: config-server
        enabled: true
遵循上述请求地址和资源文件的映射关系,即查找的文件名为
config-client-dev.properties
@Value("${foo}")
    private String foo;

    @Value("${democonfigclient.message}")
    private String message;


    @GetMapping("/configs")
    public String configs() {
        return "This is " + foo + " ; Message: " + message;
    }
此处@value中的值要与资源文件中的key相匹配

8.消息总线(Spring Cloud Bus)

此处整合rabbitMq。RabbitMQ系列(一):Windows下RabbitMQ安装及入门

安装完毕,此处直接使用下图架构:

springcloud 学习碰到的问题整合(1-8)_第1张图片

一)、springcloud_EurekaConfigServer:

1)、pom.xml

添加spring-cloud-starter-bus-amqp依赖



    4.0.0


    com.forezp
    service-ConfigServer
    0.0.1-SNAPSHOT
    jar


    service-ConfigServer
    Demo project for Spring Boot


    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    


    
        UTF-8
        UTF-8
        1.8
    


    
		
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
    	
    	
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        


        
		
		
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
		
            org.springframework.retry
            spring-retry
        
        
        
        
            org.springframework.cloud
            spring-cloud-config-monitor
        
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
            org.springframework.boot
            spring-boot-starter-aop
        


		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
            org.springframework.boot
            spring-boot-starter-actuator
        
        
          
          
            org.springframework  
            springloaded
        
        
		    org.springframework.boot
		    spring-boot-devtools
		    true
		    true
		
    


	
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Camden.SR1
                pom
                import
            
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

2)、eurekaConfingServerApplication:

package com.imm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

3)、application-dev.yml

此处配置rabbitmq对应的配置参数,和

management: #关闭安全验证
  security:
     enabled: false
server:
  port: 8910
spring:
  application:
    name: eureka-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/nihaomashaonian/cloud-demo
          searchPaths: TIMMY
          #uri: https://github.com/forezp/SpringcloudConfig/
          #searchPaths: respo
    label: master
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8900/eureka/
management: #关闭安全验证
  security:
     enabled: false
 

二)、springcloud_EurekaConfigClient:

1)、pom.xml



	4.0.0

	com.forezp
	config-client
	0.0.1-SNAPSHOT
	jar

	config-client
	Demo project for Spring Boot
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
		
			org.springframework.cloud
			spring-cloud-starter-config
		
		
		
		
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
		
		
            org.springframework.retry
            spring-retry
        
        
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
            org.springframework.boot
            spring-boot-starter-aop
        

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
            org.springframework.boot
            spring-boot-starter-actuator
        
		
		
		  
          
            org.springframework  
            springloaded
        
        
		    org.springframework.boot
		    spring-boot-devtools
		    true
		    true
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR1
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	

2)、eurekaConfingClientApplication

package com.imm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

3)、HelloControler

这里@RefreshScope是重点,即代码中需要动态刷新配置,在需要的类上加上该注解就行,

开始尝试很多次,始终接收不到参数

Received remote refresh request. Keys refreshed []

在对应的类中加入@RefreshScope 即可。

正确的客户端打印出的log应为:Received remote refresh request. Keys refreshed [foo]

package com.imm.amo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class HelloControler {
	
	@Value("${foo}")
    private String foo;

    @Value("${timmy.message}")
    private String message;
    
    @Value("${server.port}")
    String port;

    @GetMapping("/configs")
    public String configs() {
        return "This is " + foo + " ;port: "+port+"; Message: " + message;
    }
}

4)、bootstrap-local.yml

server:
  port: 8911
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8900/eureka/
spring:
  application:
    name: timmy-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:8910/
      discovery:
        serviceId: eureka-config-server   #服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。
        enabled: true
其他关于rabbitmq的配置在客户端不用配置。


参考:https://www.cnblogs.com/ityouknow/p/6931958.html   关于最后Eureka会重启的BUG



你可能感兴趣的:(spring,cloud)