Spring Cloud Alibaba 注册一个服务-HelloWorld(三)

 
“ 本章节我们写一个spring cloud alibaba 注册服务的一个HelloWorld, 大家可以了解一下基本的provider写法。”
 

 

 
 
01
 
环境检查
 
 
上一章介绍了怎么搭建Spring cloud alibaba运行环境。客官需要确认一下环境是否已经启动。
 
 
如果没有启动的话,用命令启动一下:
 
➜  docker docker-compose up -d nacos
mysql is up-to-date
Creating nacos ... done
➜  docker docker-compose up -d sentinel
Creating sentinel … done
 

 

 
如果新客官还没有安装环境的话,去看一下公众号上一章内容 “Docker composer搭建Spring Cloud Alibaba 运行环境(二)”
 
 
02
SpringBoot Provider搭建
 
 
废话不多少说,先上项目结构,是一个基本的springboot 项目结构:
暂时只需要看一下springcloud-provider项目结构,common和consumer会在之后的章节中讲到。熟悉JAVA项目的同学应该会比较熟悉,就不再赘述了。
先是pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.laobaozi</groupId>
    <artifactId>springcloud-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-provider</name>
    <description>Demo project for alibaba springcloud</description>
 
    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
        <springcloud-alibaba.version>0.9.0.RELEASE</springcloud-alibaba.version>
        <mybatis-plus.version>3.1.1</mybatis-plus.version>
        <mysql.version>5.1.47</mysql.version>
        <encoding>UTF-8</encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
 
            <!--database-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
 
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
 
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${springcloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
 
        </dependencies>
    </dependencyManagement>
 
    <dependencies>
        <!--Spring Boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
 
        <!--Spring Cloud Alibaba-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
       <!--Spring Cloud Alibaba end-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

 

 
依赖非常清晰,除springboot和数据库外,多了两个和spring cloud alibaba相关的依赖,已经做了标识。
 
其次:Application.yml
server:
  port: 8011
spring:
  application:
    name: springcloud-provicer-demo
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848/

 

这里需要注意一下,有的时候因为docker配置的问题没办法用127.0.0.1访问,这个时候可以改成局域网IP。
启动类:SpringcloudProviderApplication.java
package com.laobaozi.springcloud.provider;
 
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudProviderApplication {
 
 
 
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudProviderApplication.class, args);
    }
 
}

 

 
Controller:
package com.laobaozi.springcloud.provider.controller;
 
import com.laobaozi.springcloud.provider.model.UserDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
@RestController
public class UserController {
 
 
    @RequestMapping("/user/list")
    public List<UserDto> listUsers() {
        List users = new ArrayList();
        UserDto userDto = new UserDto();
        userDto.setAge(20);
        userDto.setName("张三");
        users.add(userDto);
        return users;
    }
 
 
    @RequestMapping("/user/getUserName")
    public String listUsersName() {
        return "this is jim";
    }
 
 
}

  

 
Fegin Service:
package com.laobaozi.springcloud.provider.service;
 
 
import com.laobaozi.springcloud.provider.model.UserDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
 
@FeignClient(name = "user-service")
public interface UserFeginService {
 
    @PostMapping("/fegin/user/list")
    UserDto listUser();
 
}

  

 
Dto:
public class UserDto {
 
    private String name;
 
    private int age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}

  

 
启动项目:
显示8011端口项目启动成功。
 
试着访问一下定义的两个接口:
list user:
Get user:
03
检查Nacos注册器
 
在Nacos系统中的服务列表就可以看到我们的provider demo了。
 
至此服务都注册进了注册中心Nacos并且都能对外提供基本的用户列表查询和单用户查询能力,本期的springcloud alibaba服务注册就到这里。
下期我们来讲怎么调用Nacos注册的服务,即消费端。
 
 
 
posted @ 2021-04-09 18:10  老包子说架构  阅读(205)  评论(0编辑  收藏  举报