# SpringCloudAll
**Repository Path**: bgt0314/SpringCloudAll
## Basic Information
- **Project Name**: SpringCloudAll
- **Description**: SpringCloud学习示例
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 2
- **Created**: 2017-10-31
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SpringCloud概述
[dubbo与SpringCloud](http://mp.weixin.qq.com/s?__biz=MzI3NjU2ODA5Mg==&mid=2247483877&idx=1&sn=3b5bfea62d3bd9da26870a337f784137&chksm=eb72c096dc0549800ca181b387ba63fcf5cd6dfd52ee034ffc629588dcf65e07331b9086ccf6&mpshare=1&scene=23&srcid=1030Xhh3Du7yAbMeNbVPSUhm#rd)
# 注册中心
> SpringCloud支持eureka、zookeeper等方式的服务发现组件。
## eureka 服务
> 一个简单的注册中心实现
- [官方示例说明](http://cloud.spring.io/spring-cloud-static/Dalston.SR4/single/spring-cloud.html#_spring_cloud_netflix)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-eureka-server
```
- 相关配置
- yml配置
```xml
spring:
application:
name: eurekacenter
server:
port: 10001
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
```
- java配置
```java
package com.ricky;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Created by baiguantao on 2017/10/25.
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args);
}
}
```
## eureka 客户端
- [eureka客户端官方演示](https://cloud.spring.io/spring-cloud-netflix/)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-eureka
```
- 相关配置
- Java配置
```java
package com.ricky;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Created by baiguantao on 2017/10/25.
*/
@SpringBootApplication
@EnableEurekaClient
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
}
```
- yml配置
```xml
spring:
application:
name: eurekaclient1
server:
port: 20001
eureka:
client:
service-url:
defaultZone: http://10.1.1.29:10001/eureka/
```
## ribbon
> 客户端负载均衡器
- [ribbon官方示例](https://spring.io/guides/gs/client-side-load-balancing/)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
```
- 相关配置
- java配置
```java
package com.ricky;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by baiguantao on 2017/10/26.
*/
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class ribbonApplication {
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(@RequestParam(value="name", defaultValue="ricky") String name) {
String greeting = this.restTemplate.getForObject("http://eurekaclient/greeting", String.class);
return String.format("%s, %s!", greeting, name);
}
public static void main(String[] args) {
new SpringApplicationBuilder(ribbonApplication.class).web(true).run(args);
}
}
```
- yml配置
```xml
spring:
application:
name: ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
server:
port: 30001
```
## feign
> 声明式HTTP客户端
- [feign官方示例](https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#spring-cloud-feign)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-feign
```
- 相关配置
- java配置
> 启动配置信息
```java
package com.ricky.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* Created by baiguantao on 2017/10/26.
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(FeignApplication.class)
.web(true)
.run(args);
}
}
```
> 接口调用相关
```java
package com.ricky.cloud.config;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Created by baiguantao on 2017/10/26.
*/
@FeignClient("eurekaclient")
public interface FeignClientConfig {
@GetMapping(value = "/greeting")
public String greet();
}
```
> 调用示例
```java
package com.ricky.cloud.controller;
import com.ricky.cloud.config.FeignClientConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by baiguantao on 2017/10/26.
*/
@RestController
public class ClientController {
@Autowired
FeignClientConfig clientConfig;
@GetMapping("/greeting")
public String greeting(){
return clientConfig.greet();
}
}
```
- yml配置
```xml
server:
port: 40001
spring:
application:
name: feign
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
```
## ribbon-hystrix
> 熔断器 进行降级 防止灾难蔓延
- [ribbon-hystrix](https://spring.io/guides/gs/circuit-breaker/)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.boot
spring-boot-starter-actuator
```
- 相关配置
- java配置
```java
package com.ricky;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by baiguantao on 2017/10/26.
*/
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class ribbonApplication {
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(@RequestParam(value="name", defaultValue="ricky") String name) {
String greeting = this.restTemplate.getForObject("http://eurekaclient/greeting", String.class);
return String.format("%s, %s!", greeting, name);
}
public static void main(String[] args) {
new SpringApplicationBuilder(ribbonApplication.class).web(true).run(args);
}
}
```
- yml配置
```xml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
server:
port: 30011
spring:
application:
name: ribbon_hystrix
```
## zuul
> API GATEWAY
- [zuul官方文档](https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_router_and_filter_zuul)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.boot
spring-boot-starter-actuator
```
- 相关配置
- java配置
```java
package com.ricky;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* Created by baiguantao on 2017/10/25.
*
* 访问:http://localhost:20011/eurekaclient/greeting
*/
@SpringBootApplication
@EnableZuulProxy
public class EurekaZuulApplication {
private static Logger log = LoggerFactory.getLogger(EurekaZuulApplication.class);
public static void main(String[] args) {
SpringApplication.run(EurekaZuulApplication.class, args);
}
}
```
- yml配置
```xml
spring:
application:
name: eureka_zuul
server:
port: 20011
eureka:
client:
service-url:
defaultZone: http://10.1.1.29:10001/eureka/
# 下面整个树都非必须,如果不配置,将默认使用 http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/** 路由到:http://想要访问的Eureka服务id的小写:该服务端口/**
zuul:
routes:
user:
path: /user/** #http://localhost:20011/user/greeting === http://localhost:20011/eurekaclient/greeting
service-id: eurekaclient
```
## config
> configServer
- [configServer官方示例](http://cloud.spring.io/spring-cloud-static/Dalston.SR4/single/spring-cloud.html#_spring_cloud_config)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-actuator
```
- 相关配置
- java配置
```java
package com.ricky;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Created by baiguantao on 2017/10/30.
*
*
* http://localhost:50001/abc-dev.yml
* scconfig_dev
* http://localhost:50001/abc-dd.yml
* sc_config_default
*
* http://localhost:50001/abc/dev/master
*
* {
name: "abc",
profiles: [
"dev"
],
label: "master",
version: "80017dd2338c1360c95800f5c546d6eb6391c5c5",
state: null,
propertySources: [
{
name: "https://gitee.com/bgt0314/sc_config/application-dev.yml",
source: {
document: "scconfig_dev"
}
},
{
name: "https://gitee.com/bgt0314/sc_config/application.yml",
source: {
document: "sc_config_default"
}
}
]
}
http://localhost:50001/abc/dd/master
{
name: "abc",
profiles: [
"dd"
],
label: "master",
version: "80017dd2338c1360c95800f5c546d6eb6391c5c5",
state: null,
propertySources: [
{
name: "https://gitee.com/bgt0314/sc_config/application.yml",
source: {
document: "sc_config_default"
}
}
]
}
*/
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigServerApplication.class)
.web(true)
.run(args);
}
}
```
- yml配置
```xml
server:
port: 50001
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: https://gitee.com/bgt0314/sc_config
eureka:
client:
service-url:
defaultZone: http://10.1.1.29:10001/eureka/
instance:
prefer-ip-address: true
```
> config客户端
- [config客户端](https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html)
- 引入依赖
```xml
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
```
- 相关配置
- java配置
```java
package com.ricky;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigClientApplication.class)
.web(true)
.run(args);
}
}
```
```java
package com.ricky;
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;
/**
* Created by baiguantao on 2017/10/31.
*
* http://localhost:50002/value
* http://localhost:50002/refresh post
* management:
security:
enabled: false
*/
@RestController
@RefreshScope
public class ProfileController {
@Value("${name}")
private String value;
@GetMapping("/value")
public String getValue(){
return value;
}
}
```
- yml配置
```xml
server:
port: 50002
spring:
application:
name: configclient
cloud:
config:
name: ricky
profile: dev
label: master
discovery:
enabled: true
service-id: configserver
eureka:
client:
service-url:
defaultZone: http://10.1.1.29:10001/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
```
nohup java -jar XXX.jar >temp.txt &