一、创建名为:eureka的模块(注册中心)
gradle依赖:
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
}
description = "Eureka注册中心"
application.yml
spring:
application:
name: registry
server:
port: 1111
eureka:
server:
evictionIntervalTimerInMs: 60000
enableSelfPreservation: true
renewalPercentThreshold: 0.85
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:1111/eureka
启动类:StartEureka
@EnableEurekaServer
@SpringBootApplication
public class StartEureka {
public static void main(String[] args) {
SpringApplication.run(StartEureka.class, args);
}
}
二、创建名为:provider的模块(生产者)
gradle依赖:
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
compile("org.springframework.cloud:spring-cloud-starter-openfeign")
compile("io.github.openfeign:feign-httpclient")
compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
compile("org.springframework.cloud:spring-cloud-starter-netflix-ribbon")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
}
description = "服务生产者实例"
application.yml
spring:
application:
name: provider
eureka:
client:
registryFetchIntervalSeconds: 10
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
server:
port: 3333
启动类:StartPrivoder
@EnableFeignClients
@EnableCircuitBreaker
@SpringBootApplication
public class StartProvider {
public static void main(String[] args) {
SpringApplication.run(StartProvider.class, args);
}
}
再写个测试的controller
@Controller
@RequestMapping("/provider")
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello,我是生产者";
}
}
三、创建名为:consumer的模块(消费者)
gradle依赖:
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
compile("org.springframework.cloud:spring-cloud-starter-openfeign")
compile("io.github.openfeign:feign-httpclient")
compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
compile("org.springframework.cloud:spring-cloud-starter-netflix-ribbon")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
}
description = "服务消费者实例"
application.yml
spring:
application:
name: consumer
server:
port: 2222
#cloud-demo-provider:
# ribbon:
# NFLoadBalancerRuleClassName: com.suixingpay.cloud.demo.consumer.ribbon.TestRule
feign:
hystrix:
enabled: true
httpclient:
maxConnections: 10
maxConnectionsPerRoute: 2
client:
config:
default:
connectTimeout: 1000
readTimeout: 1000
hystrix:
command:
default:
#ProviderClient#test1(String,Integer):
#fallback:
#enabled: false
circuitBreaker:
requestVolumeThreshold: 3
sleepWindowInMilliseconds: 5000
errorThresholdPercentage: 50
metrics:
rollingStats:
timeInMilliseconds: 10000
healthSnapshot:
intervalInMilliseconds: 500
execution:
timeout:
enabled: false
threadpool:
default:
coreSize: 5
maximumPoolSize: 10
maxQueueSize: 10
queueSizeRejectionThreshold: 3
eureka:
client:
registryFetchIntervalSeconds: 10
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 30
ribbon:
UseIPAddrForServer: true
启动类:StartConsumer
@EnableFeignClients
@EnableCircuitBreaker
@SpringBootApplication
public class StartConsumer {
public static void main(String[] args) {
SpringApplication.run(StartConsumer.class, args);
}
}
客户端接口:Client
@FeignClient(name = "provider", fallback = Client.TestFallback.class, configuration = Client.FallbackConfiguration.class)
@RequestMapping("/provider")
public interface Client {
@RequestMapping("/hello")
public String hello();
/**
* 熔断
*/
public static class TestFallback implements Client {
@Override
public String hello() {
return "熔断了。。。";
}
}
public static class FallbackConfiguration {
@Bean
public TestFallback fallbackFactory() {
return new TestFallback();
}
}
}
再写个测试的controller
@Controller
@RequestMapping("/consumer")
public class HelloController {
@Autowired
Client client;
@RequestMapping("/hello")
@ResponseBody
public String aaa(){
return client.hello();
}
}
执行:
先启动Euareka模块
访问:localhost:1111 可以看到注册在eureka上面的生产者和消费者,但此时没有任何生产者和消费者。
再启动provider模块
然后刷新localhost:1111 可以看到出来了一个生产者。
再启动consumer模块。
然后刷新localhost:1111 可以看到又出来了一个消费者。
使用消费者模块访问http://localhost:2222/consumer/hello 但可以看到显示的是生产者中的内容。