parent
b4e6ccd888
commit
a7ec79e936
@ -0,0 +1,36 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>mini-spring-cloud</artifactId>
|
||||
<groupId>com.github</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mini-spring-cloud-consumer-examples</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github</groupId>
|
||||
<artifactId>mini-spring-cloud-tutu-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,48 @@
|
||||
package com.github.cloud.examples;
|
||||
|
||||
import com.github.cloud.tutu.discovery.TutuDiscoveryClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/3/20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
static class HelloController {
|
||||
|
||||
@Autowired
|
||||
private TutuDiscoveryClient discoveryClient;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello() {
|
||||
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("provider-application");
|
||||
if (serviceInstances.size() > 0) {
|
||||
ServiceInstance serviceInstance = serviceInstances.get(0);
|
||||
URI uri = serviceInstance.getUri();
|
||||
String response = restTemplate.postForObject(uri.toString() + "/echo", null, String.class);
|
||||
return response;
|
||||
}
|
||||
|
||||
throw new RuntimeException("No service instance for provider-application found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
spring:
|
||||
application:
|
||||
name: consumer-application
|
||||
cloud:
|
||||
tutu:
|
||||
discovery:
|
||||
server-addr: localhost:6688
|
||||
service: ${spring.application.name}
|
@ -0,0 +1,85 @@
|
||||
package com.github.cloud.tutu;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 服务实例
|
||||
*
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/3/20
|
||||
*/
|
||||
public class TutuServiceInstance implements ServiceInstance {
|
||||
|
||||
private String serviceId;
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean secure = false;
|
||||
|
||||
private Map<String, String> metadata;
|
||||
|
||||
public TutuServiceInstance() {
|
||||
}
|
||||
|
||||
public TutuServiceInstance(String serviceId, String host, int port) {
|
||||
this.serviceId = serviceId;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return secure;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return DefaultServiceInstance.getUri(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
public void setServiceId(String serviceId) {
|
||||
this.serviceId = serviceId;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void setSecure(boolean secure) {
|
||||
this.secure = secure;
|
||||
}
|
||||
|
||||
public void setMetadata(Map<String, String> metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.github.cloud.tutu.discovery;
|
||||
|
||||
import com.github.cloud.tutu.TutuDiscoveryProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/3/20
|
||||
*/
|
||||
@Configuration
|
||||
public class TutuDiscoveryAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TutuDiscoveryProperties tutuDiscoveryProperties() {
|
||||
return new TutuDiscoveryProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TutuDiscoveryClient tutuDiscoveryClient(TutuDiscoveryProperties tutuDiscoveryProperties) {
|
||||
return new TutuDiscoveryClient(tutuDiscoveryProperties);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.github.cloud.tutu.discovery;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.github.cloud.tutu.TutuDiscoveryProperties;
|
||||
import com.github.cloud.tutu.TutuServiceInstance;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 服务发现实现类
|
||||
*
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/3/20
|
||||
*/
|
||||
public class TutuDiscoveryClient implements DiscoveryClient {
|
||||
private static final Logger logger = LoggerFactory.getLogger(TutuDiscoveryClient.class);
|
||||
|
||||
private TutuDiscoveryProperties tutuDiscoveryProperties;
|
||||
|
||||
public TutuDiscoveryClient(TutuDiscoveryProperties tutuDiscoveryProperties) {
|
||||
this.tutuDiscoveryProperties = tutuDiscoveryProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
param.put("serviceName", serviceId);
|
||||
|
||||
String response = HttpUtil.get(tutuDiscoveryProperties.getServerAddr() + "/list", param);
|
||||
logger.info("query service instance, serviceId: {}, response: {}", serviceId, response);
|
||||
return JSON.parseArray(response).stream().map(hostInfo -> {
|
||||
TutuServiceInstance serviceInstance = new TutuServiceInstance();
|
||||
serviceInstance.setServiceId(serviceId);
|
||||
String ip = ((JSONObject) hostInfo).getString("ip");
|
||||
Integer port = ((JSONObject) hostInfo).getInteger("port");
|
||||
serviceInstance.setHost(ip);
|
||||
serviceInstance.setPort(port);
|
||||
return serviceInstance;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getServices() {
|
||||
String response = HttpUtil.post(tutuDiscoveryProperties.getServerAddr() + "/listServiceNames", new HashMap<>());
|
||||
logger.info("query service instance list, response: {}", response);
|
||||
return JSON.parseArray(response, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Spring Cloud Tutu Discovery Client";
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.github.cloud.tutu.registry.TutuServiceRegistryAutoConfiguration
|
||||
com.github.cloud.tutu.registry.TutuServiceRegistryAutoConfiguration,\
|
||||
com.github.cloud.tutu.discovery.TutuDiscoveryAutoConfiguration
|
Loading…
Reference in new issue