SpringCloud负载均衡
时间: 2018-06-22来源:OSCHINA
前景提要
Ribbon
配合eureka可以不用配置文件,自动实现负载均衡。
consumer可以根据项目服务名称进行访问,如果多个服务名称一致,自动实现负载均衡。
Feign
一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。
我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。
由于Feign是基于Ribbon实现的,所以它自带了客户端负载均衡功能。而且能够通过注解的方式找到服务提供方的名称与执行方法。 @FeignClient("eureka-client") public interface DcClient { @GetMapping("/dc") String consumer(); } @RestController public class DcController { @Autowired DcClient dcClient; @GetMapping("/consumer") public String dc() { return dcClient.consumer(); } }
分布式配置中心
Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为==服务端==与==客户端==两个部分。
基于git存储的分布式配置中心
1.建立一个git仓库
2.仓库里建立默认配置文件,和配置中心项目名一致。
3.建立一个针对dev环境的的项目名-dev.yml配置文件。
4.创建一个springboot工程,在pom中引入 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
5.主类添加@EnableConfigServer注解
6.配置文件 spring application: name: config-server cloud: config: server: git: uri: http://git.oschina.net/didispace/config-repo-demo/ server: port: 1201
在这一步后,开始启动服务,看是否存在问题,如果不存在问题,那么可以继续git访问操作 spring.cloud.config.server.git.username:访问Git仓库的用户名 spring.cloud.config.server.git.password:访问Git仓库的用户密码
7.通过浏览器访问测试 http://localhost:1201//{application}-{profile}.yml
配置客户端
1.确保上面的能够执行
2.建立一个springboot应用 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } spring: application: name: config-client cloud: config: uri: http://localhost:1201/ profile: default label: master server: port: 2001
3.启动后查看自己的配置文件 http://localhost:2001/info

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行