mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update nacos version to 0.2.1-RC and format code
This commit is contained in:
parent
2d277e42c1
commit
e4d9f98313
@ -18,7 +18,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<sentinel.version>0.1.1</sentinel.version>
|
<sentinel.version>0.1.1</sentinel.version>
|
||||||
<oss.version>3.1.0</oss.version>
|
<oss.version>3.1.0</oss.version>
|
||||||
<nacos.version>0.2.0</nacos.version>
|
<nacos.version>0.2.1-RC1</nacos.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -1 +1 @@
|
|||||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
@ -26,9 +26,6 @@ public class ConsumerApplication {
|
|||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ConsumerApplication.class, args);
|
SpringApplication.run(ConsumerApplication.class, args);
|
||||||
}
|
}
|
||||||
|
@ -20,26 +20,27 @@ public class TestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private EchoService echoService;
|
private EchoService echoService;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private DiscoveryClient discoveryClient;
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
|
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
|
||||||
public String rest(@PathVariable String str) {
|
public String rest(@PathVariable String str) {
|
||||||
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
|
return restTemplate.getForObject("http://service-provider/echo/" + str,
|
||||||
|
String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
|
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
|
||||||
public String feign(@PathVariable String str) {
|
public String feign(@PathVariable String str) {
|
||||||
return echoService.echo(str);
|
return echoService.echo(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/services/{service}", method = RequestMethod.GET)
|
||||||
@RequestMapping(value="/services/{service}",method = RequestMethod.GET)
|
public Object client(@PathVariable String service) {
|
||||||
public Object client(@PathVariable String service){
|
|
||||||
return discoveryClient.getInstances(service);
|
return discoveryClient.getInstances(service);
|
||||||
}
|
}
|
||||||
@RequestMapping(value="/services",method = RequestMethod.GET)
|
|
||||||
public Object services(){
|
@RequestMapping(value = "/services", method = RequestMethod.GET)
|
||||||
|
public Object services() {
|
||||||
return discoveryClient.getServices();
|
return discoveryClient.getServices();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
spring.application.name=service-consumer
|
spring.application.name=service-consumer
|
||||||
server.port=18083
|
server.port=18083
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
@ -1,4 +1,4 @@
|
|||||||
server.port=18082
|
server.port=18082
|
||||||
spring.application.name=service-provider
|
spring.application.name=service-provider
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
@ -48,7 +48,8 @@ public class NacosDiscoveryProperties {
|
|||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the domain name of a service, through which the server address can be dynamically obtained.
|
* the domain name of a service, through which the server address can be dynamically
|
||||||
|
* obtained.
|
||||||
*/
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
@ -90,8 +91,8 @@ public class NacosDiscoveryProperties {
|
|||||||
private boolean registerEnabled = true;
|
private boolean registerEnabled = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ip address your want to register for your service instance, needn't to set it if
|
* The ip address your want to register for your service instance, needn't to set it
|
||||||
* the auto detect ip works well
|
* if the auto detect ip works well.
|
||||||
*/
|
*/
|
||||||
private String ip;
|
private String ip;
|
||||||
|
|
||||||
@ -290,47 +291,45 @@ public class NacosDiscoveryProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosDiscoveryProperties{" +
|
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
"serverAddr='" + serverAddr + '\'' +
|
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
|
||||||
", endpoint='" + endpoint + '\'' +
|
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
|
||||||
", namespace='" + namespace + '\'' +
|
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
|
||||||
", logName='" + logName + '\'' +
|
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
|
||||||
", service='" + service + '\'' +
|
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
|
||||||
", weight=" + weight +
|
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
|
||||||
", clusterName='" + clusterName + '\'' +
|
+ '\'' + ", secretKey='" + secretKey + '\'' + '}';
|
||||||
", metadata=" + metadata +
|
|
||||||
", registerEnabled=" + registerEnabled +
|
|
||||||
", ip='" + ip + '\'' +
|
|
||||||
", networkInterface='" + networkInterface + '\'' +
|
|
||||||
", port=" + port +
|
|
||||||
", secure=" + secure +
|
|
||||||
", accessKey='" + accessKey + '\'' +
|
|
||||||
", secretKey='" + secretKey + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void overrideFromEnv(Environment env){
|
public void overrideFromEnv(Environment env) {
|
||||||
|
|
||||||
if(StringUtils.isEmpty(this.getServerAddr())) {
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
this.setServerAddr(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getNamespace())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
this.setNamespace(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getAccessKey())) {
|
if (StringUtils.isEmpty(this.getAccessKey())) {
|
||||||
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
this.setAccessKey(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getSecretKey())) {
|
if (StringUtils.isEmpty(this.getSecretKey())) {
|
||||||
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
this.setSecretKey(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getLogName())) {
|
if (StringUtils.isEmpty(this.getLogName())) {
|
||||||
this.setLogName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
this.setLogName(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getClusterName())) {
|
if (StringUtils.isEmpty(this.getClusterName())) {
|
||||||
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.clusterName-name:}"));
|
this.setClusterName(env.resolvePlaceholders(
|
||||||
|
"${spring.cloud.nacos.discovery.clusterName-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEndpoint())) {
|
if (StringUtils.isEmpty(this.getEndpoint())) {
|
||||||
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
this.setEndpoint(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,8 @@ import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
|
|||||||
@Endpoint(id = "nacos-discovery")
|
@Endpoint(id = "nacos-discovery")
|
||||||
public class NacosDiscoveryEndpoint {
|
public class NacosDiscoveryEndpoint {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(NacosDiscoveryEndpoint.class);
|
private static final Logger LOGGER = LoggerFactory
|
||||||
|
.getLogger(NacosDiscoveryEndpoint.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
private NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||||
@ -56,14 +57,15 @@ public class NacosDiscoveryEndpoint {
|
|||||||
result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
|
result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
|
||||||
|
|
||||||
NamingService namingService = nacosRegistration.getNacosNamingService();
|
NamingService namingService = nacosRegistration.getNacosNamingService();
|
||||||
List<ServiceInfo> subscribe = Collections.emptyList() ;
|
List<ServiceInfo> subscribe = Collections.emptyList();
|
||||||
|
|
||||||
try{
|
try {
|
||||||
subscribe = namingService.getSubscribeServices();
|
subscribe = namingService.getSubscribeServices();
|
||||||
} catch (Exception e){
|
}
|
||||||
|
catch (Exception e) {
|
||||||
LOGGER.error("get subscribe services from nacos fail,", e);
|
LOGGER.error("get subscribe services from nacos fail,", e);
|
||||||
}
|
}
|
||||||
result.put("subscribe",subscribe);
|
result.put("subscribe", subscribe);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,19 +20,21 @@ import org.springframework.boot.actuate.autoconfigure.endpoint.condition.Conditi
|
|||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
@ConditionalOnWebApplication
|
@Configuration
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||||
@ConditionalOnClass(Endpoint.class)
|
@ConditionalOnClass(Endpoint.class)
|
||||||
public class NacosDiscoveryEndpointAutoConfiguration {
|
public class NacosDiscoveryEndpointAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnEnabledEndpoint
|
@ConditionalOnEnabledEndpoint
|
||||||
@Bean
|
|
||||||
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
|
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
|
||||||
return new NacosDiscoveryEndpoint();
|
return new NacosDiscoveryEndpoint();
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public class NacosServer extends Server {
|
|||||||
this.metaInfo = new MetaInfo() {
|
this.metaInfo = new MetaInfo() {
|
||||||
@Override
|
@Override
|
||||||
public String getAppName() {
|
public String getAppName() {
|
||||||
return instance.getService().getName();
|
return instance.serviceName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +36,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
|||||||
|
|
||||||
private String serviceId;
|
private String serviceId;
|
||||||
|
|
||||||
public NacosServerList(){}
|
public NacosServerList() {
|
||||||
|
}
|
||||||
|
|
||||||
public NacosServerList(String serviceId) {
|
public NacosServerList(String serviceId) {
|
||||||
this.serviceId = serviceId;
|
this.serviceId = serviceId;
|
||||||
@ -54,11 +55,14 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
|||||||
|
|
||||||
private List<NacosServer> getServers() {
|
private List<NacosServer> getServers() {
|
||||||
try {
|
try {
|
||||||
List<Instance> instances = registration.getNacosNamingService().selectInstances(serviceId, true);
|
List<Instance> instances = registration.getNacosNamingService()
|
||||||
|
.getAllInstances(serviceId);
|
||||||
return instancesToServerList(instances);
|
return instancesToServerList(instances);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId, e);
|
throw new IllegalStateException(
|
||||||
|
"Can not get service instances from nacos, serviceId=" + serviceId,
|
||||||
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user