# spring-cloud-demo **Repository Path**: chansn/spring-cloud-demo ## Basic Information - **Project Name**: spring-cloud-demo - **Description**: 项目基于springboot2.0和springcloud Finchley.RC1 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 26 - **Created**: 2018-05-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-cloud-demo ## 项目介绍 项目基于springboot2.0和springcloud Finchley.RC1 ## 搭建注册中心 ### 1. 在pom.xml文件中导入依赖 ``` 4.0.0 com.zkane eureka-server 1.0.0 jar eureka-server Spring Cloud Eureka Server org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 Finchley.RC1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false ``` ### 2. 在application.yml文件中配置 ``` server: port: 8000 spring: application: name: eureka-server #eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。 #eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。 #eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔 eureka: client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://localhost:${server.port}/eureka/ ``` ### 3. 在启动类上配置注解 ``` 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 org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 Finchley.RC1 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false ``` ### 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: keluosi@bicitech.cn * @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 org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 Finchley.RC1 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 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.cloud spring-cloud-openfeign 2.0.0.RC1 pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false ``` ### 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: keluosi@bicitech.cn * @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: keluosi@bicitech.cn * @date: 2018/5/8 */ @Component public class ProducerRemoteHystrix implements ProducerRemote { @Override public String getPort() { return "Producer Server 的服务调用失败"; } } ```