update nacos version to 0.2.1-RC and format code

pull/1534/head
flystar32 6 years ago
parent 2d277e42c1
commit e4d9f98313

@ -18,7 +18,7 @@
<properties>
<sentinel.version>0.1.1</sentinel.version>
<oss.version>3.1.0</oss.version>
<nacos.version>0.2.0</nacos.version>
<nacos.version>0.2.1-RC1</nacos.version>
</properties>
<dependencyManagement>

@ -16,39 +16,39 @@ import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Component
class SampleRunner implements ApplicationRunner {
@Value("${user.name}")
String userName;
@Value("${user.name}")
String userName;
@Value("${user.age}")
int userAge;
@Value("${user.age}")
int userAge;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(userName);
System.out.println(userAge);
}
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(userName);
System.out.println(userAge);
}
}
@RestController
@RefreshScope
class SampleController {
@Value("${user.name}")
String userName;
@Value("${user.name}")
String userName;
@Value("${user.age}")
int age;
@Value("${user.age}")
int age;
@RequestMapping("/user")
public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
}
@RequestMapping("/user")
public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
}
}

@ -1 +1 @@
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

@ -20,22 +20,19 @@ import org.springframework.web.client.RestTemplate;
@EnableFeignClients
public class ConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}
}

@ -15,31 +15,32 @@ import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private EchoService echoService;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
@RequestMapping(value="/services/{service}",method = RequestMethod.GET)
public Object client(@PathVariable String service){
return discoveryClient.getInstances(service);
}
@RequestMapping(value="/services",method = RequestMethod.GET)
public Object services(){
return discoveryClient.getServices();
}
@Autowired
private RestTemplate restTemplate;
@Autowired
private EchoService echoService;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class);
}
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
@RequestMapping(value = "/services/{service}", method = RequestMethod.GET)
public Object client(@PathVariable String service) {
return discoveryClient.getInstances(service);
}
@RequestMapping(value = "/services", method = RequestMethod.GET)
public Object services() {
return discoveryClient.getServices();
}
}

@ -1,4 +1,4 @@
spring.application.name=service-consumer
server.port=18083
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
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=*

@ -48,7 +48,8 @@ public class NacosDiscoveryProperties {
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;
@ -90,8 +91,8 @@ public class NacosDiscoveryProperties {
private boolean registerEnabled = true;
/**
* The ip address your want to register for your service instance, needn't to set it if
* the auto detect ip works well
* The ip address your want to register for your service instance, needn't to set it
* if the auto detect ip works well.
*/
private String ip;
@ -290,47 +291,45 @@ public class NacosDiscoveryProperties {
@Override
public String toString() {
return "NacosDiscoveryProperties{" +
"serverAddr='" + serverAddr + '\'' +
", endpoint='" + endpoint + '\'' +
", namespace='" + namespace + '\'' +
", logName='" + logName + '\'' +
", service='" + service + '\'' +
", weight=" + weight +
", clusterName='" + clusterName + '\'' +
", metadata=" + metadata +
", registerEnabled=" + registerEnabled +
", ip='" + ip + '\'' +
", networkInterface='" + networkInterface + '\'' +
", port=" + port +
", secure=" + secure +
", accessKey='" + accessKey + '\'' +
", secretKey='" + secretKey + '\'' +
'}';
}
public void overrideFromEnv(Environment env){
if(StringUtils.isEmpty(this.getServerAddr())) {
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
+ '\'' + ", secretKey='" + secretKey + '\'' + '}';
}
public void overrideFromEnv(Environment env) {
if (StringUtils.isEmpty(this.getServerAddr())) {
this.setServerAddr(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
}
if(StringUtils.isEmpty(this.getNamespace())) {
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
if (StringUtils.isEmpty(this.getNamespace())) {
this.setNamespace(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
}
if(StringUtils.isEmpty(this.getAccessKey())) {
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
if (StringUtils.isEmpty(this.getAccessKey())) {
this.setAccessKey(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
}
if(StringUtils.isEmpty(this.getSecretKey())) {
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
if (StringUtils.isEmpty(this.getSecretKey())) {
this.setSecretKey(env
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
}
if(StringUtils.isEmpty(this.getLogName())) {
this.setLogName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
if (StringUtils.isEmpty(this.getLogName())) {
this.setLogName(
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
}
if(StringUtils.isEmpty(this.getClusterName())) {
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.clusterName-name:}"));
if (StringUtils.isEmpty(this.getClusterName())) {
this.setClusterName(env.resolvePlaceholders(
"${spring.cloud.nacos.discovery.clusterName-name:}"));
}
if(StringUtils.isEmpty(this.getEndpoint())) {
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
if (StringUtils.isEmpty(this.getEndpoint())) {
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")
public class NacosDiscoveryEndpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(NacosDiscoveryEndpoint.class);
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosDiscoveryEndpoint.class);
@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;
@ -56,14 +57,15 @@ public class NacosDiscoveryEndpoint {
result.put("NacosDiscoveryProperties", nacosDiscoveryProperties);
NamingService namingService = nacosRegistration.getNacosNamingService();
List<ServiceInfo> subscribe = Collections.emptyList() ;
List<ServiceInfo> subscribe = Collections.emptyList();
try{
try {
subscribe = namingService.getSubscribeServices();
} catch (Exception e){
}
catch (Exception e) {
LOGGER.error("get subscribe services from nacos fail,", e);
}
result.put("subscribe",subscribe);
result.put("subscribe", subscribe);
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.autoconfigure.condition.ConditionalOnClass;
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.Configuration;
/**
* @author xiaojing
*/
@ConditionalOnWebApplication
@Configuration
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@ConditionalOnClass(Endpoint.class)
public class NacosDiscoveryEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
@Bean
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
return new NacosDiscoveryEndpoint();
}

@ -37,7 +37,7 @@ public class NacosServer extends Server {
this.metaInfo = new MetaInfo() {
@Override
public String getAppName() {
return instance.getService().getName();
return instance.serviceName();
}
@Override

@ -36,7 +36,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private String serviceId;
public NacosServerList(){}
public NacosServerList() {
}
public NacosServerList(String serviceId) {
this.serviceId = serviceId;
@ -54,11 +55,14 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
private List<NacosServer> getServers() {
try {
List<Instance> instances = registration.getNacosNamingService().selectInstances(serviceId, true);
List<Instance> instances = registration.getNacosNamingService()
.getAllInstances(serviceId);
return instancesToServerList(instances);
}
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…
Cancel
Save