Skip to content

Commit

Permalink
提交整个示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
CD826 authored and CD826 committed Jul 31, 2017
1 parent 6e9fbf6 commit 5990d5b
Show file tree
Hide file tree
Showing 135 changed files with 4,386 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -31,12 +32,18 @@
@SpringBootApplication
public class Application {

@Bean
@Bean(value = "restTemplate")
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

@Primary
@Bean(value = "lbcRestTemplate")
RestTemplate lbcRestTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;


Expand All @@ -37,8 +39,13 @@ public class HelloController {
protected Logger logger = LoggerFactory.getLogger(HelloController.class);

@Autowired
@Qualifier(value = "restTemplate")
private RestTemplate restTemplate;

@Autowired
@Qualifier(value = "lbcRestTemplate")
private RestTemplate lbcRestTemplate;

@Autowired
private LoadBalancerClient loadBalancerClient;

Expand All @@ -52,6 +59,6 @@ public String helloEx() {
ServiceInstance instance = this.loadBalancerClient.choose("SERVICE-HELLO");
URI helloUri = URI.create(String.format("http://%s:%s/hello", instance.getHost(), instance.getPort()));
logger.info("Target service uri = {}. ", helloUri.toString());
return new RestTemplate().getForEntity(helloUri, String.class).getBody();
return this.lbcRestTemplate.getForEntity(helloUri, String.class).getBody();
}
}
5 changes: 5 additions & 0 deletions eureka/service-discovery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ server.port=8260
# eureka server注册的name,唯一标识
spring.application.name=service-discovery

security.basic.enabled=true
security.user.name=cd826
security.user.password=pwd

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
eureka.client.service-url.defaultZone=http://cd826:pwd@${eureka.instance.hostname}:${server.port}/eureka

logging.level.org.springframework=INFO

logging.level.org.springframework=INFO
2 changes: 1 addition & 1 deletion feign/mall-web/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
server.port=8080

spring.application.name=mall-web
spring.application.name=MALL-WEB

eureka.client.service-url.defaultZone=http://localhost:8260/eureka

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Product {
private String bandName; // 产品品牌名称
private int price; // 产品价格(分)

// ==================================================================
// constructor ======================================================
// ========================================================================
// constructor ============================================================
public Product() {
}

Expand Down
6 changes: 6 additions & 0 deletions hystrix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# TwoStepsFromJava-Cloud-Feign

Spring Cloud 示例项目:结合Ribbon和Feign实现声明式服务调用。


Spring Cloud Release Trains: [Dalston.SR1](http://projects.spring.io/spring-cloud/)
53 changes: 53 additions & 0 deletions hystrix/mall-web/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>twostepsfromjava.cloud</groupId>
<artifactId>twostepsfromjava-cloud-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>

<artifactId>mall-web</artifactId>
<name>Spring Cloud Sample Projects: Mall</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</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-feign</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>product-service-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.twostepsfromjava.cloud.web.mall;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**
* TwoStepsFromJava Cloud -- Mall Web Project
*
* @author CD826([email protected])
* @since 1.0.0
*/
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/***
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.twostepsfromjava.cloud.web.mall.controller;

import io.twostepsfromjava.cloud.product.dto.Product;
import io.twostepsfromjava.cloud.web.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
* Product Controller
*
* @author CD826([email protected])
* @since 1.0.0
*/
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;

@RequestMapping(method = RequestMethod.GET)
public List<Product> list() {
return this.productService.findAll();
}

@RequestMapping(value = "/{itemCode}", method = RequestMethod.GET)
public Product detail(@PathVariable String itemCode) {
return this.productService.loadByItemCode(itemCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.twostepsfromjava.cloud.web.mall.service;

import io.twostepsfromjava.cloud.product.dto.Product;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


/**
* Product Service
*
* @author CD826([email protected])
* @since 1.0.0
*/
@FeignClient(name = "PRODUCT-SERVICE", fallback = ProductServiceFallback.class)
public interface ProductService {

@RequestMapping(value = "/products", method = RequestMethod.GET)
List<Product> findAll();

@RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET)
Product loadByItemCode(@PathVariable("itemCode") String itemCode);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.twostepsfromjava.cloud.web.mall.service;

import io.twostepsfromjava.cloud.product.dto.Product;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Collections;
import java.util.List;


/**
* Product Service Fallback
*
* @author CD826([email protected])
* @since 1.0.0
*/
@Component
public class ProductServiceFallback implements ProductService {
@Override
public List<Product> findAll() {
return Collections.emptyList();
}

@Override
public Product loadByItemCode(@PathVariable("itemCode") String itemCode) {
return new Product("error", "未知", "TwoStepsFromJava-Fallback", 0);
}
}
9 changes: 9 additions & 0 deletions hystrix/mall-web/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server.port=8080

spring.application.name=MALL-WEB

feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8260/eureka

logging.level.org.springframework=INFO
13 changes: 13 additions & 0 deletions hystrix/mall-web/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=======================================================================================================================
== ==
== ==
== _______ _____ ______ _ _____ _ ____ _ _ _____ ==
== |__ __|/ ____|| ____| | | / ____|| | / __ \ | | | || __ \ ==
== | | | (___ | |__ | | ______ | | | | | | | || | | || | | | ==
== | | \___ \ | __|_ | ||______|| | | | | | | || | | || | | | ==
== | | ____) || | | |__| | | |____ | |____| |__| || |__| || |__| | ==
== |_| |_____/ |_| \____/ \_____||______|\____/ \____/ |_____/ -- Consumer ==
== ==
== ==
=======================================================================================================================
:: twostepsfromjava.io :: v1.0.0.BUILD-SNAPSHOT BUILD-BY: CD826
Loading

0 comments on commit 5990d5b

Please sign in to comment.