1.微服务相关文章
2.微服务论文
3.Eureka 服务配置
1.)配置 mavne依赖
<!--导入Eureka Server依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--热部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
2.)在application.yml 文件中配置 eureka 注册服务
eureka:
instance:
hostname: localhost #Eureka服务端的实例名字
client:
register-with-eureka: false #表示是否向 Eureka 注册中心注册自己(这个模块本身是服务器,所以不需要)
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: #监控页面~
#重写Eureka的默认端口以及访问路径 —>http://localhost:7001/eureka/
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
3.)在主启动类中加入注解支持EnalbleEurekaClient
@EnableEurekaServer
4.)在contoller中添加
5). eureka-client
调整之前创建的springlouc-provider-dept-8001
导入Eureca依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency>
Eureka配置:配置服务注册中心地址
eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
为主启动类添加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient //EnableEurekaClient
客户端的启动类,在服务启动后自动向注册中心注册服务 public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
} }
先启动7001服务端后启动8001客户端进行测试,然后访问监控页http://localhost:7001/ 产看结果如图,成功
修改Eureka上的默认描述信息
Eureka配置:配置服务注册中心地址
server:
port: 8001
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
##修改eureka上面的默认描述信息
instance:
instance-id: springcloud-provider-dept-8001
##导入相应的maven依赖后 可以配置监控信息 在进入info界面会显示 流弊
info:
app.name: mybes
company.name: no
CAB
C強一致性
A可用性
P分区
ACID
A原子性
C】一致性
I隔离性
D持久性
因为所有分布式不可能同时实现 CAP 只能实现两种 所以 Eureka 是实现 AP
zookeeper实现的是 CP
因此Eureka能很好的应对网络故障导致的部分节点失去联系 而不会像zookeeper那样导致所有服务瘫痪
ribbon技术junhe使用的是轮询 和 随机分配实现负载均衡
ribbon实现是在客户端服务中实现
通过访问客户端服务来 实现像不同的消费者传递请求 轮询机制
在自己配置ribbon时 要注意 Rule 要单独拿出来放到 注意层级
在ribbon服务启动时会采用覆盖的策略
在服务器端中的详细配置如下图
- application.yml
- 开启自定义ribbon扫描
@RibbonClient(name = “EUREKA”, configuration = RuleConfig.class)
//name时注册服务时用的名字
//vonfiguration 是配置的自定义ribbon所在的地址
- 在这里开启ribbon服务
4. controller服务中负载均衡到用其他服务
5.自定义ribbon
public class TransferFiveServiceRuleCloud extends AbstractLoadBalancerRule {
/**
* 总共被调用的次数,目前要求每台被调用5次
*/
private int total = 0;
/**
* 当前提供服务的机器号
*/
private int currentIndex = 0;
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
//int index = rand.nextInt(serverCount);
//server = upList.get(index);
if (total < 5) {
// 指定调用的微服务
server = upList.get(currentIndex);
total++;
} else {
total = 0;
currentIndex++;
if (currentIndex >= upList.size()) {
currentIndex = 0;
}
}
if (server == null) {
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
server = null;
Thread.yield();
}
return server;
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
6.用bean引入自定义的ribbon 与上文中的 @RibbonClient(name = “EUREKA”, configuration = RuleConfig.class)对应
@Configuration
public class RuleConfig {
@Bean
public IRule rule() {
return new TransferFiveServiceRuleCloud();
}
二 . 实现负载均衡地二中方法 feign
- feign是面向端口 方式
- feign是集成了 ribbon的一种方式
- 在实际开发中feign比 ribbon更受欢迎
- 在feign 写法 在api的model中添加接口 value=eureka服务名称
@Component
@FeignClient(value = "EUREKA", fallbackFactory = EmployeeFeignHystrixService.class)
public interface EmployeeFeignService {
@GetMapping("/user/cese") //client服务中的请求
Object get() ;
}
- modelapi中实现上面的接口
@Component
public class EmployeeFeignHystrixService implements FallbackFactory {
@Override
public Object create(Throwable throwable) {
return new EmployeeFeignService() {
@Override
public Object get() {
return null;
}
};
}
}
- 在客户端服务中的controller中需要开启以下请求
@Controller
public class EmployeeController {
@Autowired
private EmployeeFeignService employeeFeignService;
@ResponseBody
@GetMapping("/user") //直接在当前端口下的请求
public Object user(){
return this.employeeFeignService.get();
}
}
- 在主启动类中配置fiegn端口 和 开启fiegn服务
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.jacklinsir.cloud.service") //扫描feign接口
//@RibbonClient(name = "CLOUD-PROVIDER-EMP", configuration = RuleConfig.class) //在启动该微服务的时候就能去加载我们的自定义Ribbon配置类.
public class CloudConsumerFeignEmpApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConsumerFeignEmpApplication.class, args);
}
}