一、Eureka注册中心

Eureka注册中心

1.创建Eureka注册中心服务,

1.1.选用阿里云仓库,引入注册中心服务端依赖(创建时可直接勾选),等待完成项目构建。(全量配置)

<dependency>
	<-- Eureka注册中心服务依赖 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

1.2.如果没有/resources/application.yml文件需自行创建。(全量配置)

server:
  port: 9800 #本项目服务端口
spring:
  application:
    name: registry-center-service #项目名称
eureka:
  client:
    register-with-eureka: false #是否向Eureka服务端注册自己
    fetch-registry: false #是否从Eureka服务端获取服务注册表

1.3.启动类增加注解@EnableEurekaServer,表示启动Eureka。

2.Eureka高可用配置,多个Eureka服务相互注册。

2.1.服务1,编辑application-9800.yml文件。(全量配置)

server:
  port: 9800 #本项目服务端口
spring:
  application:
    name: registry-center-service #项目名称
eureka:
  instance:
    prefer-ip-address: true #从hostname:port变为ip:port
    instance-id: server:${server.port}  #注册中心页面显示服务:server:port
  client:
    service-url:
      defaultZone: http://localhost:9800/eureka/,http://localhost:9801/eureka/,http://localhost:9802/eureka/  #将配置中心服务注册到注册中心

2.2.服务2,编辑application-9801.yml文件。(全量配置)

server:
  port: 9801 #本项目服务端口
spring:
  application:
    name: registry-center-service #项目名称
eureka:
  instance:
    prefer-ip-address: true #从hostname:port变为ip:port
    instance-id: server:${server.port}  #注册中心页面显示服务:server:port
  client:
    service-url:
      defaultZone: http://localhost:9800/eureka/,http://localhost:9801/eureka/,http://localhost:9802/eureka/  #将配置中心服务注册到注册中心

2.3.服务3,编辑application-9802.yml文件。(全量配置)

server:
  port: 9803 #本项目服务端口
spring:
  application:
    name: registry-center-service #项目名称
eureka:
  instance:
    prefer-ip-address: true #从hostname:port变为ip:port
    instance-id: server:${server.port}  #注册中心页面显示服务:server:port
  client:
    service-url:
      defaultZone: http://localhost:9800/eureka/,http://localhost:9801/eureka/,http://localhost:9802/eureka/  #将配置中心服务注册到注册中心

2.4.idea启动多个服务验证,可以在启动参数设置“有效配置文件”指定yml文件。
2.5.其他服务可以都注册进来。

3.创建Eureka公共服务类

3.1.选用阿里云仓库,引入Web和注册中心客户端依赖(创建时可直接勾选),等待完成项目构建。(全量配置)

<dependency>
	<-- Web依赖 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<-- Eureka注册中心客户端依赖 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2.如果没有/resources/application.yml文件需自行创建。(全量配置)

server:
  port: 9600 #默认端口号
spring:
  application:
    name: common-server #项目名称
eureka:
  instance:
    prefer-ip-address: true #将hostname:port变为ip:port
    instance-id: server:${server.port} #注册中心页面显示服务:server:port
  client:
    service-url:
      defaultZone: http://localhost:9800/eureka/,http://localhost:9801/eureka/,http://localhost:9802/eureka/

3.3.编写服务端
3.3.1.ServiceController对象。

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController public class ServiceController { Logger logger = LoggerFactory.getLogger(ServiceController.class);@GetMapping("/getId/{id}") public String getId(@PathVariable("id") int id) {logger.info("服务提供者getUser,方法被执行" + id);return "zhangsan";}@GetMapping("/get") public String get() {logger.info("服务提供者get,方法被执行");return "lisi";}@GetMapping("/getName") public String getName(@RequestParam("name") String name) {logger.info("服务提供者get,方法被执行" + name);return name;}}

3.4.idea启动多个服务验证是否注册到注册中心上,可以在虚拟机参数添加端口:-Dserver.port=9601。

4.创建Eureka服务调用类(基于RestTemplate的远程调用)

4.1.选用阿里云仓库,引入Web和注册中心客户端依赖(创建时可直接勾选),等待完成项目构建。(全量配置)

<dependency>
	<-- Web依赖 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<-- Eureka注册中心客户端依赖 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

4.2.如果没有/resources/application.yml文件需自行创建。(全量配置)

server:
  port: 8081 #默认端口号
spring:
  application:
    name: user-server  #项目名称
eureka:
  instance:
    prefer-ip-address: true #将hostname:port变为ip:port
    instance-id: server:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:9800/eureka/,http://localhost:9801/eureka/,http://localhost:9802/eureka/

4.3.编写客户端
4.3.1.BeanConfiguration初始化RestTemplate对象

import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configuration public class BeanConfiguration { @Bean @LoadBalanced public RestTemplate getRestTemplate() {return new RestTemplate();}}

4.3.2.ClientController对象

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.web.bind.annotation.*;import org.springframework.web.client.RestTemplate;@RestController public class ClientController { Logger logger = LoggerFactory.getLogger(ClientController.class);@Autowired @Qualifier("restTemplate") RestTemplate restTemplate;@GetMapping("/client/{id}") public String hello(@PathVariable("id") int id) {logger.info("请求进入"); /*return restTemplate.getForObject("http://spring-cloud-eureka-product:8801/getUser/" + id, String.class);*/return restTemplate.getForObject("http://spring-cloud-eureka-product/getId/" + id, String.class);/*不需要写端口,@LoadBalanced 会根据名称找到对应服务列表,做负载均衡。*/}}

5.Ribbon负载均衡策略

  • Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。
  • Spring Cloud Balancer中实现了轮询RoundRobinLoadBalancer随机数RandomLoadBalancer两种负载均衡算法。

6.基于OpenFeiqn的远程调用(替换RestTemplate方式)

6.1.是以接口注解的方式调用,添加OpenFeiqn的依赖。(全量配置)

<dependency>
	<-- Web依赖 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<-- Eureka注册中心客户端依赖 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
	<-- openfeign注册中心客户端依赖 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

6.2.编写客户端
6.2.1.IClientFeiqn,这里需要为接口和方法添加相应的注解
@FeignClient(“spring-cloud-eureka-product”),对应的服务提供者在Eureka中的注册服务名称。
@GetMapping(“/getId/{id}”),对应的服务提供者的请求方法名,注意,注意,注意请求路径。
@RequestParam(“id”),对应的服务提供者的请求参数名称。

import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;@FeignClient("spring-cloud-eureka-product") public interface IClientFeiqn { @GetMapping("/getId/{id}") String method1(@RequestParam("id") int id);@GetMapping("/get") String method2();@GetMapping("/getName") String method3(@RequestParam("name") String name);}

6.2.2.ClientFeiqnController,调用客户端注入IClientFeiqn接口类,且需要在启动类添加@EnableFeignClients注解,表示开启feign客户端,Spring 会自动生产代理对象并注入到该类。

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController public class ClientFeiqnController { Logger logger = LoggerFactory.getLogger(ClientFeiqnController.class);@Autowired public IClientFeiqn feiqn;@GetMapping("/fei1") public String hello(int id) {logger.info("地址参数方法");return feiqn.method1(id);}@GetMapping("/fei2") public String hello1() {logger.info("无传参方法");return feiqn.method2();}@GetMapping("/fei3") public String hello1(String name) {logger.info("正常传参方法" + name);return feiqn.method3(name);}}

7.基于OpenFeiqn的多模块开发

7.1.创建quickstart-Maven工程,然后再去创建多模块user-api和user-provider,user-api创建feign功能,user-provider引用user-api模块(可单独打包其他项目也可引用),实现多模远程调用,具体操作就不展示了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值