SpringCloud2+Consul+Feign

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

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

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

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

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

注册中心、配置中心:consule

首先看cloud-parent的配置:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
	<cloud.version>0.0.1-SNAPSHOT</cloud.version>
	<spring-cloud.version>Finchley.SR2</spring-cloud.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.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>
  
  <dependencyManagement>
    <dependencies>
	  <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-dependencies</artifactId>
		<version>${spring-cloud.version}</version>
		<type>pom</type>
		<scope>import</scope>
	  </dependency>
	</dependencies>
  </dependencyManagement>
  
  <repositories>
    <repository>
	  <id>spring-milestones</id>
	  <name>Spring Milestones</name>
	  <url>https://repo.spring.io/milestone</url>
	  <snapshots>
	    <enabled>false</enabled>
	  </snapshots>
	</repository>
  </repositories>
  
  <build>
    <plugins>
	  <plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
	  </plugin>
	</plugins>
  </build>
  
  <modules>
  	<module>cloud-config-server</module>
  	<module>cloud-auth</module>
  </modules>

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

cloud-auth项目:

pom.xml:

<parent>
    <groupId>com.xsb.cloud</groupId>
    <artifactId>cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>cloud-auth</artifactId>
  <description>统一安全认证、授权中心</description>
  
  <dependencies>
    <!-- 配置文件客户端 -->
  	<dependency>  
	   <groupId>org.springframework.cloud</groupId>  
	   <artifactId>spring-cloud-config-client</artifactId> 
	</dependency>
  	<!-- web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<!-- redis -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>
	<!-- 公共资源池,可以快速创建自己的对象池 -->
    <dependency>
	  <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
    <!-- consul -->
    <dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-starter-consul-discovery</artifactId>
	</dependency>
	<!-- feign -->
	<dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-starter-openfeign</artifactId>
	</dependency>
	<!-- actuator -->
	<dependency>
      <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<!-- turbine -->
	<dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-netflix-turbine</artifactId>
	</dependency>
	<!-- hystrix -->
	<dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	</dependency>
  </dependencies>
  
  
  <build>
	<plugins>
	  <plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
	  </plugin>
	</plugins>
  </build>

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;
	}
	
	/**
	 * <p>Title: testFeign</p>
	 * <p>Description: 测试feign</p>
	 * <p>Date: 2018年12月6日 下午1:56:03</p>
	 * @param name
	 * @return
	 */
	@RequestMapping("/feign")
	public String testFeign(@RequestParam(name="name") String name){
		return testFeign.testFeign(name);
	}
}

cloud-config-server项目:

<parent>
    <groupId>com.xsb.cloud</groupId>
    <artifactId>cloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>cloud-config-server</artifactId>
  <name>config-server</name>
  <description>云平台配置中心</description>
  
	<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>
		<!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul</artifactId>
        </dependency>
        
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-discovery</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>

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

		<!--consul中健康检查需要用到actuator,不添加会check failing-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

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

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

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;
	}
	
	/**
	 * <p>Title: testFeign</p>
	 * <p>Description: 测试feign</p>
	 * <p>Date: 2018年12月6日 下午1:53:23</p>
	 * @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注册了:

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

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

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

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值