File tree Expand file tree Collapse file tree 10 files changed +202
-1
lines changed
Service-Product-Consumer-Feign/src/main/resources
Service-Product-Consumer-Feign-Hystrix
Service-Product-Consumer-Ribbon/src/main/resources Expand file tree Collapse file tree 10 files changed +202
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3
+ 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 >springcloud-parent</artifactId >
7
+ <groupId >cn.mrdear.springcloud</groupId >
8
+ <version >1.0.0</version >
9
+ </parent >
10
+ <modelVersion >4.0.0</modelVersion >
11
+
12
+ <groupId >cn.mrdear.springcloud</groupId >
13
+ <artifactId >Service-Product-Consumer-Feign-Hystrix</artifactId >
14
+
15
+ <dependencies >
16
+ <dependency >
17
+ <groupId >org.springframework.cloud</groupId >
18
+ <artifactId >spring-cloud-starter-feign</artifactId >
19
+ </dependency >
20
+ <dependency >
21
+ <groupId >org.springframework.cloud</groupId >
22
+ <artifactId >spring-cloud-starter-eureka</artifactId >
23
+ </dependency >
24
+ </dependencies >
25
+ </project >
Original file line number Diff line number Diff line change
1
+ import org .springframework .boot .SpringApplication ;
2
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
3
+ import org .springframework .cloud .client .discovery .EnableDiscoveryClient ;
4
+ import org .springframework .cloud .netflix .feign .EnableFeignClients ;
5
+
6
+
7
+ /**
8
+ * @author Niu Li
9
+ * @since 2017/6/3
10
+ */
11
+ @ SpringBootApplication (scanBasePackages = "cn.medear.springcloud" )
12
+ @ EnableDiscoveryClient
13
+ @ EnableFeignClients (basePackages = "cn.medear.springcloud.restclient" )
14
+ public class UserConsumeFeignHystrixApplication {
15
+
16
+ public static void main (String [] args ) {
17
+ SpringApplication .run (UserConsumeFeignHystrixApplication .class , args );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package cn .medear .springcloud .controller ;
2
+
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+
6
+ import org .springframework .web .bind .annotation .GetMapping ;
7
+ import org .springframework .web .bind .annotation .PathVariable ;
8
+ import org .springframework .web .bind .annotation .RestController ;
9
+
10
+ import javax .annotation .Resource ;
11
+
12
+ import cn .medear .springcloud .entity .User ;
13
+ import cn .medear .springcloud .restclient .UserClient ;
14
+ import lombok .extern .slf4j .Slf4j ;
15
+
16
+ /**
17
+ * 用于消费UserService的类
18
+ * @author Niu Li
19
+ * @since 2017/6/3
20
+ */
21
+ @ RestController
22
+ @ Slf4j
23
+ public class ConsumeUserController {
24
+
25
+ @ Resource
26
+ private UserClient userClient ;
27
+
28
+ private static final ObjectMapper objectMapper = new ObjectMapper ();
29
+
30
+ @ GetMapping ("/{id}" )
31
+ public String findUserById (@ PathVariable Long id ) {
32
+ User user = userClient .findById (id );
33
+ String valueAsString = null ;
34
+ try {
35
+ valueAsString = objectMapper .writeValueAsString (user );
36
+ } catch (JsonProcessingException e ) {
37
+ log .error ("parse json error. {}" ,e );
38
+ }
39
+ return valueAsString ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package cn .medear .springcloud .entity ;
2
+
3
+ import java .io .Serializable ;
4
+
5
+ import lombok .Data ;
6
+ import lombok .NoArgsConstructor ;
7
+ import lombok .ToString ;
8
+
9
+ /**
10
+ * @author Niu Li
11
+ * @since 2017/6/3
12
+ */
13
+ @ Data
14
+ @ NoArgsConstructor
15
+ @ ToString
16
+ public class User implements Serializable {
17
+
18
+ private static final long serialVersionUID = -2885884654600114856L ;
19
+
20
+ private Long id ;
21
+
22
+ private String username ;
23
+
24
+ private String password ;
25
+ }
Original file line number Diff line number Diff line change
1
+ package cn .medear .springcloud .restclient ;
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
+ import org .springframework .web .bind .annotation .RequestMethod ;
7
+
8
+ import cn .medear .springcloud .entity .User ;
9
+ import cn .medear .springcloud .restclient .failback .UserClientFailBack ;
10
+
11
+ /**
12
+ * @author Niu Li
13
+ * @since 2017/6/4
14
+ */
15
+ @ FeignClient (name = "service-user-provider" ,fallbackFactory = UserClientFailBack .class )
16
+ public interface UserClient {
17
+
18
+ @ RequestMapping (value = "/{id}" ,method = RequestMethod .GET )
19
+ User findById (@ PathVariable ("id" ) Long id );
20
+
21
+ }
Original file line number Diff line number Diff line change
1
+ package cn .medear .springcloud .restclient .failback ;
2
+
3
+ import org .springframework .stereotype .Component ;
4
+
5
+ import cn .medear .springcloud .entity .User ;
6
+ import cn .medear .springcloud .restclient .UserClient ;
7
+ import feign .hystrix .FallbackFactory ;
8
+ import lombok .extern .slf4j .Slf4j ;
9
+
10
+ /**
11
+ * 用户调用断路器
12
+ * @author Niu Li
13
+ * @since 2017/6/4
14
+ */
15
+ @ Slf4j
16
+ @ Component
17
+ public class UserClientFailBack implements FallbackFactory <UserClient >{
18
+
19
+ private static UserClient userClient = new UserClientFailBackWithInfo ();
20
+
21
+ /**
22
+ * 错误返回内容配置
23
+ */
24
+ @ Override
25
+ public UserClient create (Throwable throwable ) {
26
+ log .error ("UserClient hystrix open,error is {}" ,throwable .getMessage ());
27
+ return userClient ;
28
+ }
29
+
30
+
31
+ private static class UserClientFailBackWithInfo implements UserClient {
32
+ @ Override
33
+ public User findById (Long id ) {
34
+ User user = new User ();
35
+ user .setId (0L );
36
+ user .setUsername ("失败用户" );
37
+ return user ;
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ server :
2
+ port : 8086
3
+
4
+ spring :
5
+ application :
6
+ name : service-product-consumer-fegin-hystrix
7
+
8
+ eureka :
9
+ client :
10
+ service-url :
11
+ default-zone : http://localhost:8761/eureka/
12
+ instance :
13
+ prefer-ip-address : true
14
+
15
+ # 开启断路器
16
+ feign :
17
+ hystrix :
18
+ enabled : true
19
+
20
+ # 负载均衡配置为随机,不配置默认轮询
21
+ service-user-provider :
22
+ ribbon :
23
+ NFLoadBalancerRuleClassName : com.netflix.loadbalancer.RandomRule
24
+
Original file line number Diff line number Diff line change 16
16
httpclient :
17
17
enabled : true
18
18
19
+ # 负载均衡配置为随机,不配置默认轮询
20
+ service-user-provider :
21
+ ribbon :
22
+ NFLoadBalancerRuleClassName : com.netflix.loadbalancer.RandomRule
23
+
Original file line number Diff line number Diff line change @@ -13,6 +13,6 @@ eureka:
13
13
prefer-ip-address : true
14
14
15
15
# 负载均衡配置为随机,不配置默认轮询
16
- user-resporitory :
16
+ service- user-provider :
17
17
ribbon :
18
18
NFLoadBalancerRuleClassName : com.netflix.loadbalancer.RandomRule
Original file line number Diff line number Diff line change 14
14
<module >Service-User-Provider</module >
15
15
<module >Service-Product-Consumer-Ribbon</module >
16
16
<module >Service-Product-Consumer-Feign</module >
17
+ <module >Service-Product-Consumer-Feign-Hystrix</module >
17
18
</modules >
18
19
19
20
<!-- Spring Boot依赖-->
You can’t perform that action at this time.
0 commit comments