springcloud fegin和rabbot分别实现微服务(二)
作者:互联网
前面一篇文章 写了第一种 实现微服务架构 现在实现第二种 依然是傻瓜
第一篇博文链接 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();
}
- 启动成功 访问
标签:web,fegin,springcloud,rabbot,springframework,org,import,annotation,cloud 来源: https://blog.csdn.net/qq_41684939/article/details/89602909