Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
######################
/target/www/**
/notes/**
**/target/

######################
# Node
Expand Down
25 changes: 24 additions & 1 deletion spring-cloud-mult-version-samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
在使用多版本控制时,需要修改服务提供方和服务消费方,分别是application.yaml和pom.xml。

1、在服务提供方的application.yaml中添加versions属性,标明服务支持哪些版本。
* eureka
```yaml
spring:
application:
Expand All @@ -20,8 +21,24 @@ eureka:
metadata-map:
versions: 1,2
```
* zookeeper
```yaml
spring:
application:
name: service-c
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
discovery:
register: true
root: dev
metadata:
versions: 1,2
server:
port: 10101
```

2、在服务消费方,只需要在pom.xml添加spring-cloud-starter-multi-version到pom.xml依赖中即可
2、在服务消费方,只需要在pom.xml添加spring-cloud-starter-multi-version到pom.xml依赖中即可,eureka和zookeeper的依赖二选一。
```xml
<dependencies>
<dependency>
Expand All @@ -32,10 +49,16 @@ eureka:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- eureka作为注册中心时使用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- zookeeper作为注册中心时使用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions spring-cloud-mult-version-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
<modules>
<module>spring-cloud-bamboo-eureka-samples</module>
<module>spring-cloud-bamboo-zuul-samples</module>
<module>spring-cloud-bamboo-zuul-zookeeper-samples</module>
<module>spring-cloud-bamboo-service-a-samples</module>
<module>spring-cloud-bamboo-service-b-samples</module>
<module>spring-cloud-bamboo-service-c-samples</module>
<module>spring-cloud-bamboo-service-d-samples</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.springcloud.bamboo.service.a;
package cn.springcloud.bamboo.service.c;

import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.springcloud.bamboo.service.a.web.rest;
package cn.springcloud.bamboo.service.c.web.rest;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.springcloud.bamboo.service.b;
package cn.springcloud.bamboo.service.d;

import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.springcloud.bamboo.service.b.feign;
package cn.springcloud.bamboo.service.d.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.springcloud.bamboo.service.b.rest;
package cn.springcloud.bamboo.service.d.rest;

import cn.springcloud.bamboo.service.b.feign.TestClient;
import cn.springcloud.bamboo.service.d.feign.TestClient;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-mult-version-samples</artifactId>
<groupId>cn.springcloud.gray</groupId>
<version>1.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-bamboo-service-c-samples</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!--skip deploy -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.springcloud.bamboo.service.c;

import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@EnableDiscoveryClient
public class BambooServiceCApplication {

private static final org.slf4j.Logger log = LoggerFactory.getLogger(BambooServiceCApplication.class);


public static void main(String[] args) throws UnknownHostException {
Environment env = new SpringApplicationBuilder(BambooServiceCApplication.class).web(true).run(args).getEnvironment();
log.info(
"\n----------------------------------------------------------\n\t"
+ "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://127.0.0.1:{}\n\t"
+ "External: \thttp://{}:{}\n----------------------------------------------------------",
env.getProperty("spring.application.name"), env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"));

String configServerStatus = env.getProperty("configserver.status");
log.info(
"\n----------------------------------------------------------\n\t"
+ "Config Server: \t{}\n----------------------------------------------------------",
configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.springcloud.bamboo.service.c.web.rest;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* Created by saleson on 2017/10/18.
*/
@RestController
@RequestMapping("/api/test")
public class TestResource {
@Autowired
Environment env;

@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> testGet(@RequestParam(value = "version", required = false) String version) {
return ImmutableMap.of("test", "success.", "version", StringUtils.defaultIfEmpty(version, ""), "serverPort", env.getProperty("server.port"));
}


@RequestMapping(value = "/post", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> testPost(@RequestParam(value = "version", required = false) String version, @RequestBody String body) {
return ImmutableMap.of(
"test", "success.", "version", StringUtils.defaultIfEmpty(version, ""),
"serverPort", env.getProperty("server.port"), "body", body);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
application:
name: service-c
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
discovery:
register: true
root: dev
metadata:
versions: 1,2

server:
port: 10101
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-mult-version-samples</artifactId>
<groupId>cn.springcloud.gray</groupId>
<version>1.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-bamboo-service-d-samples</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>

<dependency>
<groupId>cn.springcloud.gray</groupId>
<artifactId>spring-cloud-starter-multi-version</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!--skip deploy -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.springcloud.bamboo.service.d;

import org.slf4j.LoggerFactory;
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;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BambooServiceDApplication {

private static final org.slf4j.Logger log = LoggerFactory.getLogger(BambooServiceDApplication.class);


public static void main(String[] args) throws UnknownHostException {
Environment env = new SpringApplicationBuilder(BambooServiceDApplication.class).web(true).run(args).getEnvironment();
log.info(
"\n----------------------------------------------------------\n\t"
+ "Application '{}' is running! Access URLs:\n\t" + "Local: \t\thttp://127.0.0.1:{}\n\t"
+ "External: \thttp://{}:{}\n----------------------------------------------------------",
env.getProperty("spring.application.name"), env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"));

String configServerStatus = env.getProperty("configserver.status");
log.info(
"\n----------------------------------------------------------\n\t"
+ "Config Server: \t{}\n----------------------------------------------------------",
configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cn.springcloud.bamboo.service.d.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
* Created by saleson on 2017/11/10.
*/
@FeignClient(name = "service-c")
public interface TestClient {

@RequestMapping(path = "/api/test/get", method = RequestMethod.GET)
Map<String, String> testGet(@RequestParam(value = "version", required = false) String version);



@RequestMapping(value = "/api/test/post", method = RequestMethod.POST)
Map<String, String> testPost(@RequestParam(value = "version") String version, @RequestBody String body);

}
Loading