<properties>
<!-- 项目设置:编码格式UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<druid>1.0.18</druid>
</properties>
<!-- springboot父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Eureka 注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- SpringCloud版本依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
#注册中心配置
#
server:
port: 8000 #项目端口号
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实
@SpringBootApplication
@EnableEurekaServer
public class Config_App {
public static void main(String[] args) {
SpringApplication.run(Config_App.class, args);
}
}
<properties>
<!-- 项目设置:编码格式UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<druid>1.0.18</druid>
</properties>
<!-- springboot父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Eureka 注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- SpringCloud版本依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
##服务端
eureka:
client:
serviceUrl:
#eureka 注册中心地址
defaultZone: http://localhost:8000/eureka
instance:
instance-id: spring-consumer8002 #ip名称
prefer-ip-address: true
server:
#项目端口号
port: 8001
spring:
application:
#服务名称,随便写
name: spring-consumer
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String[] args) {
SpringApplication.run(Client_App.class, args);
}
}
##服务端
eureka:
client:
serviceUrl:
#eureka 注册中心地址
defaultZone: http://localhost:8000/eureka
instance:
instance-id: spring-consumer8002 #ip名称
prefer-ip-address: true
server:
#项目端口号
port: 8001
spring:
application:
#服务名称,随便写
name: spring-consumer
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String[] args) {
SpringApplication.run(Client_App.class, args);
}
}
@Configuration
public class SpringConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RestController
public class ConsumerController {
@Autowired
private RestTemplate rest;
@RequestMapping("/findAll")
public List<String> findAll(){
return rest.getForObject("http://spring-provider-restTem/findAll",List.class);
}
}
<!-- hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
@EnableHystrix
<!-- feign远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
#开启feign调用 跟断路器
feign:
hystrix:
enabled: true
接口调用
Value表示你要调用项目的application.name名称
fallbackFactory表示断路器访问的类 ,@requestMapping()表示你要调用的方法赋值给findAll
也表示Hystrix容错保护
@FeignClient(value="spring-feign-provider",fallbackFactory= ConsumerFallBack.class)
public interface ConsumerService {
@RequestMapping("/getAll")
public String findAll();
}
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
<!-- SpringCloud config-server 分布式配置中心 服务端 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
#项目注册名称
spring.application.name=config-server
#项目发布版本号
server.port=9999
#远程 Git 项目仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/li_quan_you/springcloudconfigce.git
#Gitee : https://gitee.com/longtanmashuai/springcloud-config-server.git
#Github : https://github.com/CoddingMa/-springcloud-config-server.git
#远程 Git 私有项目的账号密码
spring.cloud.config.server.git.username=
#远程 Git 私有项目的密码
spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class ConfigServer_App {
public static void main(String[] args) {
SpringApplication.run(ConfigServer_App.class, args);
}
}
<!-- SpringCloud config-server 分布式配置中心 消费端 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
#项目名称
spring.application.name=config-client
#端口号
server.port=9001
#分支
spring.cloud.config.label=master
#多环境配置,默认 default(MS:Git 仓库配置文件中 profile 与此处对应)
spring.cloud.config.profile=dev
# 注册中心配置路径
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
# 开启分布式配置
spring.cloud.config.discovery.enabled=true
# 要获取的分布式配置名称
spring.cloud.config.discovery.serviceId=config-server
@SpringBootApplication
@EnableEurekaClient
public class Client_App {
public static void main(String[] args) {
SpringApplication.run(Client_App.class, args);
}
}
<!-- SpringCloud Zuul 网关路由 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
eureka:
client:
serviceUrl:
#eureka 注册中心地址
defaultZone: http://localhost:8000/eureka/
instance:
instance-id: spring-zuul5555 #ip名称
prefer-ip-address: true
server:
#项目端口号
port: 5555
spring:
application:
#服务名称,随便写
name: spring-zuul
zuul:
ignored-services: "*" #拦截请求*包含所有
routes: #白名单
student.serviceId: crm-consumer #服务名称
student.path: /student/** #访问student会转发到crm-consumer中
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Zuul_App {
public static void main(String[] args) {
SpringApplication.run(Zuul_App.class, args);
}
}
@Component
public class ZuulDemoFilter extends ZuulFilter{
private static Logger log = LoggerFactory.getLogger(ZuulDemoFilter.class);
public boolean shouldFilter() {
//判断该过滤器是否需要被执行。这里我们直接返回了 true,因此该过滤器对所有请求都会生效。实际运用中我们可以利用该函数来指定过滤器的有效范围。
return true;
}
/**
* 过滤器的具体逻辑。
* 这里我们通过 ctx.setSendZuulResponse(false)令 zuul 过滤该请求,不对其进行路由,
* 然后通过 ctx.setResponseStatusCode(401)设置了其返回的错误码,
* 当然我们也可以进一步优化我们的返回,比如,通过 ctx.getResponse()对响应内容进
行编辑等。
*/
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(),request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
String refer=request.getHeader("refer");
if (accessToken != null) {
return null;//网关路由放行
}
log.warn("token is empty");
ctx.setSendZuulResponse(false);//网关路由过滤不路由
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
} catch (Exception e) {
}
return null;//网关路由放行
}
@Override
public String filterType() {
//过滤器的类型,它决定过滤器在请求的哪个生命周期中执行。这里定义为 pre,代表会在请求被路由之前执行。
return "pre";
}
@Override
public int filterOrder() {
//过滤器的执行顺序。当请求在一个阶段中存在多个过滤器时,需要根据该方法返回的值来依次执行。
return 0;
}
}
<!-- springboot父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<!-- SpringCloud版本依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
//子项目依赖
<dependencies>
<!-- 启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Eureka 注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- feign远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- SpringCloud Zuul 网关路由 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- SpringCloud config-server 分布式配置中心 服务端 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- SpringCloud config-server 分布式配置中心 消费端 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
#注册中心配置
server:
port: 8000 #项目端口号
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
##服务端
eureka:
client:
serviceUrl:
#eureka 注册中心地址
defaultZone: http://localhost:8000/eureka
instance:
instance-id: spring-feign-provider8001 #ip名称
prefer-ip-address: true
server:
#项目端口号
port: 8001
spring:
application:
#服务名称,随便写
name: spring-feign-provider
#开启feign调用 跟断路器
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
#eureka 注册中心地址
defaultZone: http://localhost:8000/eureka/
instance:
instance-id: spring-zuul5555 #ip名称
prefer-ip-address: true
server:
#项目端口号
port: 5555
spring:
application:
#服务名称,随便写
name: spring-zuul
zuul:
ignored-services: "*" #拦截请求*包含所有
routes: #白名单
student.serviceId: crm-consumer #服务名称
student.path: /student/** #访问student会转发到crm-consumer中
@SpringBootApplication //springboot自动装配启动依赖
@EnableZuulProxy //开启路由网关
@EnableEurekaClient //开启注册中心,把当前服务注册到注册中心
@EnableFeignClients //开启feign远程调用 包含ribbon负载均衡跟Hystrix容错保护
@EnableEurekaServer //注册中心 服务端,把当前服务为注册中心
@EnableHystrix //开启容错保护,当用feign远程调用则不需要当前注解
<!-- swagger api
的依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
eureka:
client:
#eureka 注册中心地址
serviceUrl:
defaultZone: http://localhost:8000/eureka/
#eureka 注册中心展示的微服务信息页面,默认为 http://localhost:${server.port}/info
instance:
status-page-url: http://localhost:${server.port}/swagger-ui.html #swagger启动页面访问路径
server:
#项目端口号
port: 8010
spring:
application:
#服务名称,随便写
name: service-swagger
@EnableSwagger2 //开启swagger
@SpringBootApplication //启动springboot自动配置
@EnableEurekaClient //启动注册中心
@Api:修饰整个类,描述 Controller 的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP 响应其中 1 个描述
@ApiResponses:HTTP 响应整体描述
@ApiIgnore:使用该注解忽略这个 API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
<dependency><!-- 引入自己定义的api通用包,可以使用clock部门Entity -->
<groupId>com.ysd</groupId>
<artifactId>crm-api-clock</artifactId>
<version>${project.version}</version>
</dependency>