4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
com.funtl
hello-spring-cloud-alibaba-dependencies
1.0.0-SNAPSHOT
pom
hello-spring-cloud-alibaba-dependencies
http://www.funtl.com
2018-Now
1.8
UTF-8
UTF-8
Finchley.SR2
0.2.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
true
org.apache.maven.plugins
maven-jar-plugin
false
true
true
true
org.apache.maven.plugins
maven-resources-plugin
org.apache.maven.plugins
maven-install-plugin
org.apache.maven.plugins
maven-clean-plugin
org.apache.maven.plugins
maven-antrun-plugin
org.apache.maven.plugins
maven-dependency-plugin
org.apache.maven.plugins
maven-javadoc-plugin
prepare-package
jar
net.alchim31.maven
yuicompressor-maven-plugin
1.5.1
prepare-package
compress
UTF-8
false
true
30000
true
**/*.js
**/*.css
**/*.min.js
**/*.min.css
src/main/java
**/*.java
src/main/resources
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
sonatype-repos
Sonatype Repository
https://oss.sonatype.org/content/groups/public
true
false
sonatype-repos-s
Sonatype Repository
https://oss.sonatype.org/content/repositories/snapshots
false
true
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
parent:继承了 Spring Boot 的 Parent,表示我们是一个 Spring Boot 工程
package:pom,表示该项目仅当做依赖项目,没有具体的实现代码
spring-cloud-alibaba-dependencies:在 properties 配置中预定义了版本号为 0.2.1.RELEASE ,表示我们的 Spring Cloud Alibaba 对应的是 Spring Cloud Finchley 版本
build:配置了项目所需的各种插件
repositories:配置项目下载依赖时的第三方库
# 下载源码
git clone https://github.com/alibaba/nacos.git
# 安装到本地仓库
cd nacos/
mvn -Prelease-nacos clean install -U
cd distribution/target/nacos-server-0.7.0/nacos/bin
# Linux
./startup.sh -m standalone
# Windows
startup.cmd
创建一个工程名为 hello-spring-cloud-alibaba-nacos-provider 的服务提供者项目,pom.xml 配置如下:
4.0.0
com.funtl
hello-spring-cloud-alibaba-dependencies
1.0.0-SNAPSHOT
../hello-spring-cloud-alibaba-dependencies/pom.xml
hello-spring-cloud-alibaba-nacos-provider
jar
hello-spring-cloud-alibaba-nacos-provider
http://www.funtl.com
2018-Now
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
com.funtl.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication
通过 @EnableDiscoveryClient 注解表明是一个 Nacos 客户端,该注解是 Spring Cloud 提供的原生注解
package com.funtl.hello.spring.cloud.alibaba.nacos.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{message}")
public String echo(@PathVariable String message) {
return "Hello Nacos Discovery " + message;
}
}
}
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 8081
management:
endpoints:
web:
exposure:
include: "*"
创建一个工程名为 hello-spring-cloud-alibaba-nacos-consumer 的服务消费者项目,pom.xml 配置如下:
4.0.0
com.funtl
hello-spring-cloud-alibaba-dependencies
1.0.0-SNAPSHOT
../hello-spring-cloud-alibaba-dependencies/pom.xml
hello-spring-cloud-alibaba-nacos-consumer
jar
hello-spring-cloud-alibaba-nacos-consumer
http://www.funtl.com
2018-Now
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
com.funtl.hello.spring.cloud.alibaba.nacos.consumer.NacosConsumerApplication
package com.funtl.hello.spring.cloud.alibaba.nacos.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
创建一个名为 NacosConsumerConfiguration 的 Java 配置类,主要作用是为了注入 RestTemplate
package com.funtl.hello.spring.cloud.alibaba.nacos.consumer.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class NacosConsumerConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
创建一个名为 NacosConsumerController 测试用的 Controller
package com.funtl.hello.spring.cloud.alibaba.nacos.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class NacosConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping(value = "/echo/app/name")
public String echo() {
//使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
return restTemplate.getForObject(url, String.class);
}
}
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 9091
management:
endpoints:
web:
exposure:
include: "*"