├── README.md
├── config-repo
└── springcloud-config-client-dev.properties
├── .gitignore
├── hello-service
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── loafer
│ │ │ └── demo
│ │ │ └── sc
│ │ │ └── provider
│ │ │ ├── entity
│ │ │ ├── NestedBean.java
│ │ │ └── JavaBean.java
│ │ │ ├── Application.java
│ │ │ └── web
│ │ │ ├── HelloController.java
│ │ │ └── ConvertController.java
│ │ └── resources
│ │ └── application.properties
└── pom.xml
├── feign-consumer
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── loafer
│ │ │ └── demo
│ │ │ └── sc
│ │ │ └── consumer
│ │ │ ├── entity
│ │ │ ├── NestedBean.java
│ │ │ └── JavaBean.java
│ │ │ ├── service
│ │ │ ├── HelloService.java
│ │ │ └── ConvertService.java
│ │ │ ├── formatter
│ │ │ ├── FeignFormatterRegistrarImpl.java
│ │ │ └── DateJsonSerializer.java
│ │ │ ├── FeignApplication.java
│ │ │ └── web
│ │ │ ├── HelloController.java
│ │ │ └── CustomerConvertController.java
│ │ └── resources
│ │ └── application.properties
└── pom.xml
├── ribbon-consumer
├── src
│ └── main
│ │ ├── resources
│ │ └── application.properties
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── loafer
│ │ └── demo
│ │ └── sc
│ │ └── consumer
│ │ ├── service
│ │ └── HelloService.java
│ │ ├── RibbonApplication.java
│ │ └── web
│ │ └── HelloController.java
└── pom.xml
├── springcloud-consumer-feign-hello
├── src
│ └── main
│ │ ├── resources
│ │ └── application.properties
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── loafer
│ │ └── demo
│ │ └── springcloud
│ │ └── feign
│ │ ├── entity
│ │ └── User.java
│ │ ├── Application.java
│ │ ├── service
│ │ └── HelloService.java
│ │ └── web
│ │ └── HelloController.java
└── pom.xml
├── eureka-registry
├── src
│ └── main
│ │ ├── resources
│ │ ├── application.yml
│ │ └── application.properties
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── loafer
│ │ └── demo
│ │ └── sc
│ │ └── registry
│ │ └── Application.java
└── pom.xml
└── pom.xml
/README.md:
--------------------------------------------------------------------------------
1 | # spring-cloud-tutorials
2 |
--------------------------------------------------------------------------------
/config-repo/springcloud-config-client-dev.properties:
--------------------------------------------------------------------------------
1 | info.foo=bar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | *.ipr
3 | *.iws
4 | .idea/
5 | target/
6 | pom.xml.tag
7 | pom.xml.releaseBackup
8 | pom.xml.versionsBackup
9 | pom.xml.next
10 | release.properties
11 |
--------------------------------------------------------------------------------
/hello-service/src/main/java/com/github/loafer/demo/sc/provider/entity/NestedBean.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.provider.entity;
2 |
3 | /**
4 | * Created by zhaojh.
5 | */
6 | public class NestedBean {
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/entity/NestedBean.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.entity;
2 |
3 | /**
4 | * Created by zhaojh.
5 | */
6 | public class NestedBean {
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/hello-service/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=9091
2 | spring.application.name=hello-service
3 |
4 | #eureka.instance.lease-renewal-interval-in-seconds=2
5 | #eureka.instance.lease-expiration-duration-in-seconds=6
6 |
7 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka
--------------------------------------------------------------------------------
/ribbon-consumer/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8081
2 | spring.application.name=ribbon-consumer
3 |
4 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
5 |
6 | eureka.instance.lease-renewal-interval-in-seconds=2
7 | eureka.instance.lease-expiration-duration-in-seconds=6
8 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=hello-consumer-feign
2 | server.port=8082
3 |
4 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
5 |
6 | eureka.instance.lease-renewal-interval-in-seconds=2
7 | eureka.instance.lease-expiration-duration-in-seconds=6
8 |
9 | debug=true
--------------------------------------------------------------------------------
/eureka-registry/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8761
3 | eureka:
4 | instance:
5 | hostname: localhost
6 | client:
7 | fetch-registry: false
8 | register-with-eureka: false
9 | service-url:
10 | - defaultZone=http://localhost:8761/eureka
11 | server:
12 | eviction-interval-timer-in-ms: 5000
13 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=8082
2 | spring.application.name=feign-consumer
3 |
4 | eureka.instance.lease-renewal-interval-in-seconds=2
5 | eureka.instance.lease-expiration-duration-in-seconds=10
6 |
7 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
8 |
9 | logging.level.com.github.loafer.demo.sc.consumer.service.ConvertService=debug
--------------------------------------------------------------------------------
/eureka-registry/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #server.port=8761
2 | #
3 | #eureka.client.register-with-eureka=false
4 | #eureka.client.fetch-registry=false
5 | #eureka.client.service-url.defaultZone=http://localhost:8761/eureka
6 | #
7 | ##eureka.server.enable-self-preservation=false
8 | ##5\u79D2\u5237\u65B0\u4E00\u6B21\u670D\u52A1\u5217\u8868
9 | #eureka.server.eviction-interval-timer-in-ms=5000
--------------------------------------------------------------------------------
/eureka-registry/src/main/java/com/github/loafer/demo/sc/registry/Application.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.registry;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6 |
7 | /**
8 | * @author zhaojh.
9 | */
10 | @SpringBootApplication
11 | @EnableEurekaServer
12 | public class Application {
13 | public static void main(String[] args){
14 | SpringApplication.run(Application.class, args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/hello-service/src/main/java/com/github/loafer/demo/sc/provider/Application.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.provider;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 |
7 |
8 | /**
9 | * @author zhaojh.
10 | */
11 | @SpringBootApplication
12 | @EnableEurekaClient
13 | public class Application {
14 | public static void main(String[] args){
15 | SpringApplication.run(Application.class, args);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/src/main/java/com/github/loafer/demo/springcloud/feign/entity/User.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.springcloud.feign.entity;
2 |
3 | /**
4 | * @author zhaojh.
5 | */
6 | public class User {
7 | private String name;
8 |
9 | public String getName() {
10 | return name;
11 | }
12 |
13 | public void setName(String name) {
14 | this.name = name;
15 | }
16 |
17 | @Override
18 | public String toString() {
19 | return "User{" +
20 | "name='" + name + '\'' +
21 | '}';
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/ribbon-consumer/src/main/java/com/github/loafer/demo/sc/consumer/service/HelloService.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.service;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Component;
5 | import org.springframework.web.client.RestTemplate;
6 |
7 | /**
8 | * Created by zhaojh.
9 | */
10 | @Component
11 | public class HelloService {
12 | @Autowired
13 | private RestTemplate restTemplate;
14 |
15 | public String hello(){
16 | return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/service/HelloService.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.service;
2 |
3 | import org.springframework.cloud.netflix.feign.FeignClient;
4 | import org.springframework.web.bind.annotation.PathVariable;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 |
7 | /**
8 | * @author zhaojh.
9 | */
10 | @FeignClient(name = "hello-service")
11 | public interface HelloService {
12 | @RequestMapping(path = "hello")
13 | String hello();
14 |
15 | @RequestMapping(path = "/hello/{name}")
16 | String hello(@PathVariable("name") String name);
17 | }
18 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/src/main/java/com/github/loafer/demo/springcloud/feign/Application.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.springcloud.feign;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6 | import org.springframework.cloud.netflix.feign.EnableFeignClients;
7 |
8 | /**
9 | * @author zhaojh.
10 | */
11 | @SpringBootApplication
12 | @EnableEurekaClient
13 | @EnableFeignClients
14 | public class Application {
15 | public static void main(String[] args){
16 | SpringApplication.run(Application.class, args);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/formatter/FeignFormatterRegistrarImpl.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.formatter;
2 |
3 | import org.springframework.cloud.netflix.feign.FeignFormatterRegistrar;
4 | import org.springframework.format.FormatterRegistry;
5 | import org.springframework.format.datetime.DateFormatter;
6 | import org.springframework.stereotype.Component;
7 |
8 | /**
9 | * Created by zhaojh.
10 | */
11 | @Component
12 | public class FeignFormatterRegistrarImpl implements FeignFormatterRegistrar {
13 | @Override
14 | public void registerFormatters(FormatterRegistry registry) {
15 | registry.addFormatter(new DateFormatter());
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/eureka-registry/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-cloud-tutorials
7 | com.github.loafer
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | eureka-registry
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-eureka-server
18 |
19 |
20 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/FeignApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer;
2 |
3 | import feign.Logger;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 | import org.springframework.cloud.netflix.feign.EnableFeignClients;
8 | import org.springframework.context.annotation.Bean;
9 |
10 | /**
11 | * @author zhaojh.
12 | */
13 | @SpringBootApplication
14 | @EnableDiscoveryClient
15 | @EnableFeignClients
16 | public class FeignApplication {
17 | @Bean
18 | public Logger.Level feignLoggerLevel(){
19 | return Logger.Level.FULL;
20 | }
21 |
22 | public static void main(String[] args){
23 | SpringApplication.run(FeignApplication.class, args);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/web/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.web;
2 |
3 | import com.github.loafer.demo.sc.consumer.service.HelloService;
4 | import org.springframework.web.bind.annotation.GetMapping;
5 | import org.springframework.web.bind.annotation.PathVariable;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | import javax.annotation.Resource;
10 |
11 | /**
12 | * @author zhaojh.
13 | */
14 | @RestController
15 | @RequestMapping("feign/hello")
16 | public class HelloController {
17 | @Resource
18 | private HelloService helloService;
19 |
20 | @GetMapping
21 | public String hello(){
22 | return helloService.hello();
23 | }
24 |
25 | @GetMapping("/{name}")
26 | public String hello(@PathVariable("name") String name){
27 | return helloService.hello(name);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/formatter/DateJsonSerializer.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.formatter;
2 |
3 |
4 | import com.fasterxml.jackson.core.JsonGenerator;
5 | import com.fasterxml.jackson.core.JsonProcessingException;
6 | import com.fasterxml.jackson.databind.JsonSerializer;
7 | import com.fasterxml.jackson.databind.SerializerProvider;
8 |
9 |
10 | import java.io.IOException;
11 | import java.text.SimpleDateFormat;
12 | import java.util.Date;
13 |
14 | /**
15 | * Created by zhaojh.
16 | */
17 | public class DateJsonSerializer extends JsonSerializer {
18 | @Override
19 | public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
20 | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
21 | String formattedDate = formatter.format(date);
22 | jsonGenerator.writeString(formattedDate);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/feign-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-cloud-tutorials
7 | com.github.loafer
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | feign-consumer
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-eureka
18 |
19 |
20 |
21 | org.springframework.cloud
22 | spring-cloud-starter-feign
23 |
24 |
25 |
--------------------------------------------------------------------------------
/hello-service/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-cloud-tutorials
7 | com.github.loafer
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | hello-service
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-eureka
18 |
19 |
20 |
24 |
25 |
--------------------------------------------------------------------------------
/ribbon-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-cloud-tutorials
7 | com.github.loafer
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | ribbon-consumer
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-eureka
18 |
19 |
20 |
21 | org.springframework.cloud
22 | spring-cloud-starter-ribbon
23 |
24 |
25 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-cloud-tutorials
7 | com.github.loafer
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | springcloud-consumer-feign-hello
13 |
14 |
15 |
16 | org.springframework.cloud
17 | spring-cloud-starter-eureka
18 |
19 |
20 |
21 | org.springframework.cloud
22 | spring-cloud-starter-feign
23 |
24 |
25 |
--------------------------------------------------------------------------------
/hello-service/src/main/java/com/github/loafer/demo/sc/provider/web/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.provider.web;
2 |
3 | import org.springframework.cloud.client.ServiceInstance;
4 | import org.springframework.cloud.client.discovery.DiscoveryClient;
5 | import org.springframework.web.bind.annotation.GetMapping;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | import javax.annotation.Resource;
11 |
12 | /**
13 | * @author zhaojh.
14 | */
15 | @RestController
16 | @RequestMapping("/hello")
17 | public class HelloController {
18 | @Resource
19 | private DiscoveryClient client;
20 |
21 | @GetMapping
22 | public String hello(){
23 | ServiceInstance instance = client.getLocalServiceInstance();
24 | return String.format("Hello, %s:%s:%s", instance.getHost(),instance.getServiceId(),instance.getPort());
25 | }
26 |
27 | @GetMapping("/{name}")
28 | public String hello(@PathVariable("name") String name){
29 | return String.format("Hello, %s!", name);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/service/ConvertService.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.service;
2 |
3 | import com.github.loafer.demo.sc.consumer.entity.JavaBean;
4 | import org.springframework.cloud.netflix.feign.FeignClient;
5 | import org.springframework.format.annotation.DateTimeFormat;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestParam;
9 |
10 | import java.util.Collection;
11 | import java.util.Date;
12 |
13 | /**
14 | * Created by zhaojh.
15 | */
16 | @FeignClient(name = "hello-service")
17 | public interface ConvertService {
18 | @RequestMapping(path = "/convert/primitive")
19 | String primitive(@RequestParam("value") Integer value);
20 |
21 | @RequestMapping("/convert/date/{value}")
22 | String date(@PathVariable("value") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date value);
23 |
24 | @RequestMapping("/convert/collection")
25 | String collection(@RequestParam("values") Collection values);
26 |
27 | @RequestMapping("/convert/formattedCollection")
28 | String formattedCollection(@RequestParam("values") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Collection values);
29 |
30 | @RequestMapping("/convert/bean")
31 | String bean(JavaBean bean);
32 | }
33 |
--------------------------------------------------------------------------------
/ribbon-consumer/src/main/java/com/github/loafer/demo/sc/consumer/RibbonApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.loadbalancer.LoadBalanced;
6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.http.client.ClientHttpRequestFactory;
9 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
10 | import org.springframework.http.client.SimpleClientHttpRequestFactory;
11 | import org.springframework.web.client.RestTemplate;
12 |
13 |
14 | /**
15 | * @author zhaojh.
16 | */
17 | @SpringBootApplication
18 | @EnableEurekaClient
19 | public class RibbonApplication {
20 |
21 | @Bean
22 | @LoadBalanced
23 | public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory){
24 | return new RestTemplate(requestFactory);
25 | }
26 |
27 | @Bean
28 | public ClientHttpRequestFactory requestFactory(){
29 | HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
30 | return requestFactory;
31 | }
32 |
33 |
34 | public static void main(String[] args){
35 | SpringApplication.run(RibbonApplication.class, args);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/src/main/java/com/github/loafer/demo/springcloud/feign/service/HelloService.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.springcloud.feign.service;
2 |
3 | import com.github.loafer.demo.springcloud.feign.entity.User;
4 | import org.springframework.cloud.netflix.feign.FeignClient;
5 | import org.springframework.http.MediaType;
6 | import org.springframework.web.bind.annotation.*;
7 |
8 | import java.util.Map;
9 |
10 | /**
11 | * @author zhaojh.
12 | */
13 | @FeignClient(name = "hello-service")
14 | public interface HelloService {
15 |
16 | /**
17 | * 使用GET方式调用服务时,必须在参数列表中使用@RequestParam注解指定传递的参数名
18 | *
19 | * @param name
20 | * @return
21 | */
22 | @RequestMapping(path = "api/hello/en")
23 | String english(@RequestParam("name") String name);
24 |
25 |
26 | /**
27 | * 使用GET方式调用服务传递pojo时,使用Map传递参数
28 | *
29 | * @param params
30 | * @return
31 | */
32 | @RequestMapping(path = "api/hello/en/bean")
33 | String english(@RequestParam Map params);
34 |
35 | /**
36 | * 1、参数列表中没有使用@RequestParam注解的参数都被放入到request body中,同时Http method为POST
37 | * 2、服务提供方的参数列表应使用@RequestBody注解指定对应参数
38 | *
39 | * @param name
40 | * @return
41 | */
42 | @RequestMapping(path = "api/hello/cn")
43 | String chinese(String name);
44 |
45 | /**
46 | * 当使用POST方式传递pojo时,服务提供方的参数列表应使用@RequestBody注解
47 | *
48 | * @param user
49 | * @return
50 | */
51 | @RequestMapping(path = "api/hello/cn/bean")
52 | String chinese(User user);
53 | }
54 |
--------------------------------------------------------------------------------
/ribbon-consumer/src/main/java/com/github/loafer/demo/sc/consumer/web/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.web;
2 |
3 | import com.github.loafer.demo.sc.consumer.service.HelloService;
4 | import org.springframework.cloud.client.ServiceInstance;
5 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RestController;
10 | import org.springframework.web.client.RestTemplate;
11 |
12 | import javax.annotation.Resource;
13 |
14 | /**
15 | * @author zhaojh.
16 | */
17 | @RestController
18 | @RequestMapping("/ribbon/hello")
19 | public class HelloController {
20 | @Resource
21 | private HelloService helloService;
22 | @Resource
23 | private LoadBalancerClient client;
24 |
25 | private RestTemplate template;
26 |
27 | public HelloController() {
28 | this.template = new RestTemplate();
29 | }
30 |
31 | @RequestMapping(method = RequestMethod.GET)
32 | public String hello(){
33 | return helloService.hello();
34 | }
35 |
36 | @RequestMapping(path = "client", method = RequestMethod.GET)
37 | public String handleWithLoadBalancerClient(){
38 | return template.getForEntity(serviceUrl() + "/hello", String.class).getBody();
39 | }
40 |
41 | private String serviceUrl(){
42 | ServiceInstance instance = client.choose("hello-service");
43 | return String.format("http://%s:%s", instance.getHost(), instance.getPort());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/hello-service/src/main/java/com/github/loafer/demo/sc/provider/web/ConvertController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.provider.web;
2 |
3 | import com.github.loafer.demo.sc.provider.entity.JavaBean;
4 | import org.springframework.format.annotation.DateTimeFormat;
5 | import org.springframework.web.bind.annotation.GetMapping;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestBody;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import java.util.Collection;
13 | import java.util.Date;
14 |
15 | /**
16 | * Created by zhaojh.
17 | */
18 | @RestController
19 | @RequestMapping("/convert")
20 | public class ConvertController {
21 | @GetMapping("/primitive")
22 | public String primitive(Integer value){
23 | return "Converted primitive " + value;
24 | }
25 |
26 | @GetMapping("/date/{value}")
27 | public String date(@PathVariable("value") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date){
28 | return "Converted date " + date;
29 | }
30 |
31 | @GetMapping("collection")
32 | public String collection(@RequestParam Collection values){
33 | return "Converted collection " + values;
34 | }
35 |
36 | @GetMapping("formattedCollection")
37 | public String formattedCollection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Collection values){
38 | return "Converted formatted collection " + values;
39 | }
40 |
41 | @RequestMapping("bean")
42 | public String bean(@RequestBody JavaBean bean){
43 | return "Converted " + bean;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/springcloud-consumer-feign-hello/src/main/java/com/github/loafer/demo/springcloud/feign/web/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.springcloud.feign.web;
2 |
3 | import com.github.loafer.demo.springcloud.feign.entity.User;
4 | import com.github.loafer.demo.springcloud.feign.service.HelloService;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.cglib.beans.BeanMap;
8 | import org.springframework.http.MediaType;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RequestParam;
11 | import org.springframework.web.bind.annotation.RestController;
12 |
13 | import javax.annotation.Resource;
14 |
15 | /**
16 | * @author zhaojh.
17 | */
18 | @RestController
19 | @RequestMapping("hello")
20 | public class HelloController {
21 | private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
22 |
23 | @Resource
24 | private HelloService helloService;
25 |
26 | @RequestMapping(path = "en", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
27 | public String english(String name){
28 | return helloService.english(name);
29 | }
30 |
31 | @RequestMapping(path = "en/bean")
32 | public String english(User user){
33 | BeanMap beanMap = BeanMap.create(user);
34 | return helloService.english(beanMap);
35 | }
36 |
37 | @RequestMapping(path = "cn", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
38 | public String chinese(String name){
39 | return helloService.chinese(name);
40 | }
41 |
42 | @RequestMapping(path = "cn/bean", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
43 | public String chinese(User user){
44 | logger.info("user: {}", user);
45 | return helloService.chinese(user);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 | 4.0.0
7 |
8 | com.github.loafer
9 | spring-cloud-tutorials
10 | pom
11 | 1.0-SNAPSHOT
12 |
13 |
14 | springcloud-consumer-feign-hello
15 | eureka-registry
16 | hello-service
17 | ribbon-consumer
18 | feign-consumer
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-parent
25 | 1.5.2.RELEASE
26 |
27 |
28 |
29 | UTF-8
30 | 1.8
31 |
32 |
33 |
34 |
35 |
36 | org.springframework.cloud
37 | spring-cloud-dependencies
38 | Dalston.RELEASE
39 | pom
40 | import
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-maven-plugin
50 |
51 |
52 |
53 |
54 |
55 |
56 | spring-releases
57 | Spring Releases
58 | https://repo.spring.io/libs-release-local
59 |
60 | false
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/web/CustomerConvertController.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.web;
2 |
3 | import com.github.loafer.demo.sc.consumer.entity.JavaBean;
4 | import com.github.loafer.demo.sc.consumer.service.ConvertService;
5 | import com.github.loafer.demo.sc.consumer.service.HelloService;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.format.annotation.DateTimeFormat;
10 | import org.springframework.web.bind.annotation.GetMapping;
11 | import org.springframework.web.bind.annotation.PathVariable;
12 | import org.springframework.web.bind.annotation.RequestMapping;
13 | import org.springframework.web.bind.annotation.RequestParam;
14 | import org.springframework.web.bind.annotation.RestController;
15 |
16 | import java.util.Collection;
17 | import java.util.Date;
18 |
19 | /**
20 | * Created by zhaojh.
21 | */
22 | @RestController
23 | @RequestMapping("/feign/convert")
24 | public class CustomerConvertController {
25 | private static final Logger logger = LoggerFactory.getLogger(CustomerConvertController.class);
26 | @Autowired
27 | private ConvertService convertService;
28 |
29 |
30 | @GetMapping("/primitive")
31 | public String primitive(Integer value){
32 | logger.info("value: {}", value);
33 | return convertService.primitive(value);
34 | }
35 |
36 | @GetMapping("/date/{value}")
37 | public String date(@PathVariable("value") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date value){
38 | return convertService.date(value);
39 | }
40 |
41 | @GetMapping("collection")
42 | public String collection(@RequestParam Collection values){
43 | return convertService.collection(values);
44 | }
45 |
46 | @GetMapping("formattedCollection")
47 | public String formattedCollection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Collection values){
48 | logger.info("values: {}", values);
49 | return convertService.formattedCollection(values);
50 | }
51 |
52 | @GetMapping("bean")
53 | public String bean(JavaBean bean){
54 | return convertService.bean(bean);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/hello-service/src/main/java/com/github/loafer/demo/sc/provider/entity/JavaBean.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.provider.entity;
2 |
3 | import org.springframework.format.annotation.DateTimeFormat;
4 |
5 | import java.util.Date;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | /**
10 | * Created by zhaojh.
11 | */
12 | public class JavaBean {
13 | private Integer primitive;
14 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
15 | private Date date;
16 | private List list;
17 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
18 | private List formattedList;
19 | private Map map;
20 | private NestedBean nested;
21 |
22 | public Integer getPrimitive() {
23 | return primitive;
24 | }
25 |
26 | public void setPrimitive(Integer primitive) {
27 | this.primitive = primitive;
28 | }
29 |
30 | public Date getDate() {
31 | return date;
32 | }
33 |
34 | public void setDate(Date date) {
35 | this.date = date;
36 | }
37 |
38 | public List getList() {
39 | return list;
40 | }
41 |
42 | public void setList(List list) {
43 | this.list = list;
44 | }
45 |
46 | public List getFormattedList() {
47 | return formattedList;
48 | }
49 |
50 | public void setFormattedList(List formattedList) {
51 | this.formattedList = formattedList;
52 | }
53 |
54 | public Map getMap() {
55 | return map;
56 | }
57 |
58 | public void setMap(Map map) {
59 | this.map = map;
60 | }
61 |
62 | public NestedBean getNested() {
63 | return nested;
64 | }
65 |
66 | public void setNested(NestedBean nested) {
67 | this.nested = nested;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | StringBuilder builder = new StringBuilder("JavaBean ");
73 | if(primitive != null){
74 | builder.append(" primitive=").append(primitive);
75 | }
76 |
77 | if(date != null){
78 | builder.append(" date=").append(date);
79 | }
80 |
81 | if(list != null){
82 | builder.append(" list=").append(list);
83 | }
84 |
85 | if(formattedList != null){
86 | builder.append(" formattedList=").append(formattedList);
87 | }
88 |
89 | if(map != null){
90 | builder.append(" map=").append(map);
91 | }
92 | return builder.toString();
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/feign-consumer/src/main/java/com/github/loafer/demo/sc/consumer/entity/JavaBean.java:
--------------------------------------------------------------------------------
1 | package com.github.loafer.demo.sc.consumer.entity;
2 |
3 | import org.springframework.format.annotation.DateTimeFormat;
4 |
5 | import java.util.Date;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | /**
10 | * Created by zhaojh.
11 | */
12 | public class JavaBean {
13 | private Integer primitive;
14 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
15 | private Date date;
16 | private List list;
17 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
18 | private List formattedList;
19 | private Map map;
20 | private NestedBean nested;
21 |
22 | public Integer getPrimitive() {
23 | return primitive;
24 | }
25 |
26 | public void setPrimitive(Integer primitive) {
27 | this.primitive = primitive;
28 | }
29 |
30 | public Date getDate() {
31 | return date;
32 | }
33 |
34 | public void setDate(Date date) {
35 | this.date = date;
36 | }
37 |
38 | public List getList() {
39 | return list;
40 | }
41 |
42 | public void setList(List list) {
43 | this.list = list;
44 | }
45 |
46 | public List getFormattedList() {
47 | return formattedList;
48 | }
49 |
50 | public void setFormattedList(List formattedList) {
51 | this.formattedList = formattedList;
52 | }
53 |
54 | public Map getMap() {
55 | return map;
56 | }
57 |
58 | public void setMap(Map map) {
59 | this.map = map;
60 | }
61 |
62 | public NestedBean getNested() {
63 | return nested;
64 | }
65 |
66 | public void setNested(NestedBean nested) {
67 | this.nested = nested;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | StringBuilder builder = new StringBuilder("JavaBean ");
73 | if(primitive != null){
74 | builder.append(" primitive=").append(primitive);
75 | }
76 |
77 | if(date != null){
78 | builder.append(" date=").append(date);
79 | }
80 |
81 | if(list != null){
82 | builder.append(" list=").append(list);
83 | }
84 |
85 | if(formattedList != null){
86 | builder.append(" formattedList=").append(formattedList);
87 | }
88 |
89 | if(map != null){
90 | builder.append(" map=").append(map);
91 | }
92 | return builder.toString();
93 | }
94 | }
95 |
--------------------------------------------------------------------------------