官方文档地址https://springcloud.cc/spring-cloud-dalston.html
springCloud包括:服务发现 Eureka, 短路器Hystrix, 负载平衡器Ribbon, Rest客户端feign 路由器和过滤器zuule、配置中心
eureka的原理:Eureka Server 和 Eureka Client。Eureka Server提供服务发现的能力,各个微服务启动时,会通过Eureka Client向Eureka Server进行注册自己的信息(例如网络信息),Eureka Server会存储该服务的信息。微服务启动后,会周期性地向Eureka Server发送心跳(默认周期为30秒)以续约自己的信息。如果Eureka Server在一定时间内没有接收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点(默认90秒)。Eureka Client会缓存Eureka Server中的信息。即使所有的Eureka Server节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者。配置参数所在类org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean.class及org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.class; 网友总结了这些参数引用一下https://www.cnblogs.com/fangfuhai/p/7070325.html
Feign的原理:Feign 是一个http请求调用的轻量级框架,在处理请求转换和消息解析的过程中,基本上没什么时间消耗。真正影响性能的,是处理Http请求的环节。由于默认情况下,Feign采用的是JDK的HttpURLConnection,所以整体性能并不高。可以通过拓展该接口,使用Apache HttpClient 或者OkHttp3等基于连接池的高性能Http客户端。
新建一个eureka的springboot项目,配置文件
application.yml
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
register-with-eureka: true
fetch-registry: true
server:
port: 8761
spring:
application:
name: eureka
spring:
security:
basic:
enabled: true
user:
name: admin
password: 123
启动程序,访问http://localhost:8761/ ,
进入登录页面,输入用户名和密码,登录
spring-boot版本不一样,登录配置也不同,看下org.springframework.boot.autoconfigure.security.SecurityProperties.class
如下图所示:
eureka注册中心和zookeeper的功能一致。
springcloud官网https://spring.io/projects/spring-cloud,这里面有和springboot版本对应的关系,搭配错了会报很多错误。
2、springcloud整合zookeeper
用的是3.4.13的版本zookeeper,启动zookeeper
项目配置:
spring.application.name=spring-cloud-zookeeper-client-app
server.port=8080
management.security.enabled=false
spring.cloud.zookeeper.connect-string=localhost:2181
spring.cloud.zookeeper.discovery.instanceHost=localhost
spring.cloud.zookeeper.discovery.instancePort=${server.port}
controller请求
@RestController
@RequestMapping("/zookeeper-02")
public class ZookeeperController {
@Value("${spring.application.name}")
private String instanceName;
private final DiscoveryClient discoveryClient;
@Autowired
public ZookeeperController(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@GetMapping("/hello")
@Produces(MediaType.APPLICATION_JSON)
public String hello() {
return "Hello,Zookeeper.";
}
@GetMapping("/services")
@Produces(MediaType.APPLICATION_JSON)
public List<String> serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances(instanceName);
List<String> services = new ArrayList<>();
if (list != null && list.size() > 0 ) {
list.forEach(serviceInstance -> {
services.add(serviceInstance.getUri().toString());
});
}
return services;
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
启动项目,访问http://localhost:8080/zookeeper-02/services 返回值是http://localhost:8080
2、网关项目
pom.xml配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:9100/eureka/
register-with-eureka: true
fetch-registry: true
server:
port: 8089
spring:
application:
name: zuul
zuul:
routes:
api-a:
path: /api-member/**
serviceId: member
api-b:
path: /api-order/**
serviceId: order
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
启动网关后,网关将所有端口号后带/api-member/的url转发到member项目中去,将带/api-order/的url转发到order项目中去。
三、配置中心
服务器端:
pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
如果是eclipse工具还有加个依赖,要不会报错
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.9.0.201710071750-r</version>
</dependency>
application.yml配置
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/zhanglinlove/springcloudtest.git
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
其中uri放置的是配置文件的地址
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
客户端的配置:
pom.xml中的依赖换成下面
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
拉取配置信息的配置写到bootstrap.yml文件中
server:
port: 8889
spring:
application:
name: default
cloud:
config:
profile: test
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
github上的配置文件的名字构成必须是: {application}-{profile}.properties或者{application}-{profile}.yml这种格式,其中客户端的spring.application.name对应着application, spring.cloud.config.profile对应着profile;对不上就读不到数据。
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
@RestController
public class TestController {
@Value("${from}")
String name;
@GetMapping("/hello")
public String getInfo() {
return name;
}
}
启动eureka,再启动配置服务器端,再启动客户端,在浏览器中输入http://localhost:8889/hello
返回对应的from的值。
四、断路器:Hystrix客户端
在微服务架构中,通常有多层服务调用。较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备。
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
启动类加注解@EnableCircuitBreaker
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
调用方法上加断路的注解
@Service
public class OrderService {
@Autowired
private PersonClient personClient;
@HystrixCommand(fallbackMethod = "defaultMethod")
public String getOrder() {
return personClient.getMember();
}
public String defaultMethod() {
return "请稍后重试";
}
}
五、gateway(网关)