这个要求服务统一注册到注册中心,而后调用的时候就不须要经过ip来调用,直接经过服务名便可。java
pom.xml配置,须要spring-cloud-starter-alibaba-nacos-discovery
依赖web
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
指定一下EnableDiscoveryClient
注解spring
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class,args); } }
application.yaml
配置以下,这里须要指定spring.application.nameapi
spring: application: name: lou-nacos-service-provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848 server: port: 8080
写个controller,用于测试app
@RestController public class TestController { @GetMapping("hello/{name}") public String sayHello(@PathVariable(value = "name") String name) { return "hello " + name; } }
启动application,能够在nacos后台的服务列表里面看到注册的服务。负载均衡
pom.xml配置和provider同样。ide
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
指定一下@EnableDiscoveryClient注解spring-boot
@SpringBootApplication @EnableDiscoveryClient public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
application.yaml配置和provider的基本同样,除了spring.application.name测试
spring: application: name: lou-nacos-service-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 server: port: 8070
consumer去调用provider,这里用基于HttpClient的RestTemplate,因此须要先定义个RestTemplate Bean。须要指定@LoadBalanced
。this
@Configuration public class ConsumerConfiguration { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
写controller。方法没有写具体ip,而是用了服务名lou-nacos-service-provider
来访问。
@RestController public class TestController { private final RestTemplate restTemplate; @Autowired public TestController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping("hello/{name}") public String hello(@PathVariable("name") String name) { return restTemplate.getForObject("http://lou-nacos-service-provider/hello/" + name, String.class); } }
启动,同理,能够在服务列表看到注册进去的服务。
测试。经过调用consumer的rest api接口就能获取到数据。
pom里面添加spring-cloud-starter-openfeign
依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
添加注解EnableFeignClients
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients//启用 public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
定义接口UserService
@FeignClient(name = "lou-nacos-service-provider")//服务名。 public interface UserService { @GetMapping("/hello/{name}")//这里表示请求lou-nacos-service-provider的/hello/{name}地址 String hello(@PathVariable(value = "name") String name); }
不须要实现。
经过定义的UserService接口调用服务,仍是原先的TestController
@RestController public class TestController { @Autowired//启用了FeignClient,因此能够Autowired private UserService userService; @GetMapping("hello2/{name}") public String hello2(@PathVariable("name") String name) { return userService.hello(name);//直接调用了方法。 } }
测试一下。访问hello2/feign,成功。
nacos为服务提供了自动的负载均衡,默认使用轮询
的策略。