茂展的分享博客

复习查漏补缺(一)

复习查漏补缺(一)

Nginx和Ribbon的比较

服务器端负载均衡 Nginx

nginx 是客户端所有请求统一交给 nginx,由 nginx 进行实现负载均衡请求转发,属于服务器端负载均衡。

既请求由 nginx 服务器端进行转发

客户端负载均衡 Ribbon

Ribbon 是从 eureka 注册中心服务器端上获取服务注册信息列表,缓存到本地,然后在本地实现轮询负载均衡策略。

既在客户端实现负载均衡。

有关Feign和Ribbon

spring-cloud-starter-feign 里面已经包含了 spring-cloud-starter-ribbon(Feign 中也使用了 Ribbon)

Spring Cloud Netflix 的微服务都是以 HTTP 接口的形式暴露的,所以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去调用,而 Feign 是一个使用起来更加方便的 HTTP 客戶端,使用起来就像是调用自身工程的方法,而感觉不到是调用远程方法。
feign封装了Http调用流程,更适合面向接口化的编程习惯
在feign底层,通过基于面向接口的动态代理的方式生成实现类,将请求调用委托到动态代理实现类

git本地执行命令

  1. git status
  2. git clone 地址
  3. git add .
  4. git commit -m ‘简述’
  5. git push origin master(具体的分支)

springcloud中的消息总线bus配置问题

我们在开发的时候,微服务中高端配置往往集中配置更简洁,效率更高,运维更容易
所以我们独立部署一个config项目(我使用的springcloud版本是 Dalston.SR1,配合springboot 1.5x使用的)

  1. 引入pom.xml,切记是spring-cloud-config-server,而不是spring-cloud-starter-config-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springclouddemo</artifactId>
<groupId>com.nyist</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>springclouddemo-config</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
</project>

然后我们需要配置存放github的地址,yml文件这样配置

1
2
3
4
5
6
7
8
9
10
server:
port: 9001
spring:
application:
name: springclouddemo-config
cloud:
config:
server:
git:
uri: https://github.com/lmx110522/springcloud-config.git

配置好,一定记得在启动类加上@EnableConfigServer以开启config服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudDemoConfig9001 {

public static void main(String[] args) {
SpringApplication.run(SpringCloudDemoConfig9001.class,args);
}
}

然后我们就可以把我们的各个微服务的工程的配置在git上集中化管理
在需要git管理的服务中的pom.xml添加

1
2
3
4
 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

接着,我们会出现一个叫做bootstrap.yml的文件(这个bootstrap不是前端的那个框架)
bootstrap.yml的优先级高于application.yml,所以我们不需要git管理的放在application.yml中,
bootstrap.yml这样配置

1
2
3
4
5
6
7
spring:
cloud:
config:
name: springcloud-provider-8001 # 需要从github上读取的资源名称 没有yml后缀
profile: dev
label: master
uri: http://localhost:9001

这个时候,启动该服务会从github上拿取配置
但是在我们改变github上的配置的时候,在项目中并没有生效,这个时候因为我们没有引入消息总线bus

引入消息总线bus

分别在config-server工程和需要config-client(需要git管理配置文件的工程)添加

1
2
3
4
 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

同时都要配置rabboitmq信息

1
2
3
4
spring:
rabbitmq:
host: 192.168.13.132
port: 5672

底层利用的是消息队列,会把改变放在消息队列,然后服务从队列中取出
接着在git改变配置文件后,要使用/bus/refresh的POST请求来更新操作(从控住台可以看出)

从控制台得到

借助Postman发现请求结果为:

1
2
3
4
5
6
7
{
   "timestamp": 1533892993040,
   "status": 401,
   "error": "Unauthorized",
   "message": "Full authentication is required to access this resource.",
   "path": "/bus/refresh"
}

这是因为actuator安全认证,所以我们在config-server的yml文件中关闭即可

1
2
3
management:
security:
enabled: false # 不开启actuator安全认证

注:在config-client(需要git管理配置文件的工程)中需要改变的属性所在类添加@RefreshScope,这是重点!

启动后,查看rabbitmq

rabbitmq

然后更改之后git配置之后,我们可以使用postman发起/bus/refresh请求,使配置生效
其实更好的办法,我们可以在github上设置webHooks,这样就避免我们每次都要手动发起更新
具体的如何设置,请看这个博客,写的很好,点我去阅读

------本文结束感谢阅读------
🐶 您的支持将鼓励我继续创作 🐶