在前面的过程当中,咱们建立了4个project:html
咱们使用Eureka 做为服务发现组件,学习了Eureka Server
,Eureka Client
的使用。java
Eureka Servermysql
<dependency> <groupId>org.springframework.cloud</groupId> <!--<artifactId>spring-cloud-netflix-eureka-server</artifactId>--> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.2.7.RELEASE</version> </dependency>
@SpringBootApplication @EnableEurekaServer public class DiscoveryApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryApplication.class, args); } }
eureka: instance: hostname: server1 prefer-ip-address: false client: service-url: defaultZone: http://server2:8888/eureka/,http://server3:9999/eureka/
使用Sprint Boot 项目三部曲,咱们能够快速添加一个新组件,并正常使用git
Nacos Servergithub
这个我没有在项目中实现,可是你们能够和Eureka同样,三部曲搞定。spring
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency>
加注解sql
在早期版本中,咱们须要添加@EnableDiscoveryClient
,可是在nacos 0.9以后,不须要咱们显示的添加注解了~,所以这步能够忽略。数据库
改配置api
spring: cloud: nacos: discovery: server-addr: localhost:8848 #前提是要启动Nacos Server metadata: version: v1 # 指定namespace(profile) #namespace: 404060ce-2e6c-4f72-8083-2beb4ca921ad # 指定集群名称 cluster-name: BJ
Nacos Server ,请你们自行搜索,可参考 Nacos Github负载均衡
服务发现
上,所以它也是一个client,那么须要引入spring-cloud-starter-netflix-eureka-client
)<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
/** * @SpringCloudApplication 是如下三个注解的组合注解 * @see SpringBootApplication // 标柱是Spring Boot 项目启动 * @see EnableDiscoveryClient // 标柱为服务发现 client,引入Eureka依赖以后 等同于 @EnableEurekaClient * @see EnableCircuitBreaker // 断路器,后续咱们会讲解 */ @SpringCloudApplication @EnableZuulProxy //启动网关代理服务 public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
zuul: # ignored-services: '*' # 过滤全部请求,除了下面routes中声明过的服务 routes: sponsor: #在路由中自定义服务路由名称 path: /ad-sponsor/** serviceId: mscx-ad-sponsor #微服务name strip-prefix: false search: #在路由中自定义服务路由名称 path: /ad-search/** serviceId: mscx-ad-search #微服务name strip-prefix: false prefix: /gateway/api strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀
具体的代码,参考源代码实现。
这个其实你们就能够看成是本项目内的工具类就好了,没什么特殊的需求。
该项目中,咱们使用到的技术有:
jpa
后续咱们要添加的技术
每一种技术都有一套完整的实现以及框架,想要深刻学习的同窗请自行索引,后期广告系统结束以后,我会另起一个系列来和你们一块儿讨论框架底层实现。