秒秒pk10外挂 _架构师入门:搭建基本的Eureka架构(从项目里抽取)
- 时间:
- 浏览:2
那末废话,直接上干货,理论每种大伙 还能不能 看其它资料。
这里是每种关键代码,就让时要完整版可运行的代码,请给每各自 留言。
在后继,还将给出搭建高可用Eureka架构的妙招。
1 Eureka的框架图
在Eureka的服务器里,涵盖着记录当前所有服务列表的注册中心,而服务提供者和调用者所在的机器均被称为“Eureka客户端”。
服务提供者会和服务器进行如下的交互:第一,注册某种 能提供的服务,第二,定时发送心跳,以此证明本服务存在生效情况报告。而服务调用者一般会从服务器查找服务,并根据找到的结果从服务提供者这端调用服务。
2.1搭建Eureka服务器
这里大伙 将在EurekaBasicDemo-Server项目里编写Eureka服务器的代码。
第一步,当大伙 创建完Maven类型的项目后,时要在pom.xml里编写该项目所时要的依赖包,关键代码如下。
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId>
5 <artifactId>spring-cloud-dependencies</artifactId>
6 <version>Brixton.SR5</version>
7 <type>pom</type>
8 <scope>import</scope>
9 </dependency>
10 </dependencies>
11 </dependencyManagement>
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-eureka-server</artifactId>
16 </dependency>
17 </project>
从第1到第11行,大伙 引入了版本号是Brixton.SR5的Spring Cloud包,你这种包里涵盖着Eureka的支持包,在第13到16行的代码里,引入了Eureka Server端的支持包,引入后,大伙 并能在项目的java文件里使用Eureka的形态学 。
第二步,在application.yml里,时要配置Eureka服务端的信息,代码如下。
1 server: 2 port: 8888 3 eureka: 4 instance: 5 hostname: localhost 6 client: 7 register-with-eureka: false 8 fetch-registry: false 9 serviceUrl: 10 defaultZone: http://localhost:8888/eureka/
从第2和第5行里,大伙 指定了Eureka服务端使用的主机地址和端口号,这里分别是localhost和8888,也很多很多 说让服务端运行在本地8888号端口,在第10行里,大伙 指定了服务端所在的url地址。
就让这就让是服务器端,很多很多大伙 通过第7行的代码,指定不用向Eureka注册中心注册每各自 ,同理,服务器端的职责是维护服务列表而也有调用服务,很多很多通过第8行的代码指定本端不用检索服务。
第三步,在RegisterCenterApp.java里编写Eureka启动代码。
1 省略必要的package和import代码 2 @EnableEurekaServer //指定本项目是Eureka服务端 3 @SpringBootApplication 4 public class RegisterCenterApp 5 { 6 public static void main( String[] args ) 7 {SpringApplication.run(RegisterCenterApp.class, args);} 8 }
在第6行的main函数里,大伙 还是通过run妙招启动Eureka服务。
运行App.java启动Eureka服务器端后,在浏览器里输入localhost:8888后,还能不能 就看如下图所示的Eureka服务器端的信息面板,其中Instances currently registered with Eureka目前是空的,说明尚未有服务注册到本服务器的注册中心。
2.2 编写作为服务提供者的Eureka客户端
这里大伙 将在EurekaBasicDemo-ServerProvider项目里编写Eureka客户端的代码,在你这种项目里,大伙 将提供有另2个SayHello的服务。
第一步,创建完Maven类型的项目后,大伙 时要在pom.xml里写入本项目的依赖包,关键代码如下。本项目所用到的依赖包就让都用过,很多很多这里就不展开讲了。
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId>
5 <version>Brixton.SR5</version>
6 <type>pom</type>
7 <scope>import</scope>
8 </dependency>
9 </dependencies>
10 </dependencyManagement>
11 <dependencies>
12 <dependency>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-web</artifactId>
15 <version>1.5.4.RELEASE</version>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.cloud</groupId>
19 <artifactId>spring-cloud-starter-eureka</artifactId>
20 </dependency>
21 </dependencies>
第二步,在application.yml里编写针对服务提供者的配置信息,代码如下。
1 server: 2 port: 1111 3 spring: 4 application: 5 name: sayHello 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8888/eureka/
从第2行里,大伙 能就看本服务将启用1111号端口,在第5行,大伙 指定了本服务的名字,叫sayHello,在第9行,大伙 把本服务注册到了Eureka服务端,也很多很多 注册中心里。
第三步,在Controller.java里,编写控制器每种的代码,在其中实现对外的服务。
1 //省略必要的package和import代码 2 @RestController //说明这是个控制器 3 public class Controller { 4 @Autowired //描述Eureka客户端信息的类 5 private DiscoveryClient client; 6 @RequestMapping(value = "/hello/{username}", method = RequestMethod.GET ) 7 public String hello(@PathVariable("username") String username) { 8 ServiceInstance instance = client.getLocalServiceInstance(); 9 //输出服务相关的信息 10 System.out.println("host is:" + instance.getHost()); 11 System.out.println("port is:" + instance.getPort()); 12 System.out.println("ServiceID is:" + instance.getServiceId() ); 13 //返回字符串 14 return "hello " + username; 15 } 16 }
大伙 通过第6和第7行的代码,指定了能触发hello妙招的url格式,在你这种妙招里,大伙 首先通过第10到12行的代码,输出了主机名、端口号和ServiceID等信息,并在第14行里,返回了有另2个字符串。
第四步,编写Spring Boot的启动类ServiceProviderApp.java,代码如下。
1 //省略必要的package和import代码 2 @SpringBootApplication 3 @EnableEurekaClient 4 public class ServiceProviderApp { 5 public static void main( String[] args ) 6 {SpringApplication.run(ServiceProviderApp.class, args);} 7 }
就让这是存在Eureka的客户端,很多很多加入第3行所示的注解,在main函数里,大伙 依然是通过run妙招启动Spring Boot服务。
2.3 编写服务调用者的代码
启动Eureka服务器端的RegisterApp.java和服务提供者端的ServiceProviderApp.java,在浏览器里输入http://localhost:8888/后,在Eureka的信息面板还能不能 就看SayHello服务,如下图所示。
就让这时大伙 在浏览器里输入http://localhost:1111/hello/Mike,能直接调用服务,一并能看浏览器里就看“hello Mike”的输出。不过在大多数的场景里,大伙 一般是在进程里调用服务,而也有简单地通过浏览器调用,在下面的EurekaBasicDemo-ServiceCaller项目里,大伙 将演示在Eureka客户端调用服务的步骤。
第一步。在你这种Maven项目里,大伙 编写如下的pom.xml配置,关键代码如下。
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId> 5 <version>Brixton.SR5</version> 6 <type>pom</type> 7 <scope>import</scope> 8 </dependency> 9 </dependencies> 10 </dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-web</artifactId> 15 <version>1.5.4.RELEASE</version> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-eureka</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.cloud</groupId> 23 <artifactId>spring-cloud-starter-ribbon</artifactId> 24 </dependency> 25 </dependencies>
请大伙 注意,从第21到24行,大伙 时要引入 ribbon的依赖包,通过它大伙 还能不能 实现负载均衡。而其它的依赖包大伙 就让都就让见过,很多很多就不再解释了。
第二步,在application.yml里,大伙 编写针对本项目的配置信息,代码如下。
1 spring: 2 application: 3 name: callHello 4 server: 5 port: 3030 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8888/eureka/
在第3行里,大伙 指定了本服务的名字叫callHello,在第5行里大伙 指定了本服务是运行在3030端口,在第9行里,大伙 把本服务注册到Eureka服务器上。
第三步,编写提供服务的控制器类,在其中调用服务提供者的提供的服务,代码如下。
1 //省略必要的package和import代码 2 @RestController 3 @Configuration 4 public class Controller { 5 @Bean 6 @LoadBalanced 7 public RestTemplate getRestTemplate() 8 { return new RestTemplate(); } 9 10 @RequestMapping(value = "/hello", method = RequestMethod.GET ) 11 public String hello() { 12 RestTemplate template = getRestTemplate(); 13 String retVal = template.getForEntity("http://sayHello/hello/Eureka", String.class).getBody(); 14 return "In Caller, " + retVal; 15 } 16 }
在第7行的getRestTemplate妙招上,大伙 启动了@LoadBalanced(负载均衡)的注解。关于负载均衡的细节将在顶端章节里完整版描述,这里大伙 引入@LoadBalanced注解的由于 是,RestTemplate类型的对象某种 不具备调用远程服务的能力,也很多很多 说,就让大伙 去掉 你这种注解,进程无须能跑通。那末当大伙 引入该注解,该妙招所返回的对象并能具备调用远程服务的能力。
在提供服务的第11行的hello妙招里,大伙 是通过第13行的代码,用RestTemplate类型对象的getForEntity妙招,调用服务提供者sayHello提供的hello妙招。
这里大伙 是通过http://sayHello/hello/Eureka你这种url去发现对应的服务,在你这种url里,只涵盖了服务名sayHello,并那末涵盖服务所在的主机名和端口号,换句话说,该url虽然是通过注册中心定位到sayHello服务的物理位置的。
至于你这种url和该服务物理位置的绑定关系,是在Eureka內部实现的,这也是Eureka还能不能 被称作“服务发现框架”的由于 。
第四步,在ServiceCallerApp.java妙招里,大伙 编写启动本服务的代码,这大伙 就让不粉悉了,很多很多就不再讲述了。
1 //省略必要的package和import代码 2 @EnableDiscoveryClient 3 @SpringBootApplication 4 public class ServiceCallerApp 5 { 6 public static void main( String[] args ) 7 {SpringApplication.run(ServiceCallerApp.class, args); } 8 }
2.4 通过服务调用者调用服务
当大伙 依次启动Eureka服务器(也很多很多 注册中心)、服务提供者和服务调用者的Spring Boot启动进程后,在浏览器里输入http://localhost:8888/后,能在信息面板里就看有有另2个服务,分别是服务提供者SayHello和服务调用者CallHello,如下图所示。
就让服务调用者运行在3030端口上,很多很多就让大伙 在浏览器里输入http://localhost:3030/hello,能就看在浏览器里输出“In Caller, hello Eureka”,这很多很多 明它虽就让来调用了服务提供者SayHello里的hello妙招。
此外,大伙 还能在服务提供者所在的控制台里就看host、port和ServiceID的输出,如下图所示,这能进一步验证了服务提供者里控制器类里的hello妙招被服务调用者调到。