微服務-springcloud
感覺微服務都差不多概念,最近稍微看了下springcloud,感覺入門還是很簡單的,框架用用就那麽回事,深入的話需要很多時間投入了
學一個東西,我推薦首先從概念上了解到他是做什麽的,什麽時候需要,基本模塊是什麽,然後可以自己寫一些小的例子,後續根據需要深入到探尋源碼
某位熱心同學寫的入門例子,我下載學習了下:http://git.oschina.net/zhou666/spring-cloud-7simple
集成了了Netfix的一些關鍵組件:
服務發現——Netflix Eureka
客服端負載均衡——Netflix Ribbon
斷路器——Netflix Hystrix
服務網關——Netflix Zuul
分布式配置——Spring Cloud Config
Eureka服務發現,啟動後可以在控制臺看到其他配置了該eureka地址的服務
application.properties中定義eureka服務相關信息
server.port=1111 eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/spring.application.name=cloud-eureka-server manager url:http://localhost:1111/
spring-boot 註解方式啟動服務
@SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); } }
打開控制臺可以看到eureka服務已經啟動成功
啟動兩個server2222/2223,在配置中制定eureka的服務地址
spring.application.name=ribbon-consumer server.port=2222 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
@EnableDiscoveryClient @SpringBootApplication public class ComputeServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); } }
啟動後可以看到eureka控制臺有這兩個服務出現
修改端口為3333
啟動ribbon輪詢負載均衡器:
@SpringBootApplication @EnableDiscoveryClient public class RibbonApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } }
可以看到eureka可以發現該服務,並且訪問http://localhost:3333/add?a=1&b=2時,後臺分別傳遞給了2222和2223實現了負載均衡調度
Hystrix熔斷機制:
application.yml
server: port: 8989 spring: application: name: turbine cloud: config: enabled: true uri: http://localhost:8888 eureka: instance: leaseRenewalIntervalInSeconds: 10 client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:1111/eureka/ turbine: aggregator: clusterConfig: CLOUD-SIMPLE-UI appConfig: cloud-simple-ui clusterNameExpression: metadata[‘cluster‘]
@SpringBootApplication @EnableEurekaClient @EnableHystrixDashboard @EnableTurbine public class TurbineApplication { public static void main(String[] args) { SpringApplication.run(TurbineApplication.class, args); } }
Config配置管理:
server.port=8888 spring.cloud.config.server.git.uri=https://git.oschina.net/zhou666/spring-cloud-7simple.git spring.cloud.config.server.git.searchPaths=cloud-config-repo eureka.client.serviceUrl.defaultZone=http\://localhost\:1111/eureka/ spring.application.name=cloud-config-server
@Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
zuul服務網關,類似Nginx反向代理服務器,配置各種Ip過來給哪個下級處理
logging: level.org.springframework.cloud: DEBUG server: port: 8080 zuul: ignoredPatterns: /health,/error retryable: true routes: smarts: stripPrefix: true path: /smart/** serviceId: smarts ribbon: eureka: enabled: false smarts: ribbon: listOfServers: localhost:2222,localhost:2223
定義映射規則,當訪問http://localhost:8080/smart/add?a=1&b=2會按照規則/smart/**將請求定位到2222/2223
@EnableZuulProxy @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
微服務-springcloud