一、文档学习
spring 官网对 cloud alibaba 的介绍及配置
Nacos config 是监听配置信息(yml)发生变化的
Nacos discovery 是注册发现服务的
二、实战
下载安装Nacos我就不说了
安装好访问页面
先搭建基本的服务结构
demo-cloud 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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-cloud</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>demo-order-server</module>
<module>demo-user-server</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Nacos 注册发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Nacos 配置监听-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
demo-order-server.pom 和 demo-user-server.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>
<parent>
<groupId>com.example</groupId>
<artifactId>demo-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo-order-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
demo-order-server.yml
server:
port: 8080
spring:
application:
name: order-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 值范围:1到100。值越大,重量越大。
weight: 1
# 集群名称
cluster-name: order
demo-user-server.yml
server:
port: 8081
spring:
application:
name: user-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 值范围:1到100。值越大,重量越大。
weight: 1
# 集群名称
cluster-name: user
demo-user-server Application
package com.example.userserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoUserServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoUserServerApplication.class, args);
}
/**
* 手动创建一个RestTemplate的配置:
**/
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
demo-order-server Application
package com.example.orderserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoOrderServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoOrderServerApplication.class, args);
}
}
demo-order-server OrderController
package com.example.orderserver.controller;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author big uncle
* @Date 2020/3/20 11:33
* @module HYQ_APP
**/
@RestController
@RequestMapping("order")
public class OrderController {
Map<String,List<String>> map = new HashMap<String,List<String>>(10){{
put("1",Arrays.asList("userId:1 orderId:1","userId:1 orderId:2","userId:1 orderId:3","userId:1 orderId:4"));
put("2",Arrays.asList("userId:2 orderId:10","userId:2 orderId:12","userId:2 orderId:13","userId:2 orderId:14"));
put("3",Arrays.asList("userId:3 orderId:20","userId:3 orderId:22","userId:3 orderId:23","userId:3 orderId:24"));
}};
@GetMapping("/getOrder/{userId}")
public List<String> getOrder(@PathVariable String userId){
return map.get(userId);
}
}
demo-user-server OrderController
package com.example.userserver.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* 所属Y-API模块
* 模块描述
*
* @Author big uncle
* @Date 2020/3/20 11:24
* @module HYQ_APP
**/
@RestController
@RequestMapping("user")
public class UserController {
private RestTemplate restTemplate;
@Autowired
public UserController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/getMyOrder")
public List<String> getMyOrder(@RequestParam("userId") String userId){
List<String> list = restTemplate.getForObject("http://order-server/order/getOrder/" + userId, List.class);
return list;
}
}
网友评论