Eureka 注册中心的使用

环境 springboot + springcloud

Eureka-Server注册中心服务端

pom.xml导入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.2.7.RELEASE</version> <!-- 一般在父工程中就配置了 -->
</dependency>

aplication.yml配置

yml 复制代码
server:
  port: 10086
spring:
  application:
    name: eurekaserver   # 服务名,需要配置
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

EurekaServerApplication.java启动类配置

java 复制代码
@SpringBootApplication
@EnableEurekaServer  // 开启eureka的注册中心功能
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Service提供者

pom.xml

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.7.RELEASE</version> 	<!-- 一般在父工程中就配置了 -->
</dependency>

application.yml

yml 复制代码
spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

consumer消费者 (消费者也可以作为提供者身份出现)

pom.xml

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.7.RELEASE</version> 	<!-- 一般在父工程中就配置了 -->
</dependency>

application.yml

yml 复制代码
spring:
  application:
    name: orderservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

OrderApplication 配置RestTemplate的Bean加入到Spring容器中

java 复制代码
@MapperScan("cn.xxx.order.mapper")
@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    @Bean
    @LoadBalanced # 负责均衡 和 做拉取服务
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

OrderService 利用RestTemplate调用 UserService接口

java 复制代码
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        Order order = orderMapper.findById(orderId);

		// restTemplate.getForObject(请求地址, 返回值类型);
        String url = "http://userservice/user/" + order.getUserId(); // userservice是提供者的spring.application.name
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        
        return order;
    }
}

Ribbon负载均衡 (service配置)

方式1:重写IRule接口的实现Bean

java 复制代码
@Bean
public IRule randomRule() {
    return new RandomRule();
}

方式2:配置文件指定

application.xml

yml 复制代码
userservice: # 给某个微服务配置负载均衡规则,这里是userservice服务
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 负载均衡规则 
    # 当第一次访问时才会拉取服务再做缓存

饥饿加载配置:容器加载完毕后就拉取服务做缓存

yml 复制代码
ribbon:
  eager-load:
    enabled: true
    clients:
      - userservice		# 指定一启动就加载的服务,可以配置多个
      -  xxxservice1
相关推荐
zhang98800002 小时前
docker liunx的底层逻辑是什么,docker 的原理是什么?怎么部署及应用,Docker的来龙去脉
docker·容器·eureka
randy.lou10 小时前
SpringBoot: Eureka入门
spring boot·后端·eureka
程序员三木21 小时前
[gpt胡说八道篇] 使用Docker快速启动Doris
gpt·docker·eureka
2401_857638031 天前
跨越地域界限:Eureka实现跨区域服务发现全解析
java·eureka
程序猿不脱发22 天前
注册中心不知选哪个?Zookeeper、Eureka、Nacos、Consul和Etcd 5种全方位剖析对比
zookeeper·eureka·consul
ly49832 天前
docker 安装syslog
云原生·eureka
天下·第二2 天前
使用【docker】简单部署打包构建好的镜像并运行python项目
python·docker·eureka
青石路2 天前
不单独部署注册中心,又要具备注册中心的功能,咋不让我上天?
eureka·注册中心
stateCelebrateking3 天前
20240622服务器异常
java·服务器·eureka
AaWaLa3 天前
Eureka是什么?它是如何工作的?
云原生·eureka