发表于: 2018-10-19 16:12:34
3 402
今天主要是学习spring cloud的分布式部署的几个模块,之前自己有尝试过配置spring cloud eureka简单的服务发现注册,今天学习了cloud的简单集群,和负载均衡和熔断的配置,首先贴一张网上的spring cloud的常见几个组件之间的调用关系
结合图中,cloud常见的几个组件为,服务发现注册(所有的server和client)、配置中心(config repo)、消息总线(webhook bus)、负载均衡(zuul)、断路器(hytrix)、链路追踪(sluth,zipkin)等等,
试过的目前就两个,核心的服务发现与注册;负载均衡和熔断的配置--这块我直接用的是feign,说是这个综合了zuul和hytrix,不过我今天还是主要做了负载均衡的部分。。熔断没有搞完。。
Ribbon实现的是客户端负载均衡。服务端负载均衡是在硬件设备或者软件模块中维护一份可用服务清单,然后客户端发送服务请求到这些负载均衡的设备上,这些设备根据一些算法均衡的将请求转发出去。而客户端负载均衡则是客户端自己从服务注册中心中获取服务清单缓存到本地,然后通过Ribbon内部算法均衡的去访问这些服务。
服务发现与注册,我这块用的是eureka,配置的时候,配置两个地方,一个用来接收注册,成为server,在使用的时候,主要是作为注册中心,别的其余组件都注册到它的地址;
还有一个client,所有的组件,除了server,都是算client,都需要注册到server中来进行调用;
之前实现过了注册一个最简单功能的client,因为cloud 的集群功能配置比较简单,之前也实现过了。。今天还是主要实现下feign这个拥有负载均衡和断路器功能的client;
跟client的正常配置一样。。pom文件中添加eureka-client和feign---这个有个需要注意的点,网上找的好多资料,用的spring boot是基于1.4版本的,eureka-client的包和server的包是一样的,2.0版本的eureka分开了,分为了server和client;还有就是fiegn的版本也有变化,原来是figen,2.0变为了openfign的了;
<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-openfeign</artifactId>
</dependency>
同样的注解是在启动类上面加上这个,这倒是没有什么变化
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoFeignApplication {
public static void main(String[] args) {
SpringApplication.run(DemoFeignApplication.class, args);
System.out.println("spring cloud eureka client feign consumer start success");
}
}
feign调用的时候,需要定义一个service接口,来进行调用从注册中心得到的client得到的实例的实现类;
@FeignClient("eureka-client")这个注解中的值,即为需要调用的服务提供者的名称,因为我的提供者名称为eureka-client,后面是两个client的properties文件,我是分别放在不同项目中来进行启动的,没有利用一个项目的不同配置文件;
@FeignClient("eureka-client")
public interface UserService {
@GetMapping("/hello/test")
public String getName();
}
feign的配置文件,很简单,指定一个端口就好了;
spring.application.name=eureka-client-feign-consumer
server.port=9000
eureka.instance.hostname=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
第一个client的配置;
spring.application.name=eureka-client
server.port=8080
eureka.instance.hostname=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
第二个client的配置--区别就是端口号,三个注册的服务中心是一样的;
spring.application.name=eureka-client
server.port=8090
eureka.instance.hostname=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/
三个congroller类的对比
第一个是feign的配置;
@RequestMapping("hello")
@Controller
public class ConsumerController {
@Autowired
UserService userService;
@GetMapping("test")
public String getTestOne() {
System.out.println(userService.getName());
String str=userService.getName();
return str;
}
第一个controller的配置--输出了one用来区别
@Controller
@RequestMapping("/hello")
public class TestController {
@Autowired
StudentService studentService;
@GetMapping("/test")
@ResponseBody
public String testList() {
log.info("***********start service client one**********");
return "hello eureka one test";
}
}
第二个controller的配置;是用的two
@RequestMapping("/hello")
public class TestController {
@GetMapping("/test")
@ResponseBody
public String testList() {
log.info("***********start service client two**********");
return "hello eureka two test";
}
}
最后全部启动,注册中心如下图所示;可以看到两个注册中心,然后两个client和一个feign的consumer;
在浏览器界面输入的http://localhost:9000/hello/test,
feign调用的输出台输出是没有问题的;有时候是one和two切换来,但是,就是我集中使用的话,它是一只用一个,同时,每次调用是两个client都调用了,出现了这个问题。。不知道是不是优先级或是别的什么问题。。
8次都输出了one和two,这个问题再看看网上的怎么配合吧。。
如果我将service的端口更改了,consumer调用的时候,在调用到没有这个映射地址的时候,会报错,但是,另一个正确的映射地址还是在正常的输出语句什么的。。没有调用到的就没有反应。。
感觉是负载均衡策略的问题。。但是找了别人没有配置,顶多是将轮询改为随机,也没有什么别的配置呀;也没找到别人有提过这块的东西。。难受。。
后面再找找这块的东西。。
明天计划:明天请假一天去趟城里帮朋友弄点东西。。
评论