springcloud项目搭建(Finchley.RELEASE版)

spring-cloud-demo

spring-cloud-demo demo地址:https://gitee.com/zkane/spring-cloud-demo

每个module里的README.md有详细的使用指南

如果引入的springboo版本低于2.0.3会造成在导入如下依赖时,启动报错(错误大概意思是DataSource循环依赖)

        
            org.springframework.boot
            spring-boot-starter-jdbc
        

项目介绍

项目基于springboot2.0.3和springcloud Finchley

搭建注册中心

1. 在父工程的pom.xml文件中导入依赖



    4.0.0

    com.zkane
    spring-cloud-demo
    1.0.0
    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
    
    
        UTF-8
        UTF-8
        1.8
        2.6.0
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            
            
                io.springfox
                springfox-swagger2
                ${swagger.version}
            
            
                io.springfox
                springfox-swagger-ui
                ${swagger.version}
            
        
    
    
        eureka-server
        producer-server
        consumer-server
        config-server
        config-client
        zuul-server
        auth-server
        hystrix-dashboard
        turbine
    

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

2. 在注册中心服务的pom.xml导入依赖

    
        com.zkane
        spring-cloud-demo
        1.0.0
        
    


    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

3. 在application.yml文件中配置

server:
  port: 8000
spring:
  application:
    name: eureka-server
eureka:
  client:
    # 表示是否将自己注册到Eureka Server,默认为true。
    register-with-eureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true。
    fetch-registry: false
    # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用,分隔
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/

4. 在启动类上配置注解

package com.zkane;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

搭建服务提供者

1. 在pom.xml文件中导入依赖



	4.0.0

	com.zkane
	producer-server
	1.0.0
	jar

	producer-server
	Spring Cloud Producer Server

    
        com.zkane
        spring-cloud-demo
        1.0.0
        
    

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

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

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	


2. 在application.yml配置文件中配置

server:
  port: 8100
spring:
  application:
    name: producer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3. 在启动类上添加注解

package com.zkane;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ProducerServerApplication {

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

4. 编写controller代码,提供restful风格的API访问

package com.zkane.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: [email protected]
 * @date: 2018/5/8
 */
@RestController
public class ProducerController {

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

    @GetMapping("/get")
    public String getPort() {
        return "Producer Server port: " + port;
    }
}

搭建服务消费者

1. 编写pom.xml文件



    4.0.0

    com.zkane
    consumer-server
    1.0.0
    jar

    consumer-server
    Spring Cloud Consumer Server

    
        com.zkane
        spring-cloud-demo
        1.0.0
        
    

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

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


2. 编写application.yml配置文件

server:
  port: 8200
spring:
  application:
    name: consumer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
feign:
  hystrix:
    enabled: true

3. 在启动类上添加注解

  • @EnableFeignClients注解是使用feign来调用服务提供者的API接口
package com.zkane;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerServerApplication {

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

4. 使用feign调用服务提供者的API接口

package com.zkane.remote;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author: [email protected]
 * @date: 2018/5/8
 */
@Component
@FeignClient(value = "producer-server", fallback = ProducerRemoteHystrix.class)
public interface ProducerRemote {
    @GetMapping("/get")
    String getPort();
}

5. 使用hystrix实现服务熔断机制

  • 需要在application.yml中配置feign启用hystrix
feign:
  hystrix:
    enabled: true
  • 在@FeignClient注解上添加fallback属性,指定熔断后调用的本地方法
@FeignClient(value = "producer-server", fallback = ProducerRemoteHystrix.class)
package com.zkane.remote;

import org.springframework.stereotype.Component;

/**
 * @author: [email protected]
 * @date: 2018/5/8
 */
@Component
public class ProducerRemoteHystrix implements ProducerRemote {
    @Override
    public String getPort() {
        return "Producer Server 的服务调用失败";
    }
}

6. 使用zipkin实现链路跟踪

  • 在控制台输入获取zipkin.jar的地址
    • 因为在springboot2.0以后,官方不再建议自己搭建zipkin的服务端,而是提供现成的jar包,直接运行即可.参考网址:http://www.bubuko.com/infodetail-2596757.html
curl -sSL https://zipkin.io/quickstart.sh | bash -s

在这里插入图片描述

  • 在控制台输入命令,启动zipkin的服务端。在下面截图中,我把zipkin.jar包移动到了我的E:\keluosi\zipkin下面
java -jar zipkin.jar

springcloud项目搭建(Finchley.RELEASE版)_第1张图片

  • 在页面输入zipkin服务端的地址查看zipkin的页面,控制台会打印zipkin的端口号,这里默认为9411
http://localhost:9411/zipkin

在这里插入图片描述

  • 在微服务的生产者和消费者配置zipkin
  1. 在pom.xml文件导入依赖
        
            org.springframework.cloud
            spring-cloud-starter-sleuth
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
  1. 在application.yml文件中配置zipkin的服务端地址
spring:
  zipkin:
    base-url: http://localhost:9411/
  • 打开zipkin页面就可以看到调用链情况了
    springcloud项目搭建(Finchley.RELEASE版)_第2张图片

你可能感兴趣的:(springcloud,springboot)