spring cloud nacos demo
原创
文章标签 spring java 后端 maven xml 文章分类 Hadoop 大数据
©著作权归作者所有:来自51CTO博客作者武汉淘淘的原创作品,请联系作者获取转载授权,否则将追究法律责任
参考
https://github.com/alibaba/spring-cloud-alibaba
主要maven
<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">
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-spring-cloud-discovery-exampleartifactId>
<packaging>pompackaging>
<version>0.2.0-SNAPSHOTversion>
<modules>
<module>nacos-spring-cloud-provider-examplemodule>
<module>nacos-spring-cloud-consumer-examplemodule>
<module>nacos-spring-cloud-config-examplemodule>
modules>
<properties>
<spring-boot.version>2.0.4.RELEASEspring-boot.version>
<spring-cloud.version>Finchley.SR4spring-cloud.version>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.0.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.0.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
nacos-spring-cloud-config-example
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">
<parent>
<artifactId>nacos-spring-cloud-discovery-exampleartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-cloud-config-exampleartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-contextartifactId>
dependency>
dependencies>
project>
ConfigController
package com.alibaba.nacos.example.spring.cloud.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;("/config")public class ConfigController { ("${useLocalCache:false}") private boolean useLocalCache; ("/get") public boolean get() { return useLocalCache; }}
NacosConfigApplication
package com.alibaba.nacos.example.spring.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; public class NacosConfigApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); }}
nacos-spring-cloud-consumer-example
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-spring-cloud-discovery-exampleartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-cloud-consumer-exampleartifactId>
<properties>
<spring-cloud-openfeign.version>2.0.0.RELEASEspring-cloud-openfeign.version>
<spring-cloud-netflix.version>2.0.0.RELEASEspring-cloud-netflix.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
yml
server.port=8080spring.application.name=service-consumerspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
NacosConsumerApplication
package com.alibaba.nacos.example.spring.cloud;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate; public class NacosConsumerApplication { public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } public class TestController { private final RestTemplate restTemplate; public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;} (value = "/echo/{str}", method = RequestMethod.GET) public String echo( String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } }}
nacos-spring-cloud-provider-example
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">
<parent>
<artifactId>nacos-spring-cloud-discovery-exampleartifactId>
<groupId>com.alibaba.nacosgroupId>
<version>0.2.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>nacos-spring-cloud-provider-exampleartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-validatorartifactId>
<version>5.1.0.Finalversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
yml
server.port=8070spring.application.name=service-providerspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
NacosProviderApplication
package com.alibaba.nacos.example.spring.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController; public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } class EchoController { (value = "/echo/{string}", method = RequestMethod.GET) public String echo( String string) { return "Hello Nacos Discovery " + string; } }}
- 赞
- 收藏
- 评论
- *举报
上一篇:单例模式 Java版
下一篇:算法博客模板
Original: https://blog.51cto.com/u_15704977/5444586
Author: 武汉淘淘
Title: spring cloud nacos demo
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/516734/
转载文章受原作者版权保护。转载请注明原作者出处!