一:netflix和springcloud关系
netflix公司开源了很多组件,包括服务注册与发现(Netflix Eureka)、断路器(Netflix Hystrix)、负载均衡(Netflix Ribbon)、网关(Netflix Zuul)、配置管理(Netflix Archaius)、事件总线(spring cloud bus)等等。
springcloud对这些组件实现了封装,形成spring-cloud-netflix核心模块。虽然Spring Cloud到现在为止不只有Netflix提供的方案可以集成,还有很多方案,但Netflix是最成熟的。
但是netfilx公司在宣布2.0以后就不在对源码进行开源
二:Eureka简介
为什么使用Eureka
微服务架构,我认为最核心的问题是就是要解决服务发现与负载均衡,而eureka就是为了解决服务的注册与发现,ribbon解决负载均衡
Eureka采用的是典型的C-S架构,在Eureka架构中有3个重要角色
1:Eureka Server (注册中心服务端)
2:Service Provider(服务提供者客户端)
3:Service Consumer(服务消费者客户端)
4 :服务监控界面
三 dubbo的原理对比
Provider
-
面向接口代理的高性能RPC调用提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。
-
智能负载均衡内置多种负载均衡策略,智能感知下游节点健康状况,显著减少调用延迟,提高系统吞吐量。
-
服务自动注册与发现支持多种注册中心服务,服务实例上下线实时感知。
-
高度可扩展能力遵循微内核+插件的设计原则,所有核心能力如Protocol、Transport、Serialization被设计为扩展点,平等对待内置实现和第三方实现。
-
运行期流量调度内置条件、脚本等路由策略,通过配置不同的路由规则,轻松实现灰度发布,同机房优先等功能。
-
可视化的服务治理与运维提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数。
四 简单的rpc服务治理架构的设计
可以使用socket建立一个长连接,在服务提供者调用注册中心的注册方法,消费端调用服务的调用方法,底层需要用到反射加动态代理
代码核心代码:
package com.cxy; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.ServerSocket;
import java.net.Socket; /**
* RpcFramework
*
* @author william.liangf
*/
public class RpcFramework { /**
* 暴露服务
*
* @param service 服务实现
* @param port 服务端口
* @throws Exception
*/
public static void export(final Object service, int port) throws Exception {
if (service == null)
throw new IllegalArgumentException("service instance == null");
if (port <= || port > )
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Export service " + service.getClass().getName() + " on port " + port);
ServerSocket server = new ServerSocket(port);
for(;;) {
try {
final Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
try {
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
String methodName = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[])input.readObject();
Object[] arguments = (Object[])input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
Method method = service.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(service, arguments);
output.writeObject(result);
} catch (Throwable t) {
output.writeObject(t);
} finally {
output.close();
}
} finally {
input.close();
}
} finally {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 引用服务
*
* @param <T> 接口泛型
* @param interfaceClass 接口类型
* @param host 服务器主机名
* @param port 服务器端口
* @return 远程服务
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {
if (interfaceClass == null)
throw new IllegalArgumentException("Interface class == null");
if (! interfaceClass.isInterface())
throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
if (host == null || host.length() == )
throw new IllegalArgumentException("Host == null!");
if (port <= || port > )
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
Socket socket = new Socket(host, port);
try {
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
output.writeUTF(method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(arguments);
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
Object result = input.readObject();
if (result instanceof Throwable) {
throw (Throwable) result;
}
return result;
} finally {
input.close();
}
} finally {
output.close();
}
} finally {
socket.close();
}
}
});
} }
消费者和提供者代码:
package com.cxy;
/**
*
*
* 服务暴露方法,需要调用服务的export方法
* @author 15084
*
*/
public class RpcProvider {
public static void main(String[] args) throws Exception {
HelloService service = new HelloServiceImpl();
RpcFramework.export(service, );
} }
package com.cxy;
/**
*
* 服务消费方,需要调用rpcframework中的那个refer方法,来对服务进行调用
* @author 15084
*
*/
public class RpcConsumer {
public static void main(String[] args) throws Exception {
HelloService service = RpcFramework.refer(HelloService.class, "127.0.0.1", );
for (int i = ; i < Integer.MAX_VALUE; i ++) {
String hello = service.hello("World" + i);
System.out.println(hello);
Thread.sleep();
}
}
}
package com.cxy; public interface HelloService {
String hello(String name); }
package com.cxy; public class HelloServiceImpl implements HelloService{ public String hello(String name) {
return "Hello " + name;
} }
五 注册中心eureka的服务模块搭建:
在搭建父工程的时候引入一个springcloud组件,这样就可以进行版本控制,然后将相关组价删除就好
然后在注册中心模块引入eureka的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
然后加入配置文件:
server:
port:
eureka:
client:
register-with-eureka: false #单机版建议设置为false,设置false的目的是防止自己注册自己,集群版采用默认的true
fetch-registry: false #单机版建议设置为false,设置false的目的是防止自己发现自己,集群版采用默认的true
spring:
application:
name: eureka-server # 应用名
在启动服务的时候加上@EnableEurekaServer这个注解:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer //开启eureka服务
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
这样就可以进行启动了
就可以通过访问localhost:8761/,访问到注册中心的管理界面
六 zookeeper或者console作为注册中心:
eureka作为注册中心是需要自己新建工程,而console和zookeeper本事一种程序只需要启动相关组件服务就可以并不需要进行代码操作,只是在调用端的时候进行相关发现的修改