Spring Cloud Alibaba提供者及消费者

Nacos简介

1.官方文档介绍

Nacos is committed to help you discover, configure, and manage your microservices. It provides a set of simple and useful features enabling you to realize dynamic service discovery, service configuration, service metadata and traffic management.
Nacos makes it easier and faster to construct, deliver and manage your microservices platform. It is the infrastructure that supports a service-centered modern application architecture with a microservices or cloud-native approach.

2.翻译过来

Nacos致力于帮助您发现,配置和管理微服务。它提供了一组简单有用的功能,使您能够实现动态服务发现,服务配置,服务元数据和流量管理。
Nacos使构建,交付和管理您的微服务平台变得更容易,更快捷。它是通过微服务或云原生方法支持以服务为中心的现代应用程序体系结构的基础架构。

3.GitHub下载地址

Nacos下载

4.百度云盘下载

Nacos下载
提取码: j4b1

5.docker下载

Nacos启动页面

访问nacos地址 : http://localhost:8848/nacos/
user:nacos
passwd:nacos

Spring Cloud Alibaba提供者及消费者_第1张图片

创建项目

1.创建结构如下

Spring Cloud Alibaba提供者及消费者_第2张图片

1.1Dependencies中pom.xml


    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
    

    com.liugui
    hello-spring-cloud-alibaba-dependencies
    1.0.0-SNAPSHOT
    pom

    hello-spring-cloud-alibaba-dependencies
    
        
        1.8
        UTF-8
        UTF-8

        
        Finchley.SR2
        0.2.1.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    true
                
            

            
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    
                        false
                    
                
                
                    
                        
                            
                                
                                    
                                    true
                                    true
                                    true
                                
                            
                        
                    
                
            

            
            
                org.apache.maven.plugins
                maven-resources-plugin
            

            
            
                org.apache.maven.plugins
                maven-install-plugin
            

            
            
                org.apache.maven.plugins
                maven-clean-plugin
            

            
            
                org.apache.maven.plugins
                maven-antrun-plugin
            

            
            
                org.apache.maven.plugins
                maven-dependency-plugin
            
        

        
            
                
                
                    org.apache.maven.plugins
                    maven-javadoc-plugin
                    
                        
                            prepare-package
                            
                                jar
                            
                        
                    
                

                
                
                    net.alchim31.maven
                    yuicompressor-maven-plugin
                    1.5.1
                    
                        
                            prepare-package
                            
                                compress
                            
                        
                    
                    
                        UTF-8
                        false
                        true
                        30000
                        true
                        
                            **/*.js
                            **/*.css
                        
                        
                            **/*.min.js
                            **/*.min.css
                        
                    
                
            
        

        
        
            
                src/main/java
                
                    **/*.java
                
            
            
                src/main/resources
            
        
    

    
        
            aliyun-repos
            Aliyun Repository
            http://maven.aliyun.com/nexus/content/groups/public
            
                true
            
            
                false
            
        

        
            sonatype-repos
            Sonatype Repository
            https://oss.sonatype.org/content/groups/public
            
                true
            
            
                false
            
        
        
            sonatype-repos-s
            Sonatype Repository
            https://oss.sonatype.org/content/repositories/snapshots
            
                false
            
            
                true
            
        

        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

    
        
            aliyun-repos
            Aliyun Repository
            http://maven.aliyun.com/nexus/content/groups/public
            
                true
            
            
                false
            
        
    

2.创建提供者

需要注意的是在parent中需要指明上一级

2.1Provider中pom.xml


    4.0.0

    
        com.liugui
        hello-spring-cloud-alibaba-dependencies
        1.0.0-SNAPSHOT
        ../pom.xml
    

    hello-spring-cloud-alibaba-nacos-provider
    jar

    hello-spring-cloud-alibaba-nacos-provider
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            
            4.10
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.junit.jupiter
            junit-jupiter-api
            RELEASE
            test
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.liugui.hello.spring.cloud.alibaba.nacos.provider.ProviderApplication
                
            
        
    

2.2Provider中ProviderApplication

@EnableDiscoveryClient和@EnableEurekaClient共同点就是:都是能够让注册中心能够发现,扫描到改服务。
不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。

package com.liugui.hello.spring.cloud.alibaba.nacos.provider;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {

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

}
2.3Provider中Controller
@RestController
public class ProviderController {

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/echo/{message}")
    public String echo(@PathVariable String message){
        return "Hello Nacos Discovery" + message + " port is" + port ;
    }
}
2.4Provider中Application.properties
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=192.168.199.221:8848
server.port=8082

3.创建消费者

3.1Consumer中pom.xml


    4.0.0
    
        com.liugui
        hello-spring-cloud-alibaba-dependencies
        1.0.0-SNAPSHOT
       ../pom.xml 
    
    com.liugui
    hello-spring-cloud-alibaba-nacos-consumer
    0.0.1-SNAPSHOT
    hello-spring-cloud-alibaba-nacos-consumer
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.junit.jupiter
            junit-jupiter-api
            RELEASE
            test
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.liugui.hello.spring.cloud.alibaba.nacos.consumer.ConsumerApplication
                
            
        
    



3.2Consumer中Configuration
@Configuration
public class NacosConsumerConfiguration {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
3.3Consumer中ConsumerController
@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${spring.application.name}")
    private String appName;

    @GetMapping(value = "/echo/best/name")
    public String echo(){
        ServiceInstance instance = loadBalancerClient.choose("nacos-provider");
        String url = instance.getUri() + "/echo/" + appName;
        System.out.println(url+"%%%%%%%%%");
        String forObject = restTemplate.getForObject(url, String.class);
        return  forObject;
    }
}
3.4Consumer中ConsumerApplication
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
3.5Consumer中Application.properties
spring.application.name=nacos-consumer
spring.cloud.nacos.discovery.server-addr=192.168.199.221:8848
server.port= 9091

你可能感兴趣的:(java)