eureka 负载均衡

-------- 客户端

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.cloud.eureka.client.ucenter.domain.UserDto;
import com.cloud.eureka.client.ucenter.network.request.QueryUserByIdRequest;
import com.cloud.eureka.client.ucenter.network.response.QueryUserByIdResult;
import com.cloud.profile.third.UcenterThirdService;

@RestController
@RefreshScope
public class SystemController {
    
    private static Logger logger = LoggerFactory.getLogger(SystemController.class);
     
    @Autowired
    private RestTemplate restTemplate;
    @Autowired  
    private LoadBalancerClient loadBalancerClient;  
    
    @RequestMapping(value = "/queryUser", method = RequestMethod.GET)
    public String queryUser(@RequestParam("userId") Long userId) {
        
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("ucenter");
        
        System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"  
                    + serviceInstance.getPort());// 打印当前调用服务的信息 
        String ret = this.restTemplate.getForObject("http://ucenter/hello", String.class); 
//        QueryUserByIdResult<UserDto> ret = this.restTemplate.getForObject("http://ucenter/queryUserById?userId=" +userId, QueryUserByIdResult.class);
//        logger.info("<== 根据userId查询用户 result : " + JSON.toJSONString(ret));
        return JSON.toJSONString(ret);
    }
    
    @Autowired
    private UcenterThirdService ucenterThirdService;
    
    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.GET)
    public String queryUserInfo (@RequestParam("userId") Long userId) {
    
        logger.info("profile response " + Thread.currentThread().getName() + " " + 
                 this.getClass().getSimpleName() + JSON.toJSONString(ucenterThirdService.queryUserInfo(userId)));
        logger.info(ucenterThirdService.index());
        logger.info("profile response " + Thread.currentThread().getName() + " " + this.getClass().getSimpleName() + "<<=-=-=-=>>恭喜用户 " + userId + " 你连接成功<<=-=-=-=>>");
        logger.info("profile response " + Thread.currentThread().getName() + " " + 
                 this.getClass().getSimpleName() + JSON.toJSONString(ucenterThirdService.queryUserList()));
        
        QueryUserByIdRequest queryRequest = new QueryUserByIdRequest(); 
        queryRequest.setUserId(userId);
        logger.info("<== 根据userId查询用户 queryRequest : " + JSON.toJSONString(queryRequest));
        QueryUserByIdResult<UserDto> result = ucenterThirdService.queryUserById(queryRequest);
        logger.info("<== 根据userId查询用户 result : " + JSON.toJSONString(result));
        if (null != result && "000".equals(result.getRespCode())) {
             logger.info("根据userId查询用户信息成功");
        }
        
        return "hello success";
    }
    
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

 

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@ComponentScan(basePackages = {"com.cloud.profile.controller"})
public class ComsumerAppliactionRunner {
    public static void main(String[] args) {
        SpringApplication.run(ComsumerAppliactionRunner.class, args);
    }
    
    @Autowired  
    private RestTemplateBuilder builder;  
    
    @Bean  
    @LoadBalanced
    public RestTemplate restTemplate() {  
        return builder.build();  
    } 
}

--- 提供服务端

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class UcenterApplicationRunner {
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(UcenterApplicationRunner.class).properties("server.port=" + 8765).run(args);
    }
}

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class UcenterStartRunner {
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(UcenterStartRunner.class).properties("server.port=" + 8762).run(args);
    }
    
}

--- eureka server 的 application.yml

server:
  port: 8761
  
# 是否要开启基本的鉴权  
security:
  basic:
    enabled: false

management:
  security:
    enabled: false

spring:
  application:
    name: eureka
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/Users/xuzhi268/zhongchou/config_profiles

eureka:
  instance:
    hostname: eureka
  client:
    register-with-eureka: false # 禁用eureka作为客户端注册自己
    fetch-registry: false
    service-url:
      default-zone: http://localhost:8761/eureka/

 

转载于:https://my.oschina.net/u/2510361/blog/1822642

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值