-------- 客戶端web
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;spring
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;json
@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";
}
}app
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;dom
@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();
}
}fetch
--- 提供服務端ui
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;this
@SpringBootApplication
@EnableEurekaClient
public class UcenterApplicationRunner {
public static void main(String[] args) {
new SpringApplicationBuilder(UcenterApplicationRunner.class).properties("server.port=" + 8765).run(args);
}
}url
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;.net
@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/