The previous article wrote the first way to implement microservice architecture, and now the second way is still a fool
First blog link https://blog.csdn.net/qq_41684939/article/details/89602525
This is based on the last blog post
The registration center can see in the previous article
fegin implementation of microservice architecture
- Provider directory structure
- Provider 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 # Enable Feign's support for Hystrix compression: request: enabled: true # Turn on request compression mime-types: - text/xml - application/xml - application/json # Specify compression format min-request-size: 2048 # The minimum threshold value of compression, 2048 by default, more than 2048 (bytes) for compression. response: enabled: true # Turn on compression of response
- Provider 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>
- Provider startup class
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); } }
- Provider 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="Test open source framework fregin"+"This is service Layer: port="+"8814"; return s ; } }
- Startup success
7. Define consumers (directory structure)
8. yml configuration
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 # Enable Feign's support for Hystrix compression: request: enabled: true # Turn on request compression mime-types: - text/xml - application/xml - application/json # Specify compression format min-request-size: 2048 # The minimum threshold value of compression, 2048 by default, more than 2048 (bytes) for compression. response: enabled: true # Turn on compression of response
- Startup class
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 edit
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(); } }
- Edit service
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(); }
- Start successful access