Spring Cloud Alibaba -Nocas快速上手

一、Nocas

nacos(naming Configuration service)是阿里巴巴开源的一个注册中心和配置中

1.1、nacos安装和启动

1、下载
2、解压
    cd /usr/upload
    tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local
3、启动和关闭
    启动:
        cd /usr/local/nacos/bin
        ./startup.sh -m standalone
    关闭:
        cd /usr/local/nacos/bin
        ./shutdown.sh

1.2、服务注册应用

1.2.1、创建父工程springcloud_parent

pox.xml

<dependencyManagement>
    <dependencies>
        <!--Spring Boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
         <!--spring cloud Netflix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud 阿里巴巴-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

1.2.2、创建子工程springcloud_common

创建一个实体bean

public class User {
private Integer id;
private String name;
private Integer age;
public User() {
}

public User(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}
}

1.2.3、服务提供者nacos_provider

1.2.3.1、pox.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.bjpowernode</groupId>
        <artifactId>springcloud_common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--nacos客户端-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

1.2.3.2、application.yml ,向nacos注册服务l

server:
  port: 9090
spring:
  cloud:
    nacos:
      server-addr: 192.168.10.135:8848 #nacos服务的地址
  application:
    name: nacos-provider #向注册中心注册的名字

1.2.3.3、创建启动类

package com.bjpoernode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class);
    }
}

1.2.3.4、创建service层

package com.bjpoernode.service;

import com.bjpowernode.pojo.User;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
    @Override
    public User getUserById(Integer id) {
        return new User(id,"王粪堆",18);
    }
}

1.2.3.5、创建controller层

package com.bjpoernode.controller;

import com.bjpoernode.service.UserService;
import com.bjpowernode.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/provider")
public class ProviderController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        return userService.getUserById(id);
    }
}

1.2.4、服务消费者nacos_consumer

1.2.4.1、pox.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.bjpowernode</groupId>
        <artifactId>springcloud_common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--nacos客户端-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

1.2.4.2、application.yml ,向nacos注册服务l

server:
  port: 8080
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.10.135:8848 #nacos服务的地址
  application:
    name: nacos-consumer #向注册中心注册的名字

1.2.4.3、创建启动类

@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class ConsumerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class);
    }
}

1.2.4.4、创建config

package com.bjpowernode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

1.2.4.5、创建controller层

package com.bjpowernode.controller;

import com.bjpowernode.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        //获取nacos中注册的所有服务信息
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println(service);
        }
        //获取nacos中注册的指定服务信息
        ServiceInstance instance = discoveryClient.getInstances("nacos-provider").get(0);
        //调用服务
        String url="http://"+ instance.getHost() +":"+ instance.getPort() +"provider/getUserById/"+id;
        return restTemplate.getForObject(url,User.class);
    }
}

1.3、测试

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值