一、創建名爲: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 但可以看到顯示的是生產者中的內容。