spring cloud 之demo



springboot项目构建demo

@EnableDiscoveryClient
@EnableResourceServer
@SpringBootApplication
public class MerchantApplication {

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

在demo的resource下面创建.yml

spring:
application:
name: madmin
profiles:
active: default
cloud:
config:
enabled: true
uri:  指向config
# devtools:
# livereload:
# port: 35730
#debug: true

pom.xml 添加

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

<dependency>  该jar包为eureka的连接


<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>

代码: 远程连接eureka的服务

@Autowired
private DiscoveryClient discoveryClient;

RestTemplate restTemplate = new RestTemplate();
List<ServiceInstance> instances =
discoveryClient.getInstances("admin");

if (instances.size() == 0)
return null;
log.info(XinyuanJSONUtil.gSonStringN(instances));

ServiceInstance t = instances.get(0);
String url = t.getUri()+"/"+t.getServiceId().toLowerCase();
log.info(url);
String serviceUri = String.format("地址",
url,id,loginType);
ResponseEntity<BaseResultDataDTO<Authorize>> restExchange =
restTemplate.exchange(serviceUri,HttpMethod.GET,null,new ParameterizedTypeReference<BaseResultDataDTO<Authorize>>(){},id,loginType);
BaseResultDataDTO<Authorize> authorize = restExchange.getBody();
log.info(XinyuanJSONUtil.gSonStringN(authorize));
return authorize.getData();

 
 
posted @ 2018-09-05 17:38  全栈冲鸭!  阅读(186)  评论(0编辑  收藏  举报