1,首先就是SpringBoot和SpringCloud之间的版本兼容问题,以下是我的配置,
<properties> <junit.version>4.12</junit.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> </parent>
2,如果使用Feign中的断路器,就需要在配置文件中添加(使用的是.yml)
feign: hystrix: enabled: true #打开Feign自带的断路器
比如
@FeignClient(value = "user",fallback = USerServiceHystric.class) public interface UserService { @RequestMapping(value = "/getUser",method = RequestMethod.GET) String get(); }
调用的是user服务的getUser方法(也是一个服务调用另一个服务的其中一种方法),就需要在user服务的配置文件中添加
最后记得实现接口,方法应该覆盖get()方法,就会在user服务挂了的时候执行,而不会大量请求卡在此处,从而调用它的服务也会挂掉
3,整合mybatis(使用的是.yml)
spring: application: name: user #服务名称 datasource: username: root password: 111111 url: jdbc:mysql://localhost:3306/springboot?useSSL=false&serverTimezone=UTC
serverTimezone=UTC Mysql时区问题会出现乱码
4,使用路由网关zuul,(使用的是.yml)
启动类需要的注解
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient @EnableDiscoveryClient
配置文件中添加
zuul: routes: api-a: path: /api-user/** serviceId: user api-b: path: /api-feign/** serviceId: feign
比如请求的是 http://localhost:8889/api-user/getUser 就相当于请求的是http://localhost:8763/getUser 8763是user服务的端口,请求 http://localhost:8889/api-feign/get 就相当于请求的是http://localhost:8888/get 8888是feign服务的端口