
子工程 eurekaserver pom.xmlorg.springframework.boot spring-boot-stater-parent 2.0.7.RELEASE org.springframework.boot spring-boot-stater-web javax.xml.bind jaxb-api 2.3.0 com.sun.xml.bind jaxb-impl 2.3.0 com.sun.xml.bind jaxb-core 2.3.0 javax.activation activation 1.1.1 org.springframework.cloud spring-cloud-denpendencies Finchley.SR2 pom import
eurkaserver application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-server 2.0.2.RELEASE server: port: 8761 eureka: client: register-with-eurka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication{
public static void main(String[] agrs){
SpringApplication.run(EurekaServerApplication.class,args);
}
}
eurekaclient
provider
pom.xml
eurkaserver application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE server: port: 8010 spring: application: name: provider eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true
@SpringBootApplication
public class ProviderApplication{
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class,args);
}
}
controller
@RestController
public class StudentController{
@Autowired
private StudentService studentService;
@RequestMapping("/student/findAll")
public List findAll(){
return studnetServce.finaAll();
}
}
consumer
pom.xml
eurkaserver application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE server: port: 8020 spring: application: name: consumer eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true
@SpringBootApplication
public class ConsumerApplication{
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class,args);
}
}
controller
@RestController
public class ConsumerController{
@Autowired
private StudentService studentService;
@RequestMapping("/customer/findAll")
public List findAll(){
// 利用resttemplate调用Provider提供的接口
return studnetServce.finaAll();
}
}
网关
Zuul
pom.xml
zuul application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-netflix-zuul 2.0.2.RELEASE server: port: 8030 spring: application: name: gateway eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true zuul: routes: provider: /provider/**
@SpringBootApplication
@EnableZuulProxy
@EnableAutoConfiguration
public class ZuulApplication{
public static void main(String[] args){
SpringApplication.run(ZuulApplication.class,args);
}
}
Ribbon负载均衡
spring cloud ribbon
ribbon application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE server: port: 8040 spring: application: name: ribbon eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true启动类@SpringBootApplication public class RibbonApplication{ public static void main(String[] args){ SpringApplication.run(RibbonApplication.class,args); } }controller@RestController public class RibbonController{ @Autowired private RibbonService ribbonService; @RequestMapping("/ribbon/findAll") public ListFeign声明式接口调用 spring cloud feignfindAll(){ // 使用restTemplate调用服务提供者接口 return ribbonService.finaAll(); } }
feign application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-openfeign 2.0.2.RELEASE server: port: 8050 spring: application: name: feign eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true feign: hystrix: enabled: true
@SpringBootApplication
@EnableFeignClients
public class FeignApplication{
public static void main(String[] args){
SpringApplication.run(FeignApplication.class,args);
}
}
controller
@RestController
public class FeignController{
@Autowired
private FeignProciderClient feignProciderClient;
@GetMapping("/ribbon/findAll")
public List findAll(){
return feignProciderClient.finaAll();
}
}
feign client
@FeignClient(value="provider",fallback=FeignError.class)
public interface FeignProciderClient{
@GetMapping("/student/findAll")
public List findAll();
}
熔断
@Component
public class FeignError implements FeignProciderClient{
public List findAll(){
return null;
}
}
容错机制
hystrix
设计原理
监控
feign application.yml org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-openfeign 2.0.2.RELEASE org.springframe.boot spring-boot-stater-actuator 2.0.7.RELEASE org.springframe.cloud spring-cloud-stater-netflix-hystrix 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-netflix-hystrix-dashboard 2.0.2.RELEASE server: port: 8060 spring: application: name: hystrix eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true feign: hystrix: enabled: true management: endpoints: web: exposure: include: 'hystrix.stream'
@SpringBootApplication
@EnableFeignClients
// 声明启动数据监控
@EnableCircuitBreaker
// 声明启动可视化监控
@EnableHystrixDashboard
public class HystrixApplication{
public static void main(String[] args){
SpringApplication.run(HystrixApplication.class,args);
}
}
controller
@RestController
public class HystrixController{
@Autowired
private FeignProciderClient feignProciderClient;
@GetMapping("/ribbon/findAll")
public List findAll(){
return feignProciderClient.finaAll();
}
}
feign client
@FeignClient(value="provider",fallback=FeignError.class)
public interface FeignProciderClient{
@GetMapping("/student/findAll")
public List findAll();
}
@Component
public class FeignError implements FeignProciderClient{
public List findAll(){
return null;
}
}
配置中心
nativeconfigserver application.yml org.springframe.cloud spring-cloud-config-server 2.0.2.RELEASE server: port: 8762 spring: application: name: nativeconfigserver profiles: active: active cloud: config: server: native: search-locations: classpath:/shared
server:
port: 8070
启动类
@SpringBootApplication
// 声明配置中心
@EnableConfigServer
public class NativeConfigServerApplication{
public static void main(String[] args){
SpringApplication.run(NativeConfigServerApplication.class,args);
}
}
nativeconfigclient
pom.xml
nativeconfigclient bootstrap.yml org.springframe.cloud spring-cloud-starter-config 2.0.2.RELEASE spring: application: name: configclient profiles: active: dev cloud: config: uri: http://localhost:8762 fail-fast: true
@SpringBootApplication
public class NativeConfigClientApplication{
public static void main(String[] args){
SpringApplication.run(NativeConfigClientApplication.class,args);
}
}
远程配置文件系统
configclient.yml
server:
port: 8070
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: configclient
configserver
pom.xml
configserver application.yml org.springframe.cloud spring-cloud-config-server 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE server: port: 8888 spring: application: name: configserver cloud: config: server: git: uri: git路径 searchPaths: config username: xxx password: xxx label: master eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/启动类@SpringBootApplication // 声明配置中心 @EnableConfigServer public class ConfigServerApplication{ public static void main(String[] args){ SpringApplication.run(ConfigServerApplication.class,args); } }configclient pom.xmlconfigclient bootstrap.yml org.springframe.cloud spring-cloud-starter-config 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE spring: cloud: config: name: configclient label: master discovery: enabled: true service-id: configserver eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
@SpringBootApplication
public class ConfigClientApplication{
public static void main(String[] args){
SpringApplication.run(ConfigClientApplication.class,args);
}
}
服务跟踪
spring cloud zipkin
zipkin application.yml org.springframework.boot spring-boot-stater-web io.zipkin.java zipkin-server 2.9.4 io.zipkin.java zipkin-autoconfigure-ui 2.9.4 server: port: 9090启动类@SpringBootApplication // 声明启动Zipkin server @EnableZipkinServer public class ZipKinApplication{ public static void main(String[] args){ SpringApplication.run(ZipKinApplication.class,args); } }zipkin client pom.xmlzipkinclient application.yml org.springframework.cloud spring-cloud-stater-zipkin 2.0.2.RELEASE org.springframe.cloud spring-cloud-stater-netflix-eureka-client 2.0.2.RELEASE server: port: 8090 spring: application: name: zipkinclinet sleuth: web: client: enable: true sampler: probability: 1.0 zipkin: base-url: http://localhost:9090/ eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
@SpringBootApplication
public class ZipKinClientApplication{
public static void main(String[] args){
SpringApplication.run(ZipKinClientApplication.class,args);
}
}