這個要求服務統一註冊到註冊中心,而後調用的時候就不須要經過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爲服務提供了自動的負載均衡,默認使用輪詢
的策略。