依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
服务注册:bootstrap.properties
spring.application.name=service-****
spring.cloud.nacos.discovery.server-addr=***.***.***.***:8848
spring.cloud.nacos.discovery.namespace=*******************-e9502a399712
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.config.server-addr=***.***.***.***:8848
spring.cloud.nacos.config.namespace=*******************-e9502a399712
spring.cloud.nacos.config.group=shop
spring.cloud.nacos.config.file-extension=yml
spring.cloud.nacos.config.refresh-enabled=true
?
服务调用:
依赖:
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--熔断器hystrix依赖,主要是用 @HystrixCommand -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Client:配置调用服务ProductFeignClient?
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
//调用的服务的名称,以及熔断后执行方法的类
//@FeignClient(name="service-vod",fallback = VodClientDefeat.class)
@FeignClient(name="service-business")
@Component
public interface ProductFeignClient {
/**
* 通过spuId需要搜索的商品
* @param spuId spuid
* @return es保存的商品信息
*/
@GetMapping(value = "/brand/selectAll")
public Map loadEsProductBO(@RequestParam("spuId") Long spuId);
}
调用示例:
@Autowired
private ProductFeignClient productFeignClient ;
@GetMapping(value = "test")
@ApiOperation(value = "测试调用nacos服务")
public void testnacos() throws Exception{
Long spuId=123456L;
Map sss=ProductFeignClient.loadEsProductBO(spuId);
System.out.println(sss);
}
|