Skip to content

Commit

Permalink
Spring Cloud Consul服务治理
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Mar 28, 2019
1 parent 97982aa commit 33b935b
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 55.Spring-Cloud-Consul/server-consumer/pom.xml
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>
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);
}
}
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);
// }

}
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}
58 changes: 58 additions & 0 deletions 55.Spring-Cloud-Consul/server-proivder/pom.xml
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>
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);
}
}
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";
}
}
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

0 comments on commit 33b935b

Please sign in to comment.