forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97982aa
commit 33b935b
Showing
8 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>server-consumer</artifactId> | ||
<version>1.0</version> | ||
<name>server-consumer</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-consul-discovery</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> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...loud-Consul/server-consumer/src/main/java/com/example/demo/ServerConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
|
||
@EnableDiscoveryClient | ||
@SpringBootApplication | ||
public class ServerConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ServerConsumerApplication.class, args); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
55.Spring-Cloud-Consul/server-consumer/src/main/java/com/example/demo/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.example.demo; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.discovery.DiscoveryClient; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@RestController | ||
public class TestController { | ||
|
||
private Logger loggr = LoggerFactory.getLogger(this.getClass()); | ||
|
||
@Autowired | ||
private DiscoveryClient discoveryClient; | ||
@Autowired | ||
private LoadBalancerClient loadBalancerClient; | ||
|
||
private final RestTemplate restTemplate = new RestTemplate(); | ||
|
||
// @Autowired | ||
// private RestTemplate restTemplate; | ||
|
||
private static final String SERVER_ID = "server-provider"; | ||
|
||
@GetMapping("uri") | ||
public List<URI> getServerUris() { | ||
return this.discoveryClient.getInstances(SERVER_ID) | ||
.stream() | ||
.map(ServiceInstance::getUri).collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("hello") | ||
public String hello() { | ||
ServiceInstance instance = loadBalancerClient.choose(SERVER_ID); | ||
String url = instance.getUri().toString() + "/hello"; | ||
loggr.info("remote server url:{}", url); | ||
return restTemplate.getForObject(url, String.class); | ||
} | ||
|
||
// @Bean | ||
// @LoadBalanced | ||
// public RestTemplate restTemplate() { | ||
// return new RestTemplate(); | ||
// } | ||
|
||
// @GetMapping("hello") | ||
// public String hello() { | ||
// String url = "http://" + SERVER_ID + "/hello"; | ||
// return restTemplate.getForObject(url, String.class); | ||
// } | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
55.Spring-Cloud-Consul/server-consumer/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
server: | ||
port: 9002 | ||
|
||
spring: | ||
application: | ||
name: server-consumer | ||
|
||
cloud: | ||
consul: | ||
host: 192.168.140.215 | ||
port: 8500 | ||
discovery: | ||
service-name: ${spring.application.name} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>server-provider</artifactId> | ||
<version>1.0</version> | ||
<name>server-provider</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-consul-discovery</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> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...loud-Consul/server-proivder/src/main/java/com/example/demo/ServerProviderApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
|
||
@EnableDiscoveryClient | ||
@SpringBootApplication | ||
public class ServerProviderApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(ServerProviderApplication.class, args); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
55.Spring-Cloud-Consul/server-proivder/src/main/java/com/example/demo/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.demo; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@RestController | ||
public class TestController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
@GetMapping("check") | ||
private String check() { | ||
logger.info("health check"); | ||
return "ok"; | ||
} | ||
|
||
@GetMapping("hello") | ||
public String hello() { | ||
logger.info("hello"); | ||
return "hello from server provider"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
55.Spring-Cloud-Consul/server-proivder/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
server: | ||
port: 9000 | ||
spring: | ||
application: | ||
name: server-provider | ||
|
||
cloud: | ||
consul: | ||
host: 192.168.140.215 | ||
port: 8500 | ||
discovery: | ||
health-check-interval: 10s | ||
service-name: ${spring.application.name} | ||
register-health-check: true | ||
health-check-path: /check |