Eureka 注册中心的使用

环境 springboot + springcloud

Eureka-Server注册中心服务端

pom.xml导入依赖

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

aplication.yml配置

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

EurekaServerApplication.java启动类配置

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

 


 

Service提供者

pom.xml

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

application.yml

spring:
  application:
    name: userservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

 


 

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

pom.xml

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

application.yml

spring:
  application:
    name: orderservice
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

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

@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接口

@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
@Bean
public IRule randomRule() {
    return new RandomRule();
}
方式2:配置文件指定

application.xml

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

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

ribbon:
  eager-load:
    enabled: true
    clients:
      - userservice		# 指定一启动就加载的服务,可以配置多个
      -  xxxservice1
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值