微服务解决方案 -- Spring Cloud Alibaba (二)服务提供者

不了解此套教程的服务注册与发现可以移步第一章节微服务解决方案 – Spring Cloud Alibaba (一)服务的注册与发现

通用依赖


根据Nacos基本架构图中我们有一个Provider APP,他就是我们的服务提供者。我们首先创建一个通用的依赖dependencies
创建spring-cloud-alibaba-dependencies文件夹,并且创建pom.xml


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.6.RELEASEversion>
    parent>

    <groupId>com.laoshirengroupId>
    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    <version>1.0.0-SNAPSHOTversion>
    <packaging>pompackaging>

    <description>spring cloud alibaba 统一的项目依赖description>

    <properties>
        
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

        
        <spring-cloud.version>Finchley.SR2spring-cloud.version>
        <spring-cloud-alibaba.version>0.2.1.RELEASEspring-cloud-alibaba.version>
    properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-cloud-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <showWarnings>trueshowWarnings>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>falseaddMavenDescriptor>
                    archive>
                configuration>
                <executions>
                    <execution>
                        <configuration>
                            <archive>
                                <manifest>
                                    
                                    <addDefaultImplementationEntries>trueaddDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>trueaddDefaultSpecificationEntries>
                                    <addClasspath>trueaddClasspath>
                                manifest>
                            archive>
                        configuration>
                    execution>
                executions>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-install-pluginartifactId>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-clean-pluginartifactId>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-antrun-pluginartifactId>
            plugin>

            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
            plugin>
        plugins>

        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-javadoc-pluginartifactId>
                    <executions>
                        <execution>
                            <phase>prepare-packagephase>
                            <goals>
                                <goal>jargoal>
                            goals>
                        execution>
                    executions>
                plugin>

                
                <plugin>
                    <groupId>net.alchim31.mavengroupId>
                    <artifactId>yuicompressor-maven-pluginartifactId>
                    <version>1.5.1version>
                    <executions>
                        <execution>
                            <phase>prepare-packagephase>
                            <goals>
                                <goal>compressgoal>
                            goals>
                        execution>
                    executions>
                    <configuration>
                        <encoding>UTF-8encoding>
                        <jswarn>falsejswarn>
                        <nosuffix>truenosuffix>
                        <linebreakpos>30000linebreakpos>
                        <force>trueforce>
                        <includes>
                            <include>**/*.jsinclude>
                            <include>**/*.cssinclude>
                        includes>
                        <excludes>
                            <exclude>**/*.min.jsexclude>
                            <exclude>**/*.min.cssexclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
        pluginManagement>

        
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <excludes>
                    <exclude>**/*.javaexclude>
                excludes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
        resources>
    build>

    <repositories>
        <repository>
            <id>aliyun-reposid>
            <name>Aliyun Repositoryname>
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>

        <repository>
            <id>sonatype-reposid>
            <name>Sonatype Repositoryname>
            <url>https://oss.sonatype.org/content/groups/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
        <repository>
            <id>sonatype-repos-sid>
            <name>Sonatype Repositoryname>
            <url>https://oss.sonatype.org/content/repositories/snapshotsurl>
            <releases>
                <enabled>falseenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>

        <repository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/snapshoturl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-reposid>
            <name>Aliyun Repositoryname>
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>
project>

让其被IDEA托管,我们所有的项目都将直接或间接继承这个dependencies项目

服务提供者


创建spring-cloud-alibaba-nacos-provider文件夹,并且创建pom.xml


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>com.laoshirengroupId>
        <artifactId>spring-cloud-alibaba-dependenciesartifactId>
        <version>1.0.0-SNAPSHOTversion>
        <relativePath>../spring-cloud-alibaba-dependencies/pom.xmlrelativePath>
    parent>

    <artifactId>spring-cloud-alibaba-nacos-providerartifactId>
    <packaging>jarpackaging>

    <name>spring-cloud-alibaba-nacos-providername>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <mainClass>com.laoshiren.alibaba.nacos.provider.NacosProviderApplicationmainClass>
                configuration>
            plugin>
        plugins>
    build>
project>

application.yml

spring:
  application:
    ## 应用程序名必须要有,nacos 有naming server
    name: nacos-provider
  cloud:
    ## nacos 配置
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

server:
  port: 8500

## 端点检查
management:
  endpoints:
    web:
      exposure:
        include: "*"

Application.java

package com.laoshiren.alibaba.nacos.provider;

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

/**
 * ProjectName:     spring-cloud-alibaba
 * Package:         com.laoshiren.alibaba.nacos.provider
 * ClassName:       NacosProviderApplication
 * Author:          laoshiren
 * Date:            2019/12/23 13:15
 * Version:         1.0.0
 * Description:     服务提供者 启动类
 */
@SpringBootApplication
// 注解表明是一个 Nacos 客户端,该注解是 Spring Cloud 提供的原生注解
@EnableDiscoveryClient
public class NacosProviderApplication {

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

最后随便写一个hello worldcontroller

@RestController
public class HelloController {

    @GetMapping(value = "/hello/{message}")
    public String hello(@PathVariable String message) {
        return "Hello Nacos your message is: " + message;
    }
}

启动程序
访问http://localhost:8500/hello/world,页面上打印内容正确
访问 http://localhost:8848/nacos,即Nacos Server网址,可以发发现一个服务已经注册在服务中了,服务名为 nacos-provider,就是yml配置的application name
微服务解决方案 -- Spring Cloud Alibaba (二)服务提供者_第1张图片

你可能感兴趣的:(微服务解决方案,java)