首先,引入概念分佈式微服務.一般應用都是單體架構.所有的內容都是在一個項目模塊,部署在一個服務器上,假如用戶訪問量大了,服務器壓力大了怎麼辦? 你可以使用集羣,將同樣的項目COPY部署到多臺服務器上,負載均衡(比如nginx).
然而,我們的應用模塊的服務器壓力使不一樣的,比如登陸功能,郵件功能等,有些使用量大,有些使用量小,所以引出了微服務分佈式概念. 我們通過將模塊拆分,設置權重部署到服務器,權重大的可以多部署幾臺服務器,那些不怎麼使用的模塊用一臺服務器.
Service Discovery
A dynamic directory that enables client side load balancing and smart routing
Circuit Breaker //斷路器
Microservice fault tolerance with a monitoring dashboard
Configuration Server //配置服務
Dynamic, centralized configuration management for your decentralized applications
API Gateway //網關
Single entry point for API consumers (e.g., browsers, devices, other APIs)
Distributed Tracing //分佈式追蹤
Automated application instrumentation and operational visibility for distributed systems
OAuth2 //支持單點登陸 token(一般使用session作爲token 讓用戶攜帶上訪問)
Support for single sign on, token relay and token exchange
Consumer-Driven Contracts //兩種協議的支持
Service evolution patterns to support both HTTP-based and message-based APIs
View All
Spring Cloud Reference Manual
Getting Started Guides
Config
Registry
Breakers
Load Balancing
Routing
等
簡單案例:
1.spring cloud服務註冊中心Eureka
IDEA創建一個父項目 maven,刪除掉src文件 因爲父項目作爲pom用不上
創建spring cloud 作爲父項目 刪除掉src文件
<?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>cn.how2j.springcloud</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<description>springcloud父項目內容</description>
<packaging>pom</packaging>
<!--引入-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<!--屬性配置-->
<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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--hutool工具-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
<!--統一管理項目的版本號-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
創建子項目 eureka-server,服務註冊中心Eureka>>>>new model
子項目 pom.xml ,增加 spring-cloud-starter-netflix-eureka-server jar 包
<?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>springcloud</artifactId>
<groupId>cn.how2j.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<!--eureka服務註冊中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
EurekaServerApplication> EurekaServer 啓動類。
這是一個 EurekaServer ,它扮演的角色是註冊中心,用於註冊各種微服務,以便於其他微服務找到和訪問。 所以 Eureka 這個單詞是 “找到啦” 的意思。
EurekaServer 本身就是個 Springboot 微服務, 所以它有 @SpringBootApplication 註解。
@EnableEurekaServer 表示這是個 EurekaServer 。
//可以修改爲正常的springboot啓動方式
package cn.how2j.springcloud;
import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@EnableEurekaServer //表示這是個微服務
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
//8761 這個端口是默認的,就不要修改了,後面的子項目,都會訪問這個端口。
int port = 8761;
if (!NetUtil.isUsableLocalPort(port)) {
System.err.printf("端口%d被佔用了,無法啓動%n", port);
System.exit(1);
}
new SpringApplicationBuilder(EurekaServerApplication.class).properties("server.port=" + port).run(args);
}
}
配置文件,resource下建application.yml
eureka:
instance:
hostname: localhost //主機地址 比如127.0.0.1
client:
registerWithEureka: false //服務中心本身也是一個微服務,這個表示不註冊進來
fetchRegistry: false
serviceUrl: //地址,這是http://127.0.0.1:8761/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server //微服務名稱
#修改服務註冊中心端口,默認8761
server:
port: 8761 //微服務的端口號
輸入http://127.0.0.1:8761/訪問註冊中心 查看註冊的服務等信息
還沒有微服務註冊進來,下一節註冊微服務
接下來註冊數據微服務:
創建子項目 product-data-service。
new module 來建一個數據微服務
pom文件添加依賴:
spring-cloud-starter-netflix-eureka-client 表示這是個 eureka 客戶端。
spring-boot-starter-web: 表示這是個web服務,會提供控制層
<?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>springcloud</artifactId>
<groupId>cn.how2j.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product-data-service。</artifactId>
<dependencies>
<!--spring-cloud-starter-netflix-eureka-client 表示這是個 eureka 客戶端。-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring-boot-starter-web: 表示這是個web服務,會提供控制層-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
配置pojo,server,controller層,省略部分(pojo和service)自己構造,不用數據庫
package cn.how2j.springcloud.controller;
import cn.how2j.springcloud.pojo.Product;
import cn.how2j.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Controller
public class ProductController {
@Autowired
ProductService productService;
@ResponseBody //返回json數據方便查看效果
@RequestMapping("/products")
public Object products() {
List<Product> ps = productService.listProducts();
return ps;
}
}
application.yml
在模塊下resources
#微服務名
spring:
application:
name: product-data-service //微服務名
#和服務中心配置一致 hostname,serviceUrl:一致
eureka:
instance:
hostname: localhost
client:
serviceUrl: //eureka服務中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
#自己的端口
server:
port: 8001
啓動類
package cn.how2j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@SpringBootApplication
@EnableEurekaClient //註冊微服務
public class ProductDataServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductDataServiceApplication.class,args);
}
}
啓動後再次訪問服務中心http://127.0.0.1:8761/ 發現註冊了一個微服務:
訪問微服務測試:http://127.0.0.1:8001//products
返回j’son
多來一份數據微服務 新建子項目maven copy一份改改
端口不同8002,微服務名稱和之前的一致 改個端口號
application.yml
#微服務名
spring:
application:
name: product-data-service
#和服務中心配置一致
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
#自己的端口
server:
port: 8002
pom.xml
修改一下
springcloud
cn.how2j.springcloud
1.0-SNAPSHOT
4.0.0
<artifactId>product-data-service3</artifactId> 這個修改否則子項目重名
<dependencies>
<!--spring-cloud-starter-netflix-eureka-client 表示這是個 eureka 客戶端。-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring-boot-starter-web: 表示這是個web服務,會提供控制層-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
啓動查看:http://127.0.0.1:8761/ 一個微服務啓動了兩個端口.
**視圖微服務:**接下來
Ribbon 概念
1.接下來,我們就要訪問前面註冊好的數據微服務了。 springcloud 提供了兩種方式,一種是 Ribbon,一種是 Feign。
Ribbon 是使用 restTemplate 進行調用,並進行客戶端負載均衡。 什麼是客戶端負載均衡呢? 在前面 註冊數據微服務 裏,註冊了8001和8002兩個微服務, Ribbon 會從註冊中心獲知這個信息,然後由 Ribbon 這個客戶端自己決定是調用哪個,這個就叫做客戶端負載均衡。
Feign 是什麼呢? Feign 是對 Ribbon的封裝,調用起來更簡單。。。
創建視圖微服務 new model…
創建子項目 product-view-service-ribbon
pom.xml
包含以下jar:
spring-cloud-starter-netflix-eureka-client: eureka 客戶端
spring-boot-starter-web: springmvc
spring-boot-starter-thymeleaf: thymeleaf 做服務端渲染
<?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>springcloud</artifactId>
<groupId>cn.how2j.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product-view-service-ribbon</artifactId>
<!--視圖依賴包括mvc thymeleaf eureka 客戶端-->
<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.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
接着創建和數據微服務一樣的結構
package how2j.springcloud.pojo;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
public class Product {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Product() {
}
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
Ribbon 客戶端,訪問數據微服務地址
Ribbon 客戶端, 通過 restTemplate 訪問 http://PRODUCT-DATA-SERVICE/products , 而 product-data-service 既不是域名也不是ip地址,而是 數據服務在 eureka 註冊中心的名稱。
注意看,這裏只是指定了要訪問的 微服務名稱,但是並沒有指定端口號到底是 8001, 還是 8002.
package how2j.springcloud.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Component
public class ProductClientRibbon {
//下面的對象需要自己加到容器裏請看config配置
@Autowired
RestTemplate restTemplate;
public List<Product> listProdcuts() {
//數據微服務對象訪問的/products
//相當於 http://127.0.0.1:8001/products (8001,8002其中一個)
//返回list
return restTemplate.getForObject("http://product-data-service/products",List.class);
}
服務類 service
返回list對象
package how2j.springcloud.service;
import how2j.springcloud.pojo.Product;
import how2j.springcloud.pojo.ProductClientRibbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Service
public class ProductService {
@Autowired
ProductClientRibbon productClientRibbon;
public List<Product> listProducts(){
// List<Product> ps = new ArrayList<>();
//ps.add(new Product(1,"product a from port:"+port, 50));
// ps.add(new Product(2,"product b from port:"+port, 150));
// ps.add(new Product(3,"product c from port:"+port, 250));
// return ps;
return productClientRibbon.listProdcuts();
}
}
注意:service 業務返回的數據調用了數據微服務
(相當於中介,視圖微服務先調用,再交給兩個數據微服務處理,service傳統是通過調用dao接口查詢數據庫,這裏是通過Ribbon 客戶端去數據微服務中調用)
控制器controller
package how2j.springcloud.controller;
import how2j.springcloud.pojo.Product;
import how2j.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public Object products(Model m) {
List<Product> ps = productService.listProducts();
m.addAttribute("ps", ps);
return "products";
}
}
products.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>products</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
table {
border-collapse:collapse;
width:400px;
margin:20px auto;
}
td,th{
border:1px solid gray;
}
</style>
</head>
<body>
<div class="workingArea">
<table>
<thead>
<tr>
<th>id</th>
<th>產品名稱</th>
<th>價格</th>
</tr>
</thead>
<tbody>
<tr th:each="p: ${ps}">
<td th:text="${p.id}"></td>
<td th:text="${p.name}"></td>
<td th:text="${p.price}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
啓動類:
package how2j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@SpringBootApplication
@EnableEurekaClient //註冊到服務中心
@EnableDiscoveryClient //啓用發現其他微服務
public class ProductViewServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ProductViewServiceRibbonApplication.class, args);
}
}
補充:config
如何使用Ribbon
上節示例中是使用RestTemplate進行Eureka Client(包括服務提供者以及服務消費者,在這裏其實是服務消費者使用RestTemplate)之間的通信,爲RestTemplate配置類添加@LoadBalanced註解即可
package how2j.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Configuration
public class MyConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
yml配置:
#微服務名,以及thymeleaf設置
spring:
application:
name: product-view-service-ribbon
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: html
servlet:
content-type: text/html
encoding: utf-8
mode: html5
#和服務中心配置一致
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
#自己的端口
server:
port: 8010
全部啓動測試:
1.http://127.0.0.1:8761/訪問服務註冊中心 eureka
2.查看數據微服務
http://127.0.0.1:8001//products和http://127.0.0.1:8002//products
3.訪問地址:(視圖微服務作爲中介)
http://127.0.0.1:8010/products
報錯的話建議查看微服務地址有沒有寫錯
結果報錯500,具體原因是數據微服務product-data-service 的/products請求返回的是json,而視圖微服務的是返回html
所以說 controller層需要返回一致的結果
修改controller或者一一去修改每個數據微服務
@ResponseBody
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public Object products(Model m) {
List<Product> ps = productService.listProducts();
m.addAttribute("ps", ps);
return ps.toString();
}
}
修改爲返回json後
交給了8001端口的服務
8002
這個是輪詢負載均衡機制,默認是輪着處理 我記得nginx可以設置權重 這個絕對也可以
Feign 方式:
概念:
Feign 是什麼呢? Feign 是對 Ribbon的封裝,使用註解的方式,調用起來更簡單。。。 也是主流的方式~
ribbon對比:
public List<Product> listProdcuts() {
return restTemplate.getForObject("http://PRODUCT-DATA-SERVICE/products",List.class);
}
feign對比:
@FeignClient(value = "PRODUCT-DATA-SERVICE")
public interface ProductClientFeign {
@GetMapping("/products")
public List<Product> listProdcuts();
}
好了開幹吧
pom.xml添加
spring-cloud-starter-openfeign,就是用來支持 Feign 方式的。
<?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>springcloud</artifactId>
<groupId>cn.how2j.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product-view-service-feign</artifactId>
<dependencies>
<!--client客戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
pojo:
package how2j.springcloud.pojo;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
public class Product {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Product() {
}
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
Feign 客戶端
Feign 客戶端, 通過 註解方式 訪問 訪問PRODUCT-DATA-SERVICE服務的 products路徑, product-data-service 既不是域名也不是ip地址,而是 數據服務在 eureka 註冊中心的名稱。
注意看,這裏只是指定了要訪問的 微服務名稱,但是並沒有指定端口號到底是 8001, 還是 8002.
package how2j.springcloud.client;
import how2j.springcloud.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Component
@FeignClient(value = "product-data-service") //數據微服務的名稱
public interface ProductClientFeign {
@GetMapping("/products")
public List<Product> listProdcuts(); //返回對象list
}
服務類
package how2j.springcloud.service;
import how2j.springcloud.client.ProductClientFeign;
import how2j.springcloud.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@Service
public class ProductService {
@Autowired
ProductClientFeign productClientFeign;
public List<Product> listProducts(){
// List<Product> ps = new ArrayList<>();
//ps.add(new Product(1,"product a from port:"+port, 50));
// ps.add(new Product(2,"product b from port:"+port, 150));
// ps.add(new Product(3,"product c from port:"+port, 250));
// return ps;
return productClientFeign.listProdcuts();
}
}
控制器
package how2j.springcloud.controller;
import how2j.springcloud.pojo.Product;
import how2j.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@ResponseBody
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public Object products(Model m) {
List<Product> ps = productService.listProducts();
m.addAttribute("ps", ps);
return ps.toString();
}
}
啓動類
package how2j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author 魏
* @Date 2020/3/4 0004
**/
@SpringBootApplication //springboot啓動
@EnableEurekaClient //client註冊到服務中心
@EnableDiscoveryClient //啓用發現其他微服務
@EnableFeignClients //使用feign
public class ProductViewServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ProductViewServiceRibbonApplication.class, args);
}
}
application.yml
端口修改8011
#微服務名,以及thymeleaf設置
spring:
application:
name: product-view-service-ribbon
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: html
servlet:
content-type: text/html
encoding: utf-8
mode: html5
#和服務中心配置一致
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
#自己的端口
server:
port: 8011
刪除掉config
測試:
可以看到有4個微服務 數據微服務和視圖微服務 各有兩個集羣
訪問http://localhost:8011/products 採用的-feign,不用配置config
待續…
發現問題了,html之前的錯誤是因爲後綴忘記加.了 應該是
#微服務名,以及thymeleaf設置
spring:
application:
name: product-view-service-ribbon
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
servlet:
content-type: text/html
encoding: utf-8
mode: html5
數據微服務 controller負責返回json數據
視圖微服務 feign 接口(相當於serverImpl類)返回一個pojo對象或者list 或者 map封裝的多個對象或者其他數據
視圖微服務 controller 調用接口數據… 組成視圖
鏈接:https://pan.baidu.com/s/13WRo6Wr-0cdIrPTs4jyffg
提取碼:p016