Skip to content

Commit fc2047a

Browse files
committed
微服务feign调用Demo
1 parent a41e331 commit fc2047a

File tree

14 files changed

+450
-16
lines changed

14 files changed

+450
-16
lines changed

hello-service-api/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<artifactId>hello-service-api</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<groupId>com.microservice.hello</groupId>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-web</artifactId>
15+
<version>4.2.9.RELEASE</version>
16+
</dependency>
17+
</dependencies>
18+
19+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.microservice.remote.api;
2+
3+
import com.microservice.remote.model.User;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
7+
/**
8+
* @description TODO
9+
*
10+
* @author Cheese
11+
* @date 2018/4/26
12+
* @param
13+
* @return
14+
*/
15+
@RequestMapping("/hello-service-remote")
16+
public interface HelloServiceRemoteApi {
17+
18+
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
19+
String hello(@RequestParam("name") String name);
20+
21+
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
22+
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
23+
24+
25+
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
26+
String hello(@RequestBody User user);
27+
28+
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.microservice.remote.model;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @description TODO
7+
*
8+
* @author Cheese
9+
* @date 2018/4/26
10+
* @param
11+
* @return
12+
*/
13+
public class User implements Serializable {
14+
private static final long serialVersionUID = -7233238826463139634L;
15+
16+
private Long id;
17+
18+
private String name;
19+
20+
private Integer age;
21+
22+
public User() {
23+
}
24+
25+
public User(String name, Integer age) {
26+
this.name = name;
27+
this.age = age;
28+
}
29+
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public Integer getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(Integer age) {
51+
this.age = age;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "User{" +
57+
"name='" + name + '\'' +
58+
", age=" + age +
59+
'}';
60+
}
61+
}

hello-service-consumer/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>micro-service</artifactId>
7+
<groupId>spring.cloud</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>hello-service-consumer</artifactId>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.microservice.hello</groupId>
21+
<artifactId>hello-service-api</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.cloud</groupId>
32+
<artifactId>spring-cloud-starter-eureka</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-ribbon</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-feign</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-actuator</artifactId>
48+
</dependency>
49+
50+
</dependencies>
51+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.microservice;
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+
* @description TODO
12+
*
13+
* @author Cheese
14+
* @date 2018/4/26
15+
* @param
16+
* @return
17+
*/
18+
@EnableFeignClients
19+
@EnableDiscoveryClient
20+
@SpringBootApplication
21+
public class HelloConsumerApp {
22+
23+
@Bean
24+
Logger.Level feginLoggerLevel(){
25+
return Logger.Level.FULL;
26+
}
27+
28+
public static void main(String[] args) {
29+
SpringApplication.run(HelloConsumerApp.class, args);
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.microservice.controller;
2+
3+
import com.microservice.remote.model.User;
4+
import com.microservice.service.HelloBackgroundService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.io.UnsupportedEncodingException;
10+
import java.net.URLEncoder;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* @description TODO
16+
*
17+
* @author Cheese
18+
* @date 2018/4/26
19+
* @param
20+
* @return
21+
*/
22+
@RestController
23+
public class HelloController {
24+
25+
@Autowired
26+
private HelloBackgroundService helloBackgroundService;
27+
28+
@RequestMapping(value = "/hello")
29+
public Map<String,Object> hello(){
30+
Map<String,Object> ret = new HashMap<String, Object>();
31+
StringBuffer sb = new StringBuffer();
32+
String s1 = helloBackgroundService.hello("张三");
33+
sb.append(s1).append("\n");
34+
User user = null;
35+
try {
36+
user = helloBackgroundService.hello(URLEncoder.encode("李四", "UTF-8"), 30);
37+
} catch (UnsupportedEncodingException e) {
38+
e.printStackTrace();
39+
}
40+
sb.append(user.toString()).append("\n");
41+
String s3 = helloBackgroundService.hello(new User("王五", 19));
42+
sb.append(s3).append("\n");
43+
ret.put("show",sb.toString());
44+
return ret;
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.microservice.service;
2+
3+
import com.microservice.remote.api.HelloServiceRemoteApi;
4+
import org.springframework.cloud.netflix.feign.FeignClient;
5+
6+
/**
7+
* @description TODO
8+
*
9+
* @author Cheese
10+
* @date 2018/4/26
11+
* @param
12+
* @return
13+
*/
14+
@FeignClient(value = "hello-service-provider")
15+
public interface HelloBackgroundService extends HelloServiceRemoteApi{
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
server.port=8887
2+
3+
spring.application.name=hello-service-consumer
4+
5+
6+
eureka.instance.hostname=register.center.com
7+
8+
eureka.instance.server.port=8881
9+
10+
11+
#默认的注册域
12+
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${eureka.instance.server.port}/eureka/
13+
14+
#开启请求压缩功能
15+
feign.compression.request.enabled=true
16+
17+
#开启响应压缩功能
18+
feign.compression.response.enabled=true
19+
20+
#指定压缩请求数据类型
21+
feign.compression.request.mime-types=text/xml;application/xml;application/json
22+
23+
#如果传输超过该字节,就对其进行压缩
24+
feign.compression.request.min-request-size=2048
25+
26+
#控制台彩色输
27+
spring.output.ansi.enabled=ALWAYS
28+
29+
30+
#日志配置,该接口的日志级别
31+
#logging.level.com.qee.service.HelloBackgroundService=DEBUG

hello-service-provider/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>micro-service</artifactId>
7+
<groupId>spring.cloud</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>hello-service-provider</artifactId>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.microservice.hello</groupId>
22+
<artifactId>hello-service-api</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter-eureka</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-ribbon</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.cloud</groupId>
43+
<artifactId>spring-cloud-starter-hystrix</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.cloud</groupId>
48+
<artifactId>spring-cloud-starter-sleuth</artifactId>
49+
</dependency>
50+
51+
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-actuator</artifactId>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-amqp</artifactId>
65+
</dependency>
66+
67+
</dependencies>
68+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.microservice.remote;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
8+
/**
9+
* @description TODO
10+
*
11+
* @author Cheese
12+
* @date 2018/4/26
13+
* @param
14+
* @return
15+
*/
16+
@EnableCircuitBreaker
17+
@EnableDiscoveryClient
18+
@SpringBootApplication
19+
public class HelloProviderApplication {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(HelloProviderApplication.class, args);
23+
}
24+
}

0 commit comments

Comments
 (0)