-断路器-Hystrix对Feign的支持
This commit is contained in:
9
spring-cloud-17/.gitignore
vendored
Normal file
9
spring-cloud-17/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
*/target/
|
||||
*.project
|
||||
*.classpath
|
||||
*.project
|
||||
*.settings
|
||||
*.mvn
|
||||
*.project
|
||||
*.springBeans
|
||||
*.bin
|
||||
86
spring-cloud-17/pom.xml
Normal file
86
spring-cloud-17/pom.xml
Normal file
@@ -0,0 +1,86 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>spring-cloud-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<modules>
|
||||
<module>spring-cloud-bean-17</module>
|
||||
<module>spring-cloud-api-17</module>
|
||||
<module>spring-cloud-consumer-17</module>
|
||||
<module>spring-cloud-provider-17</module>
|
||||
<module>spring-cloud-service-17</module>
|
||||
</modules>
|
||||
|
||||
<!-- <parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
<relativePath />
|
||||
</parent> -->
|
||||
|
||||
<!-- http://platform.spring.io/platform/ -->
|
||||
<parent>
|
||||
<groupId>io.spring.platform</groupId>
|
||||
<artifactId>platform-bom</artifactId>
|
||||
<version>Brussels-SR1</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-bean-17</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-api-17</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- spring cloud -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Camden.SR6</version>
|
||||
<!-- <version>Dalston.BUILD-SNAPSHOT</version> -->
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
29
spring-cloud-17/spring-cloud-api-17/pom.xml
Normal file
29
spring-cloud-17/spring-cloud-api-17/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-api-17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-api-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-bean-17</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.roncoo.education.feign;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@FeignClient(value = "roncoo", url = "www.roncoo.com")
|
||||
public interface IRoncooBiz {
|
||||
|
||||
@RequestMapping(value = "/{url}", method = RequestMethod.GET)
|
||||
String get(@PathVariable(name = "url") String url);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.roncoo.education.feign;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
|
||||
import com.roncoo.education.feign.configuration.RcFeignConfiguration;
|
||||
import com.roncoo.education.feign.fallback.RcFeignFallback;
|
||||
import com.roncoo.education.service.UserService;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@FeignClient(value = "spring-cloud-provider", configuration = RcFeignConfiguration.class, fallback = RcFeignFallback.class)
|
||||
public interface IUserBiz extends UserService{
|
||||
|
||||
// @RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
|
||||
// String view1(@PathVariable(name = "id") int id);
|
||||
//说明:method需要指定
|
||||
|
||||
//@RequestLine("GET /api/user/{id}")
|
||||
//String view1(@Param(value = "id") int id);
|
||||
|
||||
//@GetMapping(value = "/api/user/{id}")
|
||||
//String view1(@PathVariable(name = "id") int id);
|
||||
|
||||
|
||||
// 不支持该方式传值,启动出错。
|
||||
//@RequestMapping(value = "/api/user/get/{id}", method = RequestMethod.GET)
|
||||
//User get3(@PathVariable(value = "id") int id, @RequestParam String name);
|
||||
|
||||
/*@RequestMapping(value = "/api/user/get/{id}", method = RequestMethod.GET)
|
||||
User get4(@PathVariable(value = "id") int id, @RequestParam Map<String, Object> name);
|
||||
|
||||
@RequestMapping(value = "/api/user/get/{id}", method = RequestMethod.GET)
|
||||
User get5(@PathVariable(value = "id") int id, @RequestParam(value = "name") String name);
|
||||
|
||||
@RequestMapping(value = "/api/user/get/{id}", method = RequestMethod.GET)
|
||||
User get1(@PathVariable(value = "id") int id, String name);
|
||||
|
||||
@RequestMapping(value = "/api/user/get/{id}", method = RequestMethod.GET)
|
||||
User get2(@PathVariable(value = "id") int id, Map<String, Object> name);
|
||||
|
||||
@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
|
||||
User view(@PathVariable(value = "id") int id);
|
||||
|
||||
//@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
|
||||
//String view1(@PathVariable(value = "id") int id);
|
||||
|
||||
@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
|
||||
Map<String, Object> view2(@PathVariable(value = "id") int id);*/
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.roncoo.education.feign.configuration;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommand.Setter;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandKey;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Logger;
|
||||
import feign.Target;
|
||||
import feign.hystrix.HystrixFeign;
|
||||
import feign.hystrix.SetterFactory;
|
||||
|
||||
//@Configuration
|
||||
public class RcFeignConfiguration {
|
||||
|
||||
@Bean
|
||||
public Logger.Level feignLoggerLevel() {
|
||||
return feign.Logger.Level.FULL;
|
||||
}
|
||||
|
||||
/*@Bean
|
||||
public Contract feignContract() {
|
||||
return new feign.Contract.Default();
|
||||
}*/
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
public Feign.Builder feignBuilder() {
|
||||
class RcSetterFactory implements SetterFactory {
|
||||
@Override
|
||||
public Setter create(Target<?> target, Method method) {
|
||||
String groupKey = target.name();
|
||||
String commandKey = method.getName();
|
||||
return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
|
||||
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
|
||||
}
|
||||
}
|
||||
return HystrixFeign.builder().setterFactory(new RcSetterFactory());
|
||||
// return Feign.builder();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.roncoo.education.feign.fallback;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.roncoo.education.bean.User;
|
||||
import com.roncoo.education.feign.IUserBiz;
|
||||
|
||||
@Component
|
||||
public class RcFeignFallback implements IUserBiz {
|
||||
@Override
|
||||
public User find(int id) {
|
||||
System.out.println("断路器");
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName("断路器");
|
||||
user.setCreateTime(new Date());
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
25
spring-cloud-17/spring-cloud-bean-17/pom.xml
Normal file
25
spring-cloud-17/spring-cloud-bean-17/pom.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-bean-17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-bean-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.roncoo.education.bean;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 实体类
|
||||
*
|
||||
* @author wujing
|
||||
*/
|
||||
public class User {
|
||||
private int id;
|
||||
private String name;
|
||||
private Date createTime;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.roncoo.education.service;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.roncoo.education.bean.User;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/user/find/{id}")
|
||||
User find(@PathVariable(value = "id") int id);
|
||||
}
|
||||
66
spring-cloud-17/spring-cloud-consumer-17/pom.xml
Normal file
66
spring-cloud-17/spring-cloud-consumer-17/pom.xml
Normal file
@@ -0,0 +1,66 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-consumer-17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-consumer-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-api-17</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@EnableCircuitBreaker
|
||||
@EnableFeignClients
|
||||
@EnableEurekaClient
|
||||
@SpringBootApplication
|
||||
public class ConsumerApplication {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
protected RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//package com.roncoo.education.configuration;
|
||||
//import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//
|
||||
//@Configuration
|
||||
//@RibbonClient(name = "spring-cloud-provider2", configuration = RoundRobinRuleConfig.class)
|
||||
//public class Provider2Configuration {
|
||||
//
|
||||
//}
|
||||
@@ -0,0 +1,10 @@
|
||||
//package com.roncoo.education.configuration;
|
||||
//
|
||||
//import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//
|
||||
//@Configuration
|
||||
//@RibbonClient(name = "spring-cloud-provider", configuration = RandomRuleConfig.class)
|
||||
//public class ProviderConfiguration {
|
||||
//
|
||||
//}
|
||||
@@ -0,0 +1,18 @@
|
||||
//package com.roncoo.education.configuration;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import com.netflix.loadbalancer.IRule;
|
||||
//import com.netflix.loadbalancer.RandomRule;
|
||||
//
|
||||
///**
|
||||
// * 随机算法
|
||||
// *
|
||||
// * @author wujing
|
||||
// */
|
||||
////@Configuration
|
||||
//public class RandomRuleConfig {
|
||||
// @Bean
|
||||
// public IRule randomRule() {
|
||||
// return new RandomRule();
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.roncoo.education.configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
/**
|
||||
* 自定义Ribbon的负载均衡策略
|
||||
*
|
||||
*/
|
||||
public class RoncooCustomRule extends AbstractLoadBalancerRule {
|
||||
|
||||
private Server choose(ILoadBalancer lb, Object key) {
|
||||
if (lb == null) {
|
||||
return null;
|
||||
}
|
||||
Server server = null;
|
||||
|
||||
while (server == null) {
|
||||
if (Thread.interrupted()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Server> upList = lb.getReachableServers(); // 可用的服务实例
|
||||
|
||||
// 只获取端口为:7779的服务实例
|
||||
for (Server s : upList) {
|
||||
if (s.getPort() == 7779) {
|
||||
server = s;
|
||||
}
|
||||
}
|
||||
|
||||
if (server == null) {
|
||||
Thread.yield();
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("实例IP:" + server.getHost() + " 端口:" + server.getPort());
|
||||
|
||||
if (server.isAlive()) {
|
||||
return (server);
|
||||
}
|
||||
|
||||
server = null;
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server choose(Object key) {
|
||||
return choose(getLoadBalancer(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//package com.roncoo.education.configuration;
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import com.netflix.loadbalancer.IRule;
|
||||
//import com.netflix.loadbalancer.RandomRule;
|
||||
//import com.netflix.loadbalancer.RoundRobinRule;
|
||||
///**
|
||||
// * 轮询算法
|
||||
// *
|
||||
// * @author wujing
|
||||
// */
|
||||
//// @Configuration
|
||||
//public class RoundRobinRuleConfig {
|
||||
// @Bean
|
||||
// public IRule roundRobinRule() {
|
||||
// return new RandomRule();
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.roncoo.education.controller;
|
||||
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 com.roncoo.education.bean.User;
|
||||
import com.roncoo.education.feign.IRoncooBiz;
|
||||
import com.roncoo.education.feign.IUserBiz;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/feign/user", method = RequestMethod.POST)
|
||||
public class FeignUserController {
|
||||
|
||||
@Autowired
|
||||
private IUserBiz userBiz;
|
||||
|
||||
@Autowired
|
||||
private IRoncooBiz roncooBiz;
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public User get(@PathVariable(value = "id") int id) {
|
||||
//System.out.println(userBiz.view(id));
|
||||
//System.out.println(userBiz.view1(id));
|
||||
//System.out.println(userBiz.view2(id));
|
||||
|
||||
//String name = "测试1";
|
||||
//Map<String, Object> map = new HashMap<>();
|
||||
//map.put("name", "测试2");
|
||||
//userBiz.get4(id, map);
|
||||
//userBiz.get5(id, name);
|
||||
//System.out.println(roncooBiz.get("course/list.html"));
|
||||
//System.out.println(userBiz.find(id));
|
||||
User user = userBiz.find(id);
|
||||
System.out.println(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
|
||||
package com.roncoo.education.controller;
|
||||
|
||||
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 com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/user", method = RequestMethod.POST)
|
||||
public class UserController {
|
||||
|
||||
// private static final String URL="http://localhost:7777/api/user/{id}";
|
||||
private static final String URL = "http://spring-cloud-provider/api/user/{id}";
|
||||
private static final String URL2 = "http://spring-cloud-provider2/api/user/{id}";
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
// @HystrixCommand(fallbackMethod = "getFallback", commandProperties = @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"))
|
||||
@HystrixCommand(fallbackMethod = "getFallback")
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public String get(@PathVariable(value = "id") int id) {
|
||||
return restTemplate.getForObject(URL, String.class, id);
|
||||
}
|
||||
|
||||
public String getFallback(int id) {
|
||||
System.out.println("调用远程接口异常,返回自定义信息");
|
||||
return "自定义信息";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/2/{id}", method = RequestMethod.GET)
|
||||
public String get2(@PathVariable(value = "id") int id) {
|
||||
return restTemplate.getForObject(URL2, String.class, id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
# server
|
||||
server.port=8888
|
||||
|
||||
# spring
|
||||
spring.application.name=spring-cloud-consumer
|
||||
|
||||
# eureka
|
||||
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/
|
||||
|
||||
# info自定义
|
||||
info.build.name=@project.name@
|
||||
info.build.description=@project.description@
|
||||
info.build.groupId=@project.groupId@
|
||||
info.build.artifact=@project.artifactId@
|
||||
info.build.version=@project.version@
|
||||
|
||||
eureka.instance.status-page-url-path=/info
|
||||
eureka.instance.instanceId=${spring.application.name}:${random.value}
|
||||
eureka.instance.prefer-ip-address=true
|
||||
|
||||
#设置拉取服务注册信息时间,默认60s
|
||||
eureka.client.registry-fetch-interval-seconds=30
|
||||
|
||||
#指定续约更新频率,默认是30s
|
||||
eureka.instance.lease-renewal-interval-in-seconds=15
|
||||
|
||||
#设置过期剔除时间,默认90s
|
||||
eureka.instance.lease-expiration-duration-in-seconds=45
|
||||
|
||||
#设置策略
|
||||
#spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
|
||||
#spring-cloud-provider2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
|
||||
#spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName=com.roncoo.education.configuration.RoncooCustomRule
|
||||
#格式为:应用名.ribbon.NFLoadBalancerRuleClassName=xxx
|
||||
|
||||
# 日志配置,默认是不打印任何的日志
|
||||
logging.level.com.roncoo.education.feign=debug
|
||||
|
||||
# 开启压缩
|
||||
feign.compression.request.enabled=true
|
||||
feign.compression.response.enabled=true
|
||||
# 更多配置
|
||||
feign.compression.request.mime-types=text/xml,application/xml,application/json
|
||||
feign.compression.request.min-request-size=2048
|
||||
|
||||
# 在feign里面禁用hystrix
|
||||
feign.hystrix.enabled=false
|
||||
|
||||
# 在feign和Ribbon里面配置隔离策略(全局配置)
|
||||
#hystrix.command.default.execution.isolation.strategy=SEMAPHORE
|
||||
# 配置单个
|
||||
#hystrix.command.view1.execution.isolation.strategy=SEMAPHORE
|
||||
|
||||
hystrix.command.default.execution.timeout.enabled= false
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ConsumerApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
58
spring-cloud-17/spring-cloud-provider-17/pom.xml
Normal file
58
spring-cloud-17/spring-cloud-provider-17/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-provider-17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-provider-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-bean-17</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
|
||||
@EnableEurekaClient
|
||||
@SpringBootApplication
|
||||
public class ProviderApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProviderApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.roncoo.education.controller;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.roncoo.education.bean.User;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/user")
|
||||
public class ApiUserController {
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@RequestMapping(value = "/get/{id}")
|
||||
public User get(@PathVariable int id, @RequestBody(required = false) String json,
|
||||
@RequestParam(required = false) String name, HttpServletRequest request) {
|
||||
logger.info("请求方法类型:" + request.getMethod() + " RequestBody参数:" + json + " RequestParam参数:" + name);
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName("无境");
|
||||
user.setCreateTime(new Date());
|
||||
// logger.info("请求接口返回:{}", user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public User view(@PathVariable int id) throws InterruptedException {
|
||||
Thread.sleep(50000);
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName("无境1");
|
||||
user.setCreateTime(new Date());
|
||||
logger.info("请求接口返回:{}", user);
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.roncoo.education.controller;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.roncoo.education.bean.User;
|
||||
import com.roncoo.education.service.UserService;
|
||||
|
||||
/**
|
||||
* @author wujing
|
||||
*/
|
||||
@RestController
|
||||
public class FeignApiUserController implements UserService {
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Override
|
||||
public User find(@PathVariable int id) {
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName("无");
|
||||
user.setCreateTime(new Date());
|
||||
logger.info("请求接口返回:{}", user);
|
||||
// throw new RuntimeException();
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
# server
|
||||
server.port=0
|
||||
|
||||
# spring
|
||||
spring.application.name=spring-cloud-provider
|
||||
|
||||
# eureka
|
||||
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/
|
||||
|
||||
# info自定义
|
||||
info.build.name=@project.name@
|
||||
info.build.description=@project.description@
|
||||
info.build.groupId=@project.groupId@
|
||||
info.build.artifact=@project.artifactId@
|
||||
info.build.version=@project.version@
|
||||
|
||||
eureka.instance.status-page-url-path=/info
|
||||
eureka.instance.instanceId=${spring.application.name}:${random.value}
|
||||
eureka.instance.prefer-ip-address=true
|
||||
|
||||
#设置拉取服务注册信息时间,默认60s
|
||||
eureka.client.registry-fetch-interval-seconds=30
|
||||
|
||||
#指定续约更新频率,默认是30s
|
||||
eureka.instance.lease-renewal-interval-in-seconds=15
|
||||
|
||||
#设置过期剔除时间,默认90s
|
||||
eureka.instance.lease-expiration-duration-in-seconds=45
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ProviderApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
57
spring-cloud-17/spring-cloud-service-17/pom.xml
Normal file
57
spring-cloud-17/spring-cloud-service-17/pom.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.roncoo.education</groupId>
|
||||
<artifactId>spring-cloud-17</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-service-17</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-service-17</name>
|
||||
<description>roncoo-spring-cloud project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<executable>true</executable>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||
|
||||
@EnableEurekaServer
|
||||
@SpringBootApplication
|
||||
public class ServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
# server (eureka 默认端口为:8761)
|
||||
server.port=8761
|
||||
|
||||
# spring
|
||||
spring.application.name=spring-cloud-server
|
||||
|
||||
# eureka
|
||||
# 是否注册到eureka
|
||||
eureka.client.register-with-eureka=false
|
||||
# 是否从eureka获取注册信息
|
||||
eureka.client.fetch-registry=false
|
||||
# eureka服务器的地址(注意:地址最后面的 /eureka/ 这个是固定值)
|
||||
#eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
|
||||
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/
|
||||
eureka.client.availability-zones.roncoo=roncooZone
|
||||
eureka.client.service-url.roncooZone=http://roncoo:123456@localhost:8761/eureka/
|
||||
eureka.client.region=lll
|
||||
|
||||
|
||||
# info自定义
|
||||
info.build.name=@project.name@
|
||||
info.build.description=@project.description@
|
||||
info.build.groupId=@project.groupId@
|
||||
info.build.artifact=@project.artifactId@
|
||||
info.build.version=@project.version@
|
||||
|
||||
# 指定环境
|
||||
eureka.environment=dev
|
||||
|
||||
#指定数据中心
|
||||
eureka.datacenter=roncoo
|
||||
|
||||
# 关闭自我保护模式
|
||||
eureka.server.enable-self-preservation=false
|
||||
|
||||
#设置清理无效节点的时间间隔,默认60000,即是60s
|
||||
eureka.server.eviction-interval-timer-in-ms=5000
|
||||
|
||||
# 服务认证
|
||||
security.basic.enabled=true
|
||||
security.user.name=roncoo
|
||||
security.user.password=123456
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.roncoo.education;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ServiceApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user