前言
这是一个完整的分布式微服务SpringCloud的搭建过程
这里使用一个父工程来提供整个springCloud的版本控制这里使用的是——Greenwich.SR1
一、创建服务提供
- 一般使用SpringBoot来创建一个个的微服务(创建过程就是平常的springBoot项目)
- 部分代码
- 启动类
package com.yx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication//springBoot注解
@EnableDiscoveryClient//用于注册中心发现服务的注解
@MapperScan("com.yx.dao")//这里是Mapper(dao)层的扫描
public class BillApplication {
public static void main(String[] args) {
SpringApplication.run(BillApplication.class,args);
}
}
- controller
@RestController
@RequestMapping("/bill")
public class BillController {
@Resource
private BillService billService;
@Resource
private TypeService typeService;
@RequestMapping("/{id}")
public DataRe getById2(@PathVariable Long id){
Bill bill = billService.findOne(id);
DataRe dataRe = new DataRe();
dataRe.setData(bill);
dataRe.setStatus(999);
return dataRe;
}
- 配置文件
二、创建服务注册中心
-
Eureka是服务注册中心,只做服务注册;自身并不提供服务也不消费服务。可以搭建Web工程使用Eureka,可以
使用Spring Boot方式搭建 -
配置文件
-
启动类
package com.yx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//声明当前应用时Eureka服务
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
三、创建服务调用者
- 就是调用这个服务的,这里用来做一个测试,可以直接通过url地址进行相关的调用
@Controller
@RequestMapping("/con")
public class ConstmerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/{id}")
public String find(@PathVariable Integer id){
String url = "http://localhost:8080/bill/"+id;
return restTemplate.getForObject(url, String.class);
}
四、创建网关服务
- 网关服务的配置–包含ribbon负载均衡的配饰以及hystrix的配置
下面是相关的配置
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
#路由的id,可以随意些
- id: bill-service-rote
#代理的微服务地址
#uri: http://127.0.0.1:9091
uri: lb://bill-service
#路由断言:可以配置映射路径
predicates:
- Path=/bill/**
filters:
# 1:去除一个路径,2:去除2路径,以此类推
# - StripPrefix=1
default-filters:
- AddResponseHeader=X-Response-Foo, Bar
- AddResponseHeader=myname, yx
globalcors:
corsConfigurations:
'[/**]':
#allowedOrigins: * # 这种写法或者下面的都可以,*表示全部
allowedOrigins:
- "http://docs.spring.io"
- "http://localhost"
allowedMethods:
- GET
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 #服务降级超时时间,默认1S
ribbon:
ConnectTimeout: 1000 # 连接超时时长
ReadTimeout: 2000 # 数据通信超时时长
MaxAutoRetries: 0 # 当前服务器的重试次数
MaxAutoRetriesNextServer: 0 # 重试多少次服务
总结
- 搭建微服务的过程在我看来可以分为三步(这里没有将配置中心一个Spring消息队列的配置,需要可以找我要)第一步,搭建一个个的服务,第二步,搭建一个注册中心,第三步,配置网关(注意跨域问题)【配置网关中要注意一些配置,具体看上一个springCloud相关知识】。