前面一篇文章 寫了第一種 實現微服務架構 現在實現第二種 依然是傻瓜
第一篇博文鏈接 https://blog.csdn.net/qq_41684939/article/details/89602525
這是基於上一篇博文寫的
註冊中心 可以在上一篇看到
fegin實現微服務架構
- 提供者目錄結構
- 提供者uml
server:
port: 8814
eureka:
client:
serviceUrl:
defaultZone: http://192.168.88.200:12345/eureka/
security:
basic:
enabled: false
spring:
application:
name: service-fregin
feign:
hystrix:
enabled: true # 開啓 Feign 對 Hystrix 的支持
compression:
request:
enabled: true # 開啓對 request 請求壓縮
mime-types:
- text/xml
- application/xml
- application/json # 指定壓縮格式
min-request-size: 2048 # 壓縮的最小閥值,默認 2048,超過2048 (字節) 進行壓縮。
response:
enabled: true # 開啓對 response 的壓縮
- 提供者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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.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-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 提供者啓動類
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.example"})
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
- 提供者 controller
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/fregin")
@RestController
public class ServiceController {
@GetMapping("/hello")
public String hello() {
String s="測試開源框架fregin"+"這是service層:port="+"8814";
return s ;
}
}
- 啓動成功
7. 定義 消費者(目錄結構)
8. yml 配置
server:
port: 8815
eureka:
client:
serviceUrl:
defaultZone: http://192.168.88.200:12345/eureka/
security:
basic:
enabled: false
spring:
application:
name: service-fregin-server
feign:
hystrix:
enabled: true # 開啓 Feign 對 Hystrix 的支持
compression:
request:
enabled: true # 開啓對 request 請求壓縮
mime-types:
- text/xml
- application/xml
- application/json # 指定壓縮格式
min-request-size: 2048 # 壓縮的最小閥值,默認 2048,超過2048 (字節) 進行壓縮。
response:
enabled: true # 開啓對 response 的壓縮
- 啓動類
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@ComponentScan(basePackages = {"com.example"})
@EnableFeignClients(basePackages={"com.example.*"})
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
- controller 編輯
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.service.DemoService;
@RequestMapping("/freginServer")
@RestController
public class ServiceController {
@Autowired
DemoService service;
@GetMapping("/hello")
public String hello() {
return service.hello();
}
}
- serveice 編輯
package com.example.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value="service-fregin")
public interface DemoService {
@RequestMapping("/fregin/hello")
String hello();
}
- 啓動成功 訪問