Commit 7fff2afe authored by XCQi's avatar XCQi

远程服务调用现已支持负载均衡 默认情况下轮询方式负载

支持断路和降级 不可用的实例将暂时被踢出可用列表 全部实例不可用的时候将降级响应预设内容
parent 0793034b
Pipeline #47 failed with stages
package com.yingxin.springcloudconsumer.controller; package com.yingxin.springcloudconsumer.controller;
import com.yingxin.springcloudconsumer.entity.User;
import com.yingxin.springcloudconsumer.service.HelloService; import com.yingxin.springcloudconsumer.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
public class RestApi { public class RestApi {
...@@ -17,4 +20,14 @@ public class RestApi { ...@@ -17,4 +20,14 @@ public class RestApi {
public String helloConsumer() { public String helloConsumer() {
return helloService.hello(); return helloService.hello();
} }
@GetMapping("user")
public User userConsumer() {
return helloService.getUser();
}
@GetMapping("users")
public List<User> usersConsumer() {
return helloService.getUsers();
}
} }
package com.yingxin.springcloudconsumer.entity;
public class User {
private String id;
private String name;//姓名
private Integer age;//年龄
public User() {
}
public User(String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.yingxin.springcloudconsumer.service;
import com.yingxin.springcloudconsumer.entity.User;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class HelloFallBack implements HelloService {
@Override
public String hello() {
return "service error";
}
@Override
public User getUser() {
return new User();
}
@Override
public List<User> getUsers() {
return null;
}
}
package com.yingxin.springcloudconsumer.service; package com.yingxin.springcloudconsumer.service;
import com.yingxin.springcloudconsumer.entity.User;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("spring-cloud-server-HelloWorld") import java.util.List;
@FeignClient(name = "spring-cloud-server-HelloWorld", fallback = HelloFallBack.class)
public interface HelloService { public interface HelloService {
@GetMapping("hello") @GetMapping("hello")
String hello(); String hello();
@GetMapping("exampleforget")
User getUser();
@GetMapping("queryUserList")
List<User> getUsers();
} }
server.port=9000 server.port=9000
spring.application.name=consumer spring.application.name=consumer
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
\ No newline at end of file feign.hystrix.enabled=true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment