赞
踩
目录
管理服务与服务之间的依赖关系,以实现服务调用,负载均衡,容错等,实现服务注册与服务发现。
在服务注册与发现中,有一个注册中心,当服务启动时,会将该服务的相关信息注册到注册中心上去。
如:服务url地址,服务消费者会以该别名方式去注册中心上获取对应服务,进而进行远程调用。
提供服务注册,个微服务节点启动后,会在注册中心上注册相关信息,服务节点可以在界面中直观看到。
java客户端,客户端默认使用轮询负载均衡,在应用启动后,会向server发送心跳,若server端在多个心跳周期中未发现该服务,会自动将其移除注册表。
导入Eureka服务端、web模块依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- </dependencies>
①服务端口为7001;
②Eureka服务端主机名;
③Eureka客户端:
register-with-eureka:是否在服务中心注册
fetchRegistry:是否可以在注册中心被发现
service-url:服务中心url地址
- server:
- port: 7001
-
- eureka:
- instance:
- hostname: localhost
-
- client:
- register-with-eureka: false
- fetchRegistry: false
- service-url:
- defaultZone: http://localhost:7001/eureka
@EnableEurekaServer:Eureka服务端注解
- @SpringBootApplication
- @EnableEurekaServer
- public class Eureka7001 {
- public static void main(String[] args) {
- SpringApplication.run(Eureka7001.class,args);
- }
- }
访问http://localhost:7001
导入Eureka客户端、web模块、监控依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- </dependencies>
①配置服务端口号为8003;
②配置服务应用名称;
③配置Eureka注册中心,开启注册,指明注册中心地址。
- server:
- port: 8003
-
- spring:
- application:
- name: provider8003
-
- eureka:
- client:
- register-with-eureka: true
- fetchRegistry: true
- service-url:
- defaultZone: http://localhost:7001/eureka
@EnableEurekaClient:指明该服务为Eureka客户端
- @SpringBootApplication
- @EnableEurekaClient
- public class Provider8003 {
- public static void main(String[] args) {
- SpringApplication.run(Provider8003.class,args);
- }
- }
- @RestController
- public class HelloController {
- @GetMapping("/hello")
- public String hello(){
- return "Hello,Eureka";
- }
- }
启动访问http://localhost:8003/hello
可以发现注册中心已经注册该服务
导入Eureka客户端、web、监控依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- </dependencies>
①配置服务·端口号为8004;
②配置服务名称为:Consumer8004;
③配置Eureka客户端,开启注册。配置注册中心地址。
- server:
- port: 8004
-
- spring:
- application:
- name: Consumer8004
-
- eureka:
- client:
- register-with-eureka: true
- fetchRegistry: true
- service-url:
- defaultZone: http://localhost:7001/eureka
开启Eureka客户端
- @SpringBootApplication
- @EnableEurekaClient
- public class Consumer8004 {
- public static void main(String[] args) {
- SpringApplication.run(Consumer8004.class,args);
- }
- }
@LoadBalanced:负载均衡注解
- @Configuration
- public class ResConfig {
- @Bean
- @LoadBalanced
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
- }
①PROVIDER8003:服务提供者的名称,其在注册中心上注册,可以通过该名称找到对应ip地址;
②restTemplate.getForObject(”远程调用服务ip地址“,“调用服务方法返回类型”)
- @RestController
- public class HelloController {
- //调用服务地址
- private final static String url="http://PROVIDER8003";
-
- @Autowired
- RestTemplate restTemplate;
-
- @GetMapping("consumer")
- public String hello(){
- return restTemplate.getForObject(url+"/hello",String.class);
- }
- }
①启动Eureka7001
②启动服务提供者Provider8003
③启动服务消费者Consumner8004
④访问http://loaclhost:8004/hello
结果如下:
测试成功!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。