SpringCloud2+Consul+Feign

      由于之前用的SpringBoot是1.5.X版本,现在公司项目都是2.0版本,且最近Eureka2.0版本停止维护了,所以采用Consul替换Eureak,实现注册中心的功能。

代码主要分为以下几个部分:

SpringCloud2+Consul+Feign_第1张图片

cloud-parent是我计划做的云平台;

cloud-auth是我计划做的授权中心,目前只是添加了redis、feign和consul注册;

cloud-config-server是我计划做的配置中心;

注册中心、配置中心:consule

首先看cloud-parent的配置:


    org.springframework.boot
    spring-boot-starter-parent
    2.0.3.RELEASE
     
  
  
  
    UTF-8
   	UTF-8
	1.8
	0.0.1-SNAPSHOT
	Finchley.SR2
  
  
  
	
	  org.springframework.boot
      spring-boot-starter
	
	
	  org.springframework.boot
      spring-boot-starter-test
	  test
	
	
      org.projectlombok
      lombok
    
  
  
  
    
	  
		org.springframework.cloud
		spring-cloud-dependencies
		${spring-cloud.version}
		pom
		import
	  
	
  
  
  
    
	  spring-milestones
	  Spring Milestones
	  https://repo.spring.io/milestone
	  
	    false
	  
	
  
  
  
    
	  
		org.springframework.boot
		spring-boot-maven-plugin
	  
	
  
  
  
  	cloud-config-server
  	cloud-auth
  

这里一定要注意springboot的版本,我之前用2.1.1.RELEASE的版本,导致一直注册不了consul,耽误了大半天时间!

cloud-auth项目:

pom.xml:


    com.xsb.cloud
    cloud-parent
    0.0.1-SNAPSHOT
  
  cloud-auth
  统一安全认证、授权中心
  
  
    
  	  
	   org.springframework.cloud  
	   spring-cloud-config-client 
	
  	
    
      org.springframework.boot
      spring-boot-starter-web
	
	
    
      org.springframework.boot
	  spring-boot-starter-data-redis
	
	
    
	  org.apache.commons
      commons-pool2
    
    
    
	  org.springframework.cloud
	  spring-cloud-starter-consul-discovery
	
	
	
	  org.springframework.cloud
	  spring-cloud-starter-openfeign
	
	
	
      org.springframework.boot
	  spring-boot-starter-actuator
	
	
	
	  org.springframework.cloud
	  spring-cloud-netflix-turbine
	
	
	
	  org.springframework.cloud
	  spring-cloud-starter-netflix-hystrix
	
  
  
  
  
	
	  
		org.springframework.boot
		spring-boot-maven-plugin
	  
	
  

bootstrap.yml:

server:
  port: 9004
spring:
  application:
    name: cloud-auth-server
  cloud:
    consul:
      host: localhost
      port: 8500
      enabled: true #是否启用consul
      discovery:
        enable: true
        register: true
        healthCheckPath: /test #健康检查链接
        healthCheckInterval: 15s #监控检查频率
        ip-address: true
        instance-id: ${spring.application.name}:${spring.cloud.client.ipaddress}:${spring.application.instance_id:${server.port}}

这里配置也要注意2点,第一是healthCheckPath,默认是/health,改成/test后,那后台就要写个/test的映射,目的是为了快速就行健康检查验证,快速返回,还有一个比较坑的是instance-id配置,spring.cloud.client.ipaddress之前配置的是spring.cloud.client.ipAddress,cloud2不能识别,consul直接显示spring-cloud-client-ipAddress,而不是IP地址,只要改成小写才能被识别为IP地址;

application.yml:

spring:
  profiles:
    active: native
  redis:
    host: localhost
    password: admin
    port: 6379
    timeout: 10000 #超时时间
    database: 10 #默认是0,可以自己随意配置,不要与其他平台冲突
    lettuce:
      pool:
        max-active: -1 #最大连接数,暂时配置为无限制
        max-wait: -1 #最大阻塞等待时间,无限制
        max-idle: 8 #最大空闲连接,默认8
        min-idle: 0 #最小空闲连接,默认0

启动类AuthApp:

@SpringBootApplication
@EnableDiscoveryClient //启用服务发现
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
public class AuthApp {
	
	public static void main(String[] args) {
		SpringApplication.run(AuthApp.class, args);
	}
	
}

TestFeign:调用cloud-confi-server服务的/feign接口

@FeignClient(value="cloud-config-server",fallback = TestFeignBack.class)
public interface TestFeign {
	@RequestMapping(value = "/feign",method = RequestMethod.GET)
	String testFeign(@RequestParam(value = "name") String name);
}

TestFeignBack:

public class TestFeignBack implements TestFeign{

	@Override
	public String testFeign(String name) {
		return "sorry,熔断介入";
	}

}

测试Controller:

@RestController
public class IndexController {
	@Autowired
	TestFeign testFeign;
	
	/**
	 * 配置健康检查
	 * @return
	 */
	@RequestMapping("/test")
	public int health(){
		return HttpStatus.SC_OK;
	}
	
	/**
	 * 

Title: testFeign

*

Description: 测试feign

*

Date: 2018年12月6日 下午1:56:03

* @param name * @return */ @RequestMapping("/feign") public String testFeign(@RequestParam(name="name") String name){ return testFeign.testFeign(name); } }

cloud-config-server项目:


    com.xsb.cloud
    cloud-parent
    0.0.1-SNAPSHOT
  
  cloud-config-server
  config-server
  云平台配置中心
  
	
		UTF-8
		UTF-8
		1.8
	

	
		
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-consul
        
        
		
			org.springframework.cloud
			spring-cloud-starter-consul-config
		
		
			org.springframework.cloud
			spring-cloud-starter-consul-discovery
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-hystrix
		
		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
            org.springframework.boot
            spring-boot-actuator
        

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		    org.springframework.cloud
		    spring-cloud-netflix-turbine
		
		
	

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

bootstrap.yml:

server:
  port: 18007
spring:
  application:
    name: cloud-config-server
  cloud:
    consul:
      enabled: true #是否启用consul
      config:
        enabled: true #启用consul配置
#        format: YAML
        prefix: cloud #配置中心文件夹前缀 
        defaultContext: ${spring.application.name} #指定consul配置的配置文件父路径
        data-key: conf #指定consul配置的配置文件为conf,默认是data
#        failFast: false
      host: ${CONSUL_HOST:localhost}
      port: ${CONSUL_PORT:8500}

application.yml

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      enabled: true #是否启用consul
      discovery:
        enable: true
        register: true
        healthCheckPath: /test #健康检查链接
        healthCheckInterval: 15s #监控检查频率
        ip-address: true
        instance-id: ${spring.application.name}:${spring.cloud.client.ipaddress}:${spring.application.instance_id:${server.port}}

启动类:ConfigServerApp

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties({TestConfig.class})
@ComponentScan(value="com.leador.cloud.config")
public class ConfigServerApp{
	
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }

}

web测试类:TestController

@RestController
public class TestController {

	@Autowired
	TestConfig testConfig;
	
	/**
	 * 配置健康检查
	 * @return
	 */
	@RequestMapping("/test")
	public int health(){
		return HttpStatus.SC_OK;
	}
	
	/**
	 * 

Title: testFeign

*

Description: 测试feign

*

Date: 2018年12月6日 下午1:53:23

* @param name * @return */ @RequestMapping("/feign") public String testFeign(@RequestParam(name="name") String name){ return "cloud-config-server-2:"+name; } }

启动cloud-config-server和cloud-auth项目,发现在consul注册了:

SpringCloud2+Consul+Feign_第2张图片

直接请求auth-server的feign测试链接,看是否返回cloud-config-server的消息:

SpringCloud2+Consul+Feign_第3张图片

SpringCloud2+Consul+Feign_第4张图片

至此基本实现了注册服务和feign服务调用;

题外话:您可以在本机部署多个cloud-config-server服务,只需要改一下端口,每次请求auth-server的时候,都会通过本地选举来选择请求哪个服务,相当于实现了负载均衡;

 

 

你可能感兴趣的:(Spring,Cloud,JAVA)