第三篇
This commit is contained in:
parent
1b84b75db5
commit
23a8e246b7
@ -0,0 +1,50 @@
|
||||
package com.fxb.licensingservice.service;
|
||||
|
||||
import com.fxb.licensingservice.Entity.Organization;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2018/11/22 19:29
|
||||
*/
|
||||
@Service
|
||||
public class OrganizationService {
|
||||
|
||||
private static final String SERVICE_NAME = "organizationservice";
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@Autowired
|
||||
public OrganizationService(DiscoveryClient discoveryClient) {
|
||||
this.discoveryClient = discoveryClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Spring DiscoveryClient查询
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Organization getOrganization(String id) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(SERVICE_NAME);
|
||||
if (instances.size() == 0) {
|
||||
throw new RuntimeException("无可用的服务");
|
||||
}
|
||||
String serviceUri = String.format("%s/organization/%s", instances.get(0).getUri().toString(), id);
|
||||
ResponseEntity<Organization> responseEntity = restTemplate.exchange(serviceUri, HttpMethod.GET
|
||||
, null, Organization.class, id);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
spring:
|
||||
application:
|
||||
#指定名称,以便spring cloud config客户端知道查找哪个配置
|
||||
name: licensingservice
|
||||
profiles:
|
||||
#指定环境
|
||||
active: dev
|
||||
cloud:
|
||||
config:
|
||||
enabled: true
|
||||
eureka:
|
||||
instance:
|
||||
prefer-ip-address: true
|
||||
client:
|
||||
register-with-eureka: true
|
||||
fetch-registry: true
|
||||
service-url:
|
||||
defaultZone: http://localhost:8761/eureka/
|
||||
|
@ -0,0 +1,16 @@
|
||||
package com.fxb.licensingservice;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class LicensingserviceApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
65
springcloud/第三篇所用代码/organizationservice/pom.xml
Normal file
65
springcloud/第三篇所用代码/organizationservice/pom.xml
Normal file
@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.fxb</groupId>
|
||||
<artifactId>organizationservice</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>organizationservice</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.4.RELEASE</version>
|
||||
</parent>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Camden.SR5</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Camden.SR5</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-config-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.fxb.organizationservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class OrganizationserviceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OrganizationserviceApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.fxb.organizationservice.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2018/11/22 18:23
|
||||
*/
|
||||
@RestController
|
||||
public class OrganizationController {
|
||||
|
||||
private static int count=1;
|
||||
|
||||
@GetMapping(value = "/organization/{orgId}")
|
||||
public Object getOrganizationInfo(@PathVariable("orgId") String orgId) throws Exception{
|
||||
if(count%2==0){
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
}
|
||||
count++;
|
||||
Map<String, String> data = new HashMap<>(2);
|
||||
data.put("id", orgId);
|
||||
data.put("name", orgId + "公司");
|
||||
return data;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
spring:
|
||||
application:
|
||||
#指定名称,以便spring cloud config客户端知道查找哪个配置
|
||||
name: organizationservice
|
||||
profiles:
|
||||
#指定环境
|
||||
active: dev
|
||||
cloud:
|
||||
config:
|
||||
enabled: true
|
||||
eureka:
|
||||
instance:
|
||||
prefer-ip-address: true
|
||||
client:
|
||||
register-with-eureka: true
|
||||
fetch-registry: true
|
||||
service-url:
|
||||
defaultZone: http://localhost:8761/eureka/
|
@ -0,0 +1,16 @@
|
||||
package com.fxb.organizationservice;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class OrganizationserviceApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user