spring cloud 學習筆記(1)
阿新 • • 發佈:2019-04-11
star encoding for http ble ppi oot lease 技術分享
SpringCloud + Eureka / Nacos
git:https://github.com/huanmsf/springCloudLearn.git
項目目錄:
父pom:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>spring.cloud.learn</groupId> <artifactId>sc-learn</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>api</module> <module>consumer-eureka</module> <module>consumer-nacos</module> <module>provider-eureka</module> <module>provider-nacos</module> <module>eureka-server</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <spring-boot.version>2.0.4.RELEASE</spring-boot.version> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.1.1.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
基於Eureka的服務註冊和發現 provider-eureka
目錄
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>spring.cloud.learn</groupId> <artifactId>sc-learn</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>sc.learn.provider.eureka</groupId> <artifactId>provider-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>provider-eureka</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>spring.cloud.learn.api</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>spring.cloud.learn.api</groupId> <artifactId>api</artifactId> <version>2.1.4.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
application.yml
server:
port: 7070
spring:
application:
name: provider-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ProviderEurekaApplication
@SpringBootApplication @EnableEurekaClient public class ProviderEurekaApplication { public static void main(String[] args) { SpringApplication.run(ProviderEurekaApplication.class, args); } }
EchoServiceController
@RestController
@RequestMapping("/pe")
public class EchoServiceController {
@Autowired
private EchoService echoService;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return echoService.echo(name);
}
}
結果
基於Eureka的服務註冊和發現 consumer-eureka
目錄
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.consumer.eureka</groupId>
<artifactId>consumer-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-eureka</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>spring.cloud.learn.api</groupId>
<artifactId>api</artifactId>
<version>2.1.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 7000
spring:
application:
name: consumer-eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
ConsumerEurekaApplication
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class ConsumerEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerEurekaApplication.class, args);
}
}
InvokeEchoServiceController
@RestController
@RequestMapping("/ce")
public class InvokeEchoServiceController {
@Autowired
private HelloController helloController;
@GetMapping("/echo")
public String echo(@RequestParam String name) {
return helloController.hello(name);
}
}
HelloController
@FeignClient(value = "provider-eureka")
public interface HelloController {
@GetMapping("/pe/echo")
String hello(@RequestParam(value = "name") String name);
}
結果
基於Nacos的服務註冊和發現和配置中心 provider-nacos
目錄
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>spring.cloud.learn</groupId>
<artifactId>sc-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>sc.learn.provider.nacos</groupId>
<artifactId>provider-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-nacos</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
</project>
bootstrap.properties
spring.application.name=provider-nacos
server.port=9090
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
application.properties 空著,配置中心只能寫在bootstrap.properties中,不然報錯
ProviderNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderNacosApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderNacosApplication.class, args);
}
}
EchoController
@RestController
@RequestMapping("/pn")
@RefreshScope
public class EchoController {
@Value("${sys.user.name:null}")
private String user;
@GetMapping(value = "/hello")
public String hello(@RequestParam String name) {
return user + "Hello : " + name;
}
@GetMapping(value = "/echo")
public String echo(@RequestParam String name) {
return user + "Echo : " + name;
}
}
結果
基於Nacos的服務註冊和發現和配置中心 consumer-nacos
目錄
bootstrap.properties
server.port=9000
spring.application.name=consumer-nacos
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
ConsumerNacosApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerNacosApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerNacosApplication.class, args);
}
}
EchoControllerCn
@RestController
@RequestMapping("/cn")
@RefreshScope
public class EchoControllerCn {
private final RestTemplate restTemplate;
@Value("${sys.user.name:null}")
private String user;
@Autowired
public EchoControllerCn(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping(value = "/hello")
public String echo(@RequestParam String name) {
return restTemplate.getForObject("http://nacos-provider/pn/hello?name=" + name, String.class);
}
@Autowired
private HelloController2 helloController2;
@Autowired
private HelloController helloController1;
@GetMapping(value = "/hello1")
public String echo1(String name) {
return user + helloController1.hello(name);
}
@GetMapping(value = "/hello2")
public String echo2(String name) {
return user + helloController2.hello(name);
}
}
HelloController
@FeignClient(value = "provider-nacos")
public interface HelloController {
@GetMapping("/pn/hello")
String hello(@RequestParam(value = "name") String name);
}
結果
Nacos 服務器
spring cloud 學習筆記(1)