码迷,mamicode.com
首页 > 其他好文 > 详细

nacos入门

时间:2020-05-02 14:45:16      阅读:45      评论:0      收藏:0      [点我收藏+]

标签:wired   group   ice   空间   throw   rgs   manage   turn   autowired   

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>
       <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

application.yml

spring:
  profiles:
    active: default
  application:
    name: user-center
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        # namespace 值为nacos上配置的命名空间ID
        # namespace: b7c0175d-f64c-45ab-8bb9-0981b4aceae9
        # 集群
        # cluster-name: beijing
        # 元数据配置
        metadata:
          hello: hi

简单使用

@Slf4j
@RestController
@RequestMapping("/share")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareController {
    private final ShareService shareService;

    private final RestTemplate restTemplate;

    private final DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
    public ShareDto findById(@PathVariable Integer id){
        Share share = shareService.findById(id);
        List<ServiceInstance> instances = discoveryClient.getInstances("user-center");
        String url = instances.stream()
                .map(instance->instance.getUri()+"/user/{id}")
                .findFirst()
                .orElseThrow(()->new IllegalArgumentException("当前没有实例!"));
        UserDto userDto = restTemplate.getForObject(
                url,
                UserDto.class,
                share.getId());
        log.info("请求的目标地址{}",url);
        ShareDto shareDto = new ShareDto();
        BeanUtils.copyProperties(share,shareDto);
        shareDto.setWxNickname(userDto.getWxNickname());
        return shareDto;
    }
}

nacos入门

标签:wired   group   ice   空间   throw   rgs   manage   turn   autowired   

原文地址:https://www.cnblogs.com/fly-book/p/12818017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!